Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Read settings once, parse file directly without "readBytes" and "buffer pointer" before #570

Closed
nardev opened this issue Sep 1, 2017 · 1 comment
Labels
v5 ArduinoJson 5

Comments

@nardev
Copy link

nardev commented Sep 1, 2017

I was able to directly pass File to a parseObject and it was read properly, but when i added the struct, the way you suggest it to be added in another thread, it wont accept the File any more.

This was working ok:

  DynamicJsonBuffer configFileJB;
  File configFile = SPIFFS.open("/settings.json", "r");

  if (!configFile) {
    Serial.println("Failed to open config file");
  }

  JsonObject& settings = configFileJB.parseObject(configFile);

Now when i added the struct:

struct JsonBundle {
  public:
    void parse(const char* json) {
      _jsonBuffer = new DynamicJsonBuffer();
      _jsonVariant = _jsonBuffer->parseObject(json);
    }

    const JsonObject& root() const {
      return _jsonVariant;
    }

    const JsonObject& getNode(String key) const {
      JsonObject& i = _jsonVariant.as<JsonObject>().get<JsonVariant>(key);
      return i;
    }

  private:
    DynamicJsonBuffer* _jsonBuffer;
    JsonVariant _jsonVariant;
};

JsonBundle settings;

I can't pass File to .parse() i have to do this:

DynamicJsonBuffer configFileJB;
File configFile = SPIFFS.open("/settings.json", "r");
size_t size = configFile.size();
std::unique_ptr<char[]> buf(new char[size]);
configFile.readBytes(buf.get(), size);
settings.parse(buf.get());

How is the File recognizes in first example? Can't figure out from here: https://arduinojson.org/api/jsonbuffer/parseobject/

@nardev
Copy link
Author

nardev commented Sep 1, 2017

Ah, the File was parsed as "Stream& json" ... sorry.. it's ok...

Maybe someone would like to have this

struct JsonBundle {
  public:
    void parse(const char* json) {
      _jsonBuffer = new DynamicJsonBuffer();
      _jsonVariant = _jsonBuffer->parseObject(json);
    }

    void parse(Stream& json) {
      _jsonBuffer = new DynamicJsonBuffer();
      _jsonVariant = _jsonBuffer->parseObject(json);
    }

    const JsonObject& root() const {
      return _jsonVariant;
    }

    const JsonObject& getNode(String key) const {
      JsonObject& i = _jsonVariant.as<JsonObject>().get<JsonVariant>(key);
      return i;
    }

  private:
    DynamicJsonBuffer* _jsonBuffer;
    JsonVariant _jsonVariant;
};

JsonBundle settings;

And read like:

  DynamicJsonBuffer configFileJB;
  File configFile = SPIFFS.open("/settings.json", "r");
  settings.parse(configFile);

And access where you need it as an object:

settings.getNode("mqtt").printTo(Serial);

@nardev nardev closed this as completed Sep 1, 2017
Repository owner locked and limited conversation to collaborators Sep 21, 2018
@bblanchon bblanchon added the v5 ArduinoJson 5 label Feb 6, 2024
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
v5 ArduinoJson 5
Projects
None yet
Development

No branches or pull requests

2 participants