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

DynamicJsonDocument returns a null object #1260

Closed
sblantipodi opened this issue May 6, 2020 · 3 comments
Closed

DynamicJsonDocument returns a null object #1260

sblantipodi opened this issue May 6, 2020 · 3 comments
Labels
v6 ArduinoJson 6

Comments

@sblantipodi
Copy link

sblantipodi commented May 6, 2020

Hi all,
I have a class with a function like this

DynamicJsonDocument myClass::readConfigFile() {
    // ...
    DynamicJsonDocument doc(2048);
    deserializeJson(doc, file);

    String val = doc["value"];
    Serial.print(val); // IT WORKS OOK HERE
    return doc;
}

when In another class I call:


DynamicJsonDocument doc = myclassInstance.readConfigFile();
String val = doc["value"];
Serial.print(val); // HERE VAL IS EMPTY

Why val is empty when I try to extract it from a function call?

Thanks

@bblanchon
Copy link
Owner

Hi,

I tried to reproduce the issue, but it works as expected.
See online demo: https://wandbox.org/permlink/XrgdlP9UNQZEemNV

However, if file is of type char*, deserializeJson() uses the "zero-copy mode" and stores pointers instead of copies, so you must ensure that the input buffer stays in memory.
A simple workaround is to cast to const char* or pass the File instance directly to deserializeJson().

Best regards,
Benoit

@sblantipodi
Copy link
Author

sblantipodi commented May 7, 2020

I am trying to read/write SPIFFS in the ESP8266, it seems that closing the file before returning the DynamicJsonDocument solved the problem when reading.

I'm not using char *, I'm storing a DynamicJsonDocument inside SPIFFS,
the document I'm storing contains "String"

Still don't understand why I had the problem since no char* has been used.

Thanks for the answer, for your time and for the awesome work.
Arduino would not be that good without your lib.

@bblanchon
Copy link
Owner

I don't understand either.
Please reopen this issue if you need.

@lock lock bot locked as resolved and limited conversation to collaborators Jun 24, 2020
@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
v6 ArduinoJson 6
Projects
None yet
Development

No branches or pull requests

2 participants