Skip to content

[Question] Is there any simple approach to update a field on parsed object and print back? #553

Closed
@omersiar

Description

@omersiar

Hello, thanks for the great library, it really helped with my project. https://github.com/omersiar/esp-rfid

I have a json encoded configuration file on ESP8266 filesystem (SPIFFS) which I can quickly parse the information that i need like this:

bool loadConfiguration() {
  File configFile = SPIFFS.open("/auth/config.json", "r");
  if (!configFile) {
    Serial.println(F("[ WARN ] Failed to open config file"));
    return false;
  }
  size_t size = configFile.size();
  // Allocate a buffer to store contents of the file.
  std::unique_ptr<char[]> buf(new char[size]);
  // We don't use String here because ArduinoJson library requires the input
  // buffer to be mutable. If you don't use ArduinoJson, you may as well
  // use configFile.readString instead.
  configFile.readBytes(buf.get(), size);
  DynamicJsonBuffer jsonBuffer;
  JsonObject& json = jsonBuffer.parseObject(buf.get());
  if (!json.success()) {
    Serial.println(F("[ WARN ] Failed to parse config file"));
    return false;
  }
}

What I want to do is changing a field on parsed file for example "devicename":"esp-rfid" and write it back as a whole without breaking other fields.

Should I simply get the all the fields on memory, change the field that i want to change and create another buffer and create another JsonObject and print it to the file?

I'm already doing above, because i'm not aware of any other approach. Anyway, kind regards.

Metadata

Metadata

Assignees

No one assigned

    Labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions