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

reusing a json document - JsonDocument.clear() not clearing pool #1712

Closed
proddy opened this issue Feb 12, 2022 · 3 comments
Closed

reusing a json document - JsonDocument.clear() not clearing pool #1712

proddy opened this issue Feb 12, 2022 · 3 comments
Labels
question v6 ArduinoJson 6

Comments

@proddy
Copy link

proddy commented Feb 12, 2022

I'm finding the JsonDocument.clear() function doesn't always reset and clear the pool. I've read https://arduinojson.org/v6/how-to/reuse-a-json-document/ and I suspect it's because my json object is dangling after each clear() but wanted to report it here anyway since it used to work < 6.19.0.

Environment

ESP8266, ESP32, PlatformIO, also standalone with G++ and gcc

Reproduction

Copy into wandbox:

#include <iostream>

#include "ArduinoJson.h"

using namespace std;

int main() { 
  DynamicJsonDocument doc(1024);
  JsonObject json = doc.to<JsonObject>(); // I need the JsonObject to pass to other functions later
    
  for (int i = 1; i < 5; i++) {
      doc.clear();
      printf("Adding object %d:\n", i);
      printf(" Before: ");
      serializeJson(json, cout);
      cout<<endl;
      
      json["name"] = "name";
      json["index"] = i;
      
      printf(" After: ");
      serializeJson(json, cout);
      cout<<endl;
  }
   
}

Expected output:

Adding object 1:
 Before: {}
 After: {"name":"name","index":1}
Adding object 2:
 Before: {}
 After: {"name":"name","index":2}
Adding object 3:
 Before: {}
 After: {"name":"name","index":3}
Adding object 4:
 Before: {}
 After: {"name":"name","index":4}

Actual output:

Adding object 1:
 Before: {}
 After: {"name":"name","index":1}
Adding object 2:
 Before: {"name":"name","index":1}
 After: {"name":"name","index":2}
Adding object 3:
 Before: {"name":"name","index":2}
 After: {"name":"name","index":3}
Adding object 4:
 Before: {"name":"name","index":3}
 After: {"name":"name","index":4}

(the JsonObject json still has data after the JsonDocument clear() )

@bblanchon
Copy link
Owner

bblanchon commented Feb 13, 2022

Hi @proddy,

This is the expected behavior, and I think the documentation is pretty clear about it:

every reference acquired before clearing the JsonDocument is invalidated. In other words, you must not use a JsonArray, JsonObject, or JsonVariant created before calling JsonDocument::clear().

If you're surprised (or disappointed) by this behavior, think of std::vector::clear(): "All iterators, pointers and references related to this container are invalidated." This is a known issue in C and C++ collections; there is nothing we can do about it.

To fix you program, you just have to move JsonObject json = doc.to<JsonObject>() inside the loop.
Also, you wouldn't notice the issue if you passed the JsonDocument to serializeJson() instead of the dangling JsonObject.

Best regards,
Benoit

@proddy
Copy link
Author

proddy commented Feb 13, 2022

thanks, makes sense.

@proddy proddy closed this as completed Feb 13, 2022
@github-actions github-actions bot locked as resolved and limited conversation to collaborators Mar 16, 2022
@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
question v6 ArduinoJson 6
Projects
None yet
Development

No branches or pull requests

2 participants