You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
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"usingnamespacestd;intmain() {
DynamicJsonDocument doc(1024);
JsonObject json = doc.to<JsonObject>(); // I need the JsonObject to pass to other functions laterfor (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;
}
}
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.
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 myjson
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:
Expected output:
Actual output:
(the JsonObject json still has data after the JsonDocument clear() )
The text was updated successfully, but these errors were encountered: