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

jSonDocument property: sometimes null, sometimes not! #998

Closed
vaz83 opened this issue May 19, 2019 · 4 comments
Closed

jSonDocument property: sometimes null, sometimes not! #998

vaz83 opened this issue May 19, 2019 · 4 comments
Labels
bug v6 ArduinoJson 6

Comments

@vaz83
Copy link

vaz83 commented May 19, 2019

Hi,
i have the following code:
`void read()
{
if (SPIFFS.begin())
{
if(SPIFFS.exists("/dev.json"))
{
File infoFile = SPIFFS.open("/dev.json", "r");
if(infoFile)
{
size_t size = infoFile.size();
std::unique_ptr<char[]> buf(new char[size]);
infoFile.readBytes(buf.get(), size);

    DynamicJsonDocument jDoc(1500);
    DeserializationError error = deserializeJson(jDoc,buf.get());
    if(!error)
    {
      if(jDoc["GUID"]!=NULL)
      {
          String tempstr = jDoc["GUID"];
          if(tempstr.startsWith("SECURED."))
          {
            tempstr.replace("SECURED.","");
            jDoc["GUID"] = tempstr;
            tempstr.toCharArray(GUID,80);
            XORcrypt(GUID);
          }
          else
            strcpy(GUID, jDoc["GUID"]);
      }
    }
  }
}

}`

What i am facing now is that the condition if(jDoc["GUID"]!=NULL) is returning false, and when i pass jDoc["GUID"] to a string variable it has value! the property exists and has data. Why is it returning null? in another part of the code when i open other json files this condition works just fine, please help

@bblanchon
Copy link
Owner

Hi @vaz83,

The problem is that NULL is not a pointer, it is the integer 0, so ArduinoJson applies the conversion to integer. Since the value in doc["GUID"] is not an integer, the conversion returns 0, so the if clause runs.

I'll fix the comparison operator so that it returns false when the value has the wrong type. I'll also to add support for nullptr_t, so you can write if (doc["GUID"] != nullptr) instead.

Best Regards,
Benoit

@bblanchon bblanchon added the bug label May 19, 2019
@bblanchon
Copy link
Owner

As a temporary workaround, please use JsonObject::containsKey()

@vaz83
Copy link
Author

vaz83 commented May 19, 2019

As a temporary workaround, please use JsonObject::containsKey()

Will do. I also found out other problem, i will make a new thread. Thanks!

bblanchon added a commit that referenced this issue May 22, 2019
@UltimatPronin
Copy link

I have similar problem (on V6):
1)in my sketch I use an DynamicJsonDocument _item(256);
2)periodically I change data stored in this object and send json to my service:
_item["last_time"] = timeClient.getFormattedTime(); //string data
_item["rnd"] = random(2); //int data
and after about 100 operations of changing _item["last_time"] it continiously return null and I can not assign any value to it.
3)For now a workaround for me is pass [long type] variable [string] place to _item["last_time"]. With it all is ok.

bblanchon added a commit that referenced this issue May 24, 2019
Repository owner locked and limited conversation to collaborators Jun 26, 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
bug v6 ArduinoJson 6
Projects
None yet
Development

No branches or pull requests

3 participants