From b06cee8f4d3fc35500ebb707b732c3897cd8e814 Mon Sep 17 00:00:00 2001 From: Benoit Blanchon Date: Thu, 27 Feb 2025 19:01:26 +0100 Subject: [PATCH] Remove the overload of `setString()` for `StringNode` --- extras/tests/Deprecated/BasicJsonDocument.cpp | 2 +- extras/tests/JsonDeserializer/input_types.cpp | 2 +- extras/tests/JsonDeserializer/object.cpp | 4 ++-- extras/tests/JsonDeserializer/string.cpp | 4 ++-- src/ArduinoJson/Json/JsonDeserializer.hpp | 10 ++++------ src/ArduinoJson/MsgPack/MsgPackDeserializer.hpp | 9 ++++----- src/ArduinoJson/Object/ObjectData.hpp | 4 +++- src/ArduinoJson/Object/ObjectImpl.hpp | 16 ++++++++++++++++ src/ArduinoJson/Variant/VariantData.hpp | 5 ----- 9 files changed, 33 insertions(+), 23 deletions(-) diff --git a/extras/tests/Deprecated/BasicJsonDocument.cpp b/extras/tests/Deprecated/BasicJsonDocument.cpp index 788fc009..a3509779 100644 --- a/extras/tests/Deprecated/BasicJsonDocument.cpp +++ b/extras/tests/Deprecated/BasicJsonDocument.cpp @@ -46,7 +46,7 @@ TEST_CASE("BasicJsonDocument") { deserializeJson(doc, "{\"hello\":\"world\"}"); REQUIRE(doc.as() == "{\"hello\":\"world\"}"); doc.clear(); - REQUIRE(allocatorLog == "ARAARDDD"); + REQUIRE(allocatorLog == "AARARDDD"); } SECTION("copy") { diff --git a/extras/tests/JsonDeserializer/input_types.cpp b/extras/tests/JsonDeserializer/input_types.cpp index 21e890cc..e750fd6a 100644 --- a/extras/tests/JsonDeserializer/input_types.cpp +++ b/extras/tests/JsonDeserializer/input_types.cpp @@ -26,8 +26,8 @@ TEST_CASE("deserializeJson(char*)") { REQUIRE(spy.log() == AllocatorLog{ Allocate(sizeofStringBuffer()), - Reallocate(sizeofStringBuffer(), sizeofString("hello")), Allocate(sizeofPool()), + Reallocate(sizeofStringBuffer(), sizeofString("hello")), Allocate(sizeofStringBuffer()), Reallocate(sizeofStringBuffer(), sizeofString("world")), Reallocate(sizeofPool(), sizeofObject(1)), diff --git a/extras/tests/JsonDeserializer/object.cpp b/extras/tests/JsonDeserializer/object.cpp index d7936c95..ffae69ae 100644 --- a/extras/tests/JsonDeserializer/object.cpp +++ b/extras/tests/JsonDeserializer/object.cpp @@ -299,8 +299,8 @@ TEST_CASE("deserialize JSON object") { REQUIRE(spy.log() == AllocatorLog{ Allocate(sizeofStringBuffer()), - Reallocate(sizeofStringBuffer(), sizeofString("a")), Allocate(sizeofPool()), + Reallocate(sizeofStringBuffer(), sizeofString("a")), Allocate(sizeofStringBuffer()), Reallocate(sizeofStringBuffer(), sizeofString("b")), Allocate(sizeofStringBuffer()), @@ -378,7 +378,7 @@ TEST_CASE("deserialize JSON object under memory constraints") { } SECTION("pool allocation fails") { - timebomb.setCountdown(2); + timebomb.setCountdown(1); char input[] = "{\"a\":1}"; DeserializationError err = deserializeJson(doc, input); diff --git a/extras/tests/JsonDeserializer/string.cpp b/extras/tests/JsonDeserializer/string.cpp index 5a5c372e..5ad86786 100644 --- a/extras/tests/JsonDeserializer/string.cpp +++ b/extras/tests/JsonDeserializer/string.cpp @@ -133,8 +133,8 @@ TEST_CASE("Allocation of the key fails") { REQUIRE(spy.log() == AllocatorLog{ Allocate(sizeofStringBuffer()), - Reallocate(sizeofStringBuffer(), sizeofString("hello")), Allocate(sizeofPool()), + Reallocate(sizeofStringBuffer(), sizeofString("hello")), AllocateFail(sizeofStringBuffer()), ReallocateFail(sizeofPool(), sizeofObject(1)), }); @@ -155,8 +155,8 @@ TEST_CASE("Allocation of the key fails") { REQUIRE(spy.log() == AllocatorLog{ Allocate(sizeofStringBuffer()), - Reallocate(sizeofStringBuffer(), sizeofString("hello")), Allocate(sizeofPool()), + Reallocate(sizeofStringBuffer(), sizeofString("hello")), AllocateFail(sizeofStringBuffer()), ReallocateFail(sizeofPool(), sizeofObject(1)), }); diff --git a/src/ArduinoJson/Json/JsonDeserializer.hpp b/src/ArduinoJson/Json/JsonDeserializer.hpp index f7359f75..38b49359 100644 --- a/src/ArduinoJson/Json/JsonDeserializer.hpp +++ b/src/ArduinoJson/Json/JsonDeserializer.hpp @@ -275,13 +275,11 @@ class JsonDeserializer { if (memberFilter.allow()) { auto member = object.getMember(adaptString(key), resources_); if (!member) { - // Save key in memory pool. - auto savedKey = stringBuilder_.save(); - - // Allocate slot in object - member = object.addMember(savedKey, resources_); - if (!member) + auto keyVariant = object.addPair(&member, resources_); + if (!keyVariant) return DeserializationError::NoMemory; + + keyVariant->setOwnedString(stringBuilder_.save()); } else { member->clear(resources_); } diff --git a/src/ArduinoJson/MsgPack/MsgPackDeserializer.hpp b/src/ArduinoJson/MsgPack/MsgPackDeserializer.hpp index 8df5e412..44e3cdd0 100644 --- a/src/ArduinoJson/MsgPack/MsgPackDeserializer.hpp +++ b/src/ArduinoJson/MsgPack/MsgPackDeserializer.hpp @@ -408,12 +408,11 @@ class MsgPackDeserializer { if (memberFilter.allow()) { ARDUINOJSON_ASSERT(object != 0); - // Save key in memory pool. - auto savedKey = stringBuffer_.save(); - - member = object->addMember(savedKey, resources_); - if (!member) + auto keyVariant = object->addPair(&member, resources_); + if (!keyVariant) return DeserializationError::NoMemory; + + keyVariant->setOwnedString(stringBuffer_.save()); } else { member = 0; } diff --git a/src/ArduinoJson/Object/ObjectData.hpp b/src/ArduinoJson/Object/ObjectData.hpp index 0d17c2f7..9b989628 100644 --- a/src/ArduinoJson/Object/ObjectData.hpp +++ b/src/ArduinoJson/Object/ObjectData.hpp @@ -10,9 +10,11 @@ ARDUINOJSON_BEGIN_PRIVATE_NAMESPACE class ObjectData : public CollectionData { public: - template // also works with StringNode* + template VariantData* addMember(TAdaptedString key, ResourceManager* resources); + VariantData* addPair(VariantData** value, ResourceManager* resources); + template VariantData* getOrAddMember(TAdaptedString key, ResourceManager* resources); diff --git a/src/ArduinoJson/Object/ObjectImpl.hpp b/src/ArduinoJson/Object/ObjectImpl.hpp index 0f78cac4..67677d74 100644 --- a/src/ArduinoJson/Object/ObjectImpl.hpp +++ b/src/ArduinoJson/Object/ObjectImpl.hpp @@ -68,6 +68,22 @@ inline VariantData* ObjectData::addMember(TAdaptedString key, return valueSlot.ptr(); } +inline VariantData* ObjectData::addPair(VariantData** value, + ResourceManager* resources) { + auto keySlot = resources->allocVariant(); + if (!keySlot) + return nullptr; + + auto valueSlot = resources->allocVariant(); + if (!valueSlot) + return nullptr; + *value = valueSlot.ptr(); + + CollectionData::appendPair(keySlot, valueSlot, resources); + + return keySlot.ptr(); +} + // Returns the size (in bytes) of an object with n members. constexpr size_t sizeofObject(size_t n) { return 2 * n * ResourceManager::slotSize; diff --git a/src/ArduinoJson/Variant/VariantData.hpp b/src/ArduinoJson/Variant/VariantData.hpp index 8ebd8255..ab00b42d 100644 --- a/src/ArduinoJson/Variant/VariantData.hpp +++ b/src/ArduinoJson/Variant/VariantData.hpp @@ -488,11 +488,6 @@ class VariantData { template bool setString(TAdaptedString value, ResourceManager* resources); - bool setString(StringNode* s, ResourceManager*) { - setOwnedString(s); - return true; - } - template static void setString(VariantData* var, TAdaptedString value, ResourceManager* resources) {