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

serializeJson to SPIFFS seems not working for me after upgrade 5->6 #958

Closed
tschaban opened this issue Apr 1, 2019 · 4 comments
Closed
Labels
question v6 ArduinoJson 6

Comments

@tschaban
Copy link

tschaban commented Apr 1, 2019

Hi Folks, I have just upgraded from version 5 to 6, and after a couple of modifications required for version 6 I still can't save to SPIFFS using serializeJson

  • Platform: ESP8266 / Testing on ESP12e

I prepared a sample sketch and I can't spot the issue. The similar code on AJson 5 works very well.
Would you advise please? Much appriciate.

#include <ArduinoJson.h>
#include <FS.h>

struct someStructure {
  char helloWorld[11];
};

void load() {
  Serial.println(F("Loading file"));
  File configFile = SPIFFS.open("test.json", "r");

  size_t size = configFile.size();
  std::unique_ptr<char[]> buf(new char[size]);
  configFile.readBytes(buf.get(), size);

  StaticJsonDocument<100> doc;
  DeserializationError error = deserializeJson(doc, configFile);
  if (error) {
    Serial.println(F("Error: deserializeJson"));
    Serial.println(error.c_str());
  }
  Serial.print(F("serializeJson = "));
  serializeJson(doc, Serial);

  configFile.close();
}

void save() {
  Serial.println(F("Saving file"));

  File configFile = SPIFFS.open("test.json", "w");

  if (configFile) {
    Serial.println(F("File opened"));
    StaticJsonDocument<100> doc;
    //  DynamicJsonDocument doc(2048);

    doc["helloWorld"] = "Hello World";
    Serial.print(F("Serializing to file. Size = "));
    uint16 size = serializeJson(doc, configFile);
    Serial.println(size);

  }
  configFile.close();
}

void setup() {

  Serial.begin(9600);

  if (SPIFFS.begin()) {
    Serial.println(F("\nSPIFFS mounted"));
  } else {
    Serial.println(F("\nFailed to mount SPIFFS"));
  }

  Serial.println(F("Formating SPIFFS "));
  if (SPIFFS.format()) {
    Serial.println(F("done"));
  } else {
    Serial.println(F("ERROR"));
  }

  delay(1000);

  save();

  delay(1000);

  load();
}

void loop() {}

Result:

@bblanchon
Copy link
Owner

Hi @tschaban,

Your program read the file twice.
When you call deserializeJson(), the read pointer is already at the end of the file, so it doesn't find any JSON input and returns IncompleteInput (there is no dedicated error code for an empty input).

Simply remove the call to readBytes() and your program will work:

void load() {
  Serial.println(F("Loading file"));
  File configFile = SPIFFS.open("test.json", "r");
  
  StaticJsonDocument<100> doc;
  DeserializationError error = deserializeJson(doc, configFile);
  if (error) {
    Serial.println(F("Error: deserializeJson"));
    Serial.println(error.c_str());
  }
  Serial.print(F("serializeJson = "));
  serializeJson(doc, Serial);

  configFile.close();
}

Regards,
Benoit

@tschaban
Copy link
Author

tschaban commented Apr 3, 2019

I see, thanks @bblanchon
I'll play with it late evening and confirm it. Warm regards.

@tschaban
Copy link
Author

tschaban commented Apr 5, 2019

Hi @bblanchon,

I wanted to confirm that it works well.
Good piece of the code !
thx

@tschaban tschaban closed this as completed Apr 5, 2019
@bblanchon
Copy link
Owner

Cool! Thanks for the update @tschaban, and good luck with your project.

Repository owner locked and limited conversation to collaborators May 6, 2019
@bblanchon bblanchon added the v6 ArduinoJson 6 label Feb 6, 2024
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
question v6 ArduinoJson 6
Projects
None yet
Development

No branches or pull requests

2 participants