Code Monkey home page Code Monkey logo

Comments (6)

bblanchon avatar bblanchon commented on June 19, 2024 1

I worked several hours on this issue but could not find an elegant solution that doesn't increase the code size significantly.
If it were a functionality used by many users, I would accept this penalty, but you are the first to notice this problem in 10 years.
I plan to implement the fix in ArduinoJson 7, which has looser code size constraints.

from arduinojson.

gonzabrusco avatar gonzabrusco commented on June 19, 2024

Anticipating the possible response that may come, in my project's API, I don't have direct access to the JsonDocument, so I can't use the capacity() memoryUsage() methods to try to workaround this issue.

from arduinojson.

gonzabrusco avatar gonzabrusco commented on June 19, 2024

Ok. I worked around it keeping a count of the JsonObject that were successfully added to the JsonArray and when it exits the loop (because the last addition failed due to memory), I compare the size() of the JsonArray with that count. If they differ, then It means that although the addition failed, something was added nevertheless, so I have to delete the last item of the array (the partially added JsonObject).

from arduinojson.

bblanchon avatar bblanchon commented on June 19, 2024

Hi @gonzabrusco,

I guess I could add a special treatment for add() so as to provide this "transactional guarantee", but I cannot do the same for set() since the original value is lost.

Also, this "transactional guarantee" would be very limited since you'd not necessarily return to the state before the operation. For example, if add failed in the following program:

doc["config"]["wifi"].add(newAccessPointObject);

If doc was initially:

{"config":{"url":"api.example.com"}}

After "reverting" the failing add(), we would end up with:

{"config":{"url":"api.example.com","wifi":[]}}

It is probably okay, but definitely not the same as the original.

Moreover, because ArduinoJson 6's allocator is monotonic, we cannot recover the memory consumed by the removed object.

Best regards,
Benoit

from arduinojson.

gonzabrusco avatar gonzabrusco commented on June 19, 2024

Yes I understand what you are saying but I still think that what I propose would be the correct choice in this regard. If the add method fails, it should not leave "a partial copy" of a JSON.

In the example you provided, it "fails" because you are doing two things at a time (creating an array and then adding an item). I think in that case the user should use JsonDocument::createNestedArray() and check the return value before adding items. Or just leave the array empty (which is probably fine because the other end is probably expecting the array, empty or not).

from arduinojson.

gonzabrusco avatar gonzabrusco commented on June 19, 2024

Fair enough. I will stick to my workaround then. Thank you for taking the time to evaluate this issue.

Feel free to what you want with this issue report.

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.