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

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

Closed
omersiar opened this issue Jul 11, 2017 · 2 comments
Labels
question v5 ArduinoJson 5

Comments

@omersiar
Copy link

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.

@omersiar omersiar changed the title [Question] Is there any simple approach to update a field parsed object and print back? [Question] Is there any simple approach to update a field on parsed object and print back? Jul 11, 2017
@bblanchon
Copy link
Owner

bblanchon commented Jul 12, 2017

Hello @omersiar,

If you need to change a field in the JsonObject, you can modify it "in-place" and print back to file.

By the way, I'm not familiar with SPIFFS, butI believe you can pass the File object directly to parseObject().

Here is the scheme you could use:

File oldConfigFile, newConfigFile;
JsonObject& json = jsonBuffer.parseObject(configFile);
json["hello"] = "world";
json.printTo(newConfigFile);

See also:

Regards,
Benoit

@omersiar
Copy link
Author

Hello @bblanchon,

Thank you for the explanation. I can not believe this was that easy, thank you Sir, kind regards.

P.S. I will add this to wiki (bag of tricks maybe?), i searched everywhere for this.

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
question v5 ArduinoJson 5
Projects
None yet
Development

No branches or pull requests

2 participants