Skip to content

JsonArray.add(JsonObject) adds partial JsonObject when the JsonDocument is full #2081

@gonzabrusco

Description

@gonzabrusco

Describe the bug
Not sure if a bug or a design choice. But this is the workflow to reproduce this issue:

  • Use ArduinoJSON V6
  • Create a JsonDocument with fixed size (static or dynamic).
  • Create a JsonArray inside that JsonDocument.
  • Create a loop that fills the JsonArray with JsonObjects like this {"id":3,"test":"this is a test", "another_key":"random text"}
  • The loop exits when JsonArray.add() returns false (no more memory in the JsonDocument to add another item in the array).

When checking what was saved in the JsonArray, I find something like this:

[
 {"id":3,"test":"this is a test", "another_key":"random text"}, 
 {"id":3,"test":"this is a test", "another_key":"random text"},
 {"id":3,"test":"this is a test", "another_key":"random text"},
 {"id":3,"test":"this is a test", "another_key":"random text"},
 {"id":3}  <--- PARTIAL JSON!!
]

It seems that when you call the method add() it returns false if there's no more memory to fully add the JsonObject, but it leaves a partial JSON in the array. I think this should not be the correct behavior. It should notify if there's not enough memory to store the JsonObject but leave the array untouched. Adding a partial JSON probably is a bad idea in most situations (API breaker).

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugv6ArduinoJson 6

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions