BREAKING CHANGES
Static string cannot contain NUL characters anymore (they could since 7.3.0). This is an extremely rare case, so you probably won't be affected.
For example, the following code produces different output in 7.3 and 7.4:
JsonDocument doc; doc["a\0b"] = "c\0d"; serializeJson(doc, Serial); // With Arduino 7.3 -> {"a\u0000b":"c\u0000d"} // With Arduino 7.4 -> {"a":"c"}
JsonStringcontructor now only accepts two arguments, not three. If your code usesJsonStringto store a string as a pointer, you must remove the size argument.For example, if you have something like this:
doc["key"] = JsonString(str.c_str(), str.size(), true);You must replace with either:
doc["key"] = JsonString(str.c_str(), true); // store as pointer, cannot contain NUL characters doc["key"] = JsonString(str.c_str(), str.size()); // store by copy, NUL characters allowed doc["key"] = str; // same as previous line for supported string classes (`String`, `std::string`, etc.)
const char[] (issue #2166)deserializeJson()ElementProxy and MemberProxy non-copyableJsonString is now stored by copy, unless specified otherwiseJsonString::Ownership with boolJsonString::isLinked() to isStatic()BREAKING CHANGES
In previous versions,
MemberProxy(the class returned byoperator[]) could lead to dangling pointers when used with a temporary string. To prevent this issue,MemberProxyandElementProxyare now non-copyable.Your code is likely to be affected if you use
autoto store the result ofoperator[]. For example, the following line won't compile anymore:auto value = doc["key"];To fix the issue, you must append either
.as<T>()or.to<T>(), depending on the situation.For example, if you are extracting values from a JSON document, you should update like this:
- auto config = doc["config"]; + auto config = doc["config"].as<JsonObject>(); const char* name = config["name"];However, if you are building a JSON document, you should update like this:
- auto config = doc["config"]; + auto config = doc["config"].to<JsonObject>(); config["name"] = "ArduinoJson";
deserializeJson(JsonArray|JsonObject, ...) (issue #2135)JsonDocument::set()operator[](variant) ignoring NUL charactersdouble and long long) in an additional slotserializeJson() et al.ARDUINOJSON_USE_DOUBLE to 0 by default on 8-bit architecturescontainsKey() in favor of doc["key"].is<T>()\' (issue #2124)| Architecture | before | after |
|---|---|---|
| 8-bit | 8 bytes | 6 bytes |
| 32-bit | 16 bytes | 8 bytes |
| 64-bit | 24 bytes | 16 bytes |
BREAKING CHANGES
After being on the death row for years, the
containsKey()method has finally been deprecated. You should replacedoc.containsKey("key")withdoc["key"].is<T>(), which not only checks that the key exists but also that the value is of the expected type.// Before if (doc.containsKey("value")) { int value = doc["value"]; // ... } // After if (doc["value"].is<int>()) { int value = doc["value"]; // ... }
ARDUINOJSON_STRING_LENGTH_SIZE to the namespace namedeserializeMsgPack()JsonVariant as a key or index (issue #2080)
Note: works only for reading, not for writingElementProxy and MemberProxy in JsonDocument's constructorARDUINOJSON_USE_LONG_LONG is 0
(they are set to null if they don't fit in a long)JSON_STRING_SIZE(N) return N+1 to fix third-party code (issue #2054)char or char* (issue #2043)poolIndex < count_ after JsonDocument::clear() (issue #2034)JsonObjectConst::operator[] (issue #2019)volatile bool serialized as 1 or 0 instead of true or false (issue #2029)BasicJsonDocumentStaticJsonDocumentAllocator classDynamicJsonDocument with JsonDocumentJSON_ARRAY_SIZE(), JSON_OBJECT_SIZE(), and JSON_STRING_SIZE()ARDUINOJSON_ENABLE_STRING_DEDUPLICATION (string deduplication cannot be disabled anymore)JsonDocument::capacity()serialized("string") by copy (#1915)deserializeJson() and deserializeMsgPack()to<JsonVariant>()size() in serializeMsgPack()ARDUINOJSON_SLOT_OFFSET_SIZE in the namespace nameJsonVariant::shallowCopy()JsonDocument's capacity grows as needed, no need to pass it to the constructor anymoreJsonDocument's allocator is not monotonic anymore, removed values get recycledJsonDocument::memoryUsage()JsonDocument::garbageCollect()deserializeJson(JsonVariant, ...) and deserializeMsgPack(JsonVariant, ...) (#1226)shrinkToFit() in deserializeJson() and deserializeMsgPack()serializeJson() and serializeMsgPack() replace the content of std::string and String instead of appending to itadd() with add<T>() (add(T) is still supported)createNestedArray() and createNestedObject() (use to<JsonArray>() and to<JsonObject>() instead)BREAKING CHANGES
As every major release, ArduinoJson 7 introduces several breaking changes. I added some stubs so that most existing programs should compile, but I highty recommend you upgrade your code.
JsonDocumentIn ArduinoJson 6, you could allocate the memory pool on the stack (with
StaticJsonDocument) or in the heap (withDynamicJsonDocument).
In ArduinoJson 7, the memory pool is always allocated in the heap, soStaticJsonDocumentandDynamicJsonDocumenthave been merged intoJsonDocument.In ArduinoJson 6,
JsonDocumenthad a fixed capacity; in ArduinoJson 7, it has an elastic capacity that grows as needed. Therefore, you don't need to specify the capacity anymore, so the macrosJSON_ARRAY_SIZE(),JSON_OBJECT_SIZE(), andJSON_STRING_SIZE()have been removed.// ArduinoJson 6 StaticJsonDocument<256> doc; // or DynamicJsonDocument doc(256); // ArduinoJson 7 JsonDocument doc;In ArduinoJson 7,
JsonDocumentreuses released memory, sogarbageCollect()has been removed.
shrinkToFit()is still available and releases the over-allocated memory.Due to a change in the implementation, it's not possible to store a pointer to a variant from another
JsonDocument, soshallowCopy()has been removed.In ArduinoJson 6, the meaning of
memoryUsage()was clear: it returned the number of bytes used in the memory pool.
In ArduinoJson 7, the meaning ofmemoryUsage()would be ambiguous, so it has been removed.Custom allocators
In ArduinoJson 6, you could specify a custom allocator class as a template parameter of
BasicJsonDocument.
In ArduinoJson 7, you must inherit fromArduinoJson::Allocatorand pass a pointer to an instance of your class to the constructor ofJsonDocument.// ArduinoJson 6 class MyAllocator { // ... }; BasicJsonDocument<MyAllocator> doc(256); // ArduinoJson 7 class MyAllocator : public ArduinoJson::Allocator { // ... }; MyAllocator myAllocator; JsonDocument doc(&myAllocator);
createNestedArray()andcreateNestedObject()In ArduinoJson 6, you could create a nested array or object with
createNestedArray()andcreateNestedObject().
In ArduinoJson 7, you must useadd<T>()orto<T>()instead.For example, to create
[[],{}], you would write:// ArduinoJson 6 arr.createNestedArray(); arr.createNestedObject(); // ArduinoJson 7 arr.add<JsonArray>(); arr.add<JsonObject>();And to create
{"array":[],"object":{}}, you would write:// ArduinoJson 6 obj.createNestedArray("array"); obj.createNestedObject("object"); // ArduinoJson 7 obj["array"].to<JsonArray>(); obj["object"].to<JsonObject>();