Giter VIP home page Giter VIP logo

Comments (7)

bblanchon avatar bblanchon commented on June 2, 2024 1

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,
Benoit

from arduinojson.

hitecSmartHome avatar hitecSmartHome commented on June 2, 2024

Thanks for the suggestion.

The question is that if I serialize it why do i get string "2" instead of a string object.

from arduinojson.

hitecSmartHome avatar hitecSmartHome commented on June 2, 2024

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;
}

from arduinojson.

bblanchon avatar bblanchon commented on June 2, 2024

Indeed, I just realized that doc[var] doesn't work when var is a string; it only works with integers.
This is clearly a bug.
Until I fix it, please use zonesObj[newZoneData["id"].as<JsonString>()] = newZoneData; instead.

About your original issue, where serializeJson() produces 2 as the output JSON document, this could be due to the initial data in the zones buffer.
Please add a terminator at the beginning of the buffer to ensure that deserializeJson() sees an empty input.

  if( !hshSystem.alloc(zones,ZONES_SIZE) ){
      printf("FAILED TO ALLOCATE ZONES ARRAY\n");
+     return;
  }
+ zones[0] = 0;

from arduinojson.

hitecSmartHome avatar hitecSmartHome commented on June 2, 2024

I see. Will check it, thank you very much! And I will try with JsonString also.

from arduinojson.

hitecSmartHome avatar hitecSmartHome commented on June 2, 2024

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;

from arduinojson.

hitecSmartHome avatar hitecSmartHome commented on June 2, 2024

It works fine. Thank you very much for the help. I appreciate it.

from arduinojson.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.