Skip to content

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

Closed
@nardev

Description

@nardev

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/

Metadata

Metadata

Assignees

No one assigned

    Labels

    v5ArduinoJson 5

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions