-
-
Notifications
You must be signed in to change notification settings - Fork 1.1k
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
Initial serializing #2079
Comments
Hi @hitecSmartHome, It looks like you can simplify your code like so: boolean ComponentHandler::updateZones(JsonObject newZoneData){
JsonDocument zonesObj;
DeserializationError error = deserializeJson(zonesObj, (const char*)zones);
if(error){ zonesObj.clear(); } // I have tried to just ignore the error
- std::string id = newZoneData["id"];
- if( zonesObj[id].isNull() ){
- JsonObject zone = zonesObj[id].to<JsonObject>();
- zone.set( newZoneData );
- }else{
- zonesObj[id].set( newZoneData );
- }
+ zonesObj[newZoneData["id"]] = newZoneData;
return serializeJson(zonesObj, zones, ZONES_SIZE) > 0;
} But what is your question exactly? Best regards, |
Thanks for the suggestion. The question is that if I serialize it why do i get string "2" instead of a string object. |
It doesn't work like that boolean ComponentHandler::updateZones(JsonObject newZoneData){
JsonDocument zonesObj;
deserializeJson(zonesObj, (const char*)zones); // Doesn't matter if the zones char array is empty or not.
zonesObj[newZoneData["id"]] = newZoneData; // Does not save it. I have to use set() method
return serializeJson(zonesObj, zones, ZONES_SIZE) > 0;
} |
Indeed, I just realized that About your original issue, where if( !hshSystem.alloc(zones,ZONES_SIZE) ){
printf("FAILED TO ALLOCATE ZONES ARRAY\n");
+ return;
}
+ zones[0] = 0; |
I see. Will check it, thank you very much! And I will try with JsonString also. |
Confirmed it is working with JsonString. boolean ComponentHandler::updateZones(JsonObject newZoneData){
JsonDocument zonesObj;
deserializeJson(zonesObj, (const char*)zones);
zonesObj[newZoneData["id"].as<JsonString>()] = newZoneData;
return serializeJson(zonesObj, zones, ZONES_SIZE) > 0;
} Thank you very much. Will test this if( !hshSystem.alloc(zones,ZONES_SIZE) ){
printf("FAILED TO ALLOCATE ZONES ARRAY\n");
+ return;
}
+ zones[0] = 0; |
It works fine. Thank you very much for the help. I appreciate it. |
Hello I need some help!
I'm using Arduino as an IDF component.
My device is an ESP32-Wrover-E ( 16mb ram and 4mb psram )
I'm using ArduinoJson V7.
I have a global char array which is allocated from External ram. This char array is used for syncronisation purposes across my code.
It contains fresh information about components.
Here is my function which I have problem with.
The thing is that initially the
zones
char array is empty. So thedeserializeJson
will fail.The zones object looks something like this:
When the
zones
char array is empty sometimes I get2
from theserializeJson
which is bigger than 0 so the function will return true.I'm allocating ram for it like this
alloc
method is just a wrapper for ps_mallocSo the idea of this function is that it tries to deserialize the zones array first so I can have an object representation of the global zones.
After that it checks if a zone with the given id exists. If it exists, it will overwrite, if it does not exists it creates it.
But when serialization happens, i got
2
as the output.The text was updated successfully, but these errors were encountered: