Explorar el Código

Remove the overload of `setString()` for `StringNode`

Benoit Blanchon hace 10 meses
padre
commit
b06cee8f4d

+ 1 - 1
extras/tests/Deprecated/BasicJsonDocument.cpp

@@ -46,7 +46,7 @@ TEST_CASE("BasicJsonDocument") {
     deserializeJson(doc, "{\"hello\":\"world\"}");
     REQUIRE(doc.as<std::string>() == "{\"hello\":\"world\"}");
     doc.clear();
-    REQUIRE(allocatorLog == "ARAARDDD");
+    REQUIRE(allocatorLog == "AARARDDD");
   }
 
   SECTION("copy") {

+ 1 - 1
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)),

+ 2 - 2
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);

+ 2 - 2
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)),
             });

+ 4 - 6
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_);
         }

+ 4 - 5
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;
       }

+ 3 - 1
src/ArduinoJson/Object/ObjectData.hpp

@@ -10,9 +10,11 @@ ARDUINOJSON_BEGIN_PRIVATE_NAMESPACE
 
 class ObjectData : public CollectionData {
  public:
-  template <typename TAdaptedString>  // also works with StringNode*
+  template <typename TAdaptedString>
   VariantData* addMember(TAdaptedString key, ResourceManager* resources);
 
+  VariantData* addPair(VariantData** value, ResourceManager* resources);
+
   template <typename TAdaptedString>
   VariantData* getOrAddMember(TAdaptedString key, ResourceManager* resources);
 

+ 16 - 0
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;

+ 0 - 5
src/ArduinoJson/Variant/VariantData.hpp

@@ -488,11 +488,6 @@ class VariantData {
   template <typename TAdaptedString>
   bool setString(TAdaptedString value, ResourceManager* resources);
 
-  bool setString(StringNode* s, ResourceManager*) {
-    setOwnedString(s);
-    return true;
-  }
-
   template <typename TAdaptedString>
   static void setString(VariantData* var, TAdaptedString value,
                         ResourceManager* resources) {