1. The forums will be archived and moved to a read only mode in about 2 weeks (mid march).

Correct Way To Get JSON Config?

Discussion in 'Development' started by eDroid, Dec 27, 2016.

  1. eDroid

    eDroid Witch

    Messages:
    59
    GitHub:
    edroiid
    what would be the correct way to get a json config?

    PHP:
    @mkdir($this->getDataFolder());
    $this->saveDefaultConfig();
    $this->configPath $this->getDataFolder() . 'config.json';
    $file file_get_contents($this->configPath);
    $this->config json_decode($filetrue);   
    unset(
    $file);
    or

    PHP:
    $this->config = new Config($this->getDataFolder() . 'config.json'CONFIG::JSON);
    or is there an easier way?
     
  2. Diduhless

    Diduhless Baby Zombie

    Messages:
    199
    GitHub:
    Diduhless
    PHP:
    $json = new Config($this->getDataFolder() . "config.json"Config::JSON);
    $config json_decode(file_get_contents($json), true);
     
  3. eDroid

    eDroid Witch

    Messages:
    59
    GitHub:
    edroiid
    kk ill try it :)
     
    Diduhless likes this.
  4. eDroid

    eDroid Witch

    Messages:
    59
    GitHub:
    edroiid
    also would i have to use
    PHP:
    $this->saveDefaultConfig();
     
  5. Diduhless

    Diduhless Baby Zombie

    Messages:
    199
    GitHub:
    Diduhless
    Put it on onDisable()
     
  6. Primus

    Primus Zombie Pigman

    Messages:
    749
    This is wrong. Argument 1 for json_decode must be string, encoded data in json format, however file_get_contents call will return boolean false and raise warning message. Long story short, you can't pass Config object to file_get_contents.
    By doing Config::save() on config object which has Config::JSON type will decode holding data into json for you! That's why this class exists in first place.
    "Why you have to reinvent the wheel"? Simply use Config object with json format as you already showed in second example :)
    He meant inside.

    Edit: If you want to decode and save the data manually
    PHP:
    file_put_contents("file.json"json_encode($config->getAll()));
     
    Last edited: Dec 27, 2016
    applqpak likes this.
  7. Diduhless

    Diduhless Baby Zombie

    Messages:
    199
    GitHub:
    Diduhless
    Okay sir lol
     
  8. eDroid

    eDroid Witch

    Messages:
    59
    GitHub:
    edroiid
    thanks! i got it done :)
     
  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.