Przeglądaj źródła

Fix `JsonVariant::memoryUsage()` for raw strings

Benoit Blanchon 4 lat temu
rodzic
commit
599e927590

+ 1 - 0
CHANGELOG.md

@@ -11,6 +11,7 @@ HEAD
 * Add `as<JsonString>()` and `is<JsonString>()`
 * Add safe bool idiom in `JsonString`
 * Remove `DeserializationError == bool` and `DeserializationError != bool`
+* Fix `JsonVariant::memoryUsage()` for raw strings
 
 v6.18.5 (2021-09-28)
 -------

+ 3 - 1
extras/tests/JsonVariant/memoryUsage.cpp

@@ -30,10 +30,12 @@ TEST_CASE("JsonVariant::memoryUsage()") {
   SECTION("returns size of owned string") {
     var.set(std::string("hello"));
     REQUIRE(var.memoryUsage() == 6);
+    REQUIRE(var.memoryUsage() == doc.memoryUsage());
   }
 
   SECTION("returns size of owned raw") {
     var.set(serialized(std::string("hello")));
-    REQUIRE(var.memoryUsage() == 5);
+    REQUIRE(var.memoryUsage() == 6);
+    REQUIRE(var.memoryUsage() == doc.memoryUsage());
   }
 }

+ 3 - 1
src/ArduinoJson/Variant/VariantData.hpp

@@ -257,7 +257,9 @@ class VariantData {
       case VALUE_IS_OWNED_STRING:
         return strlen(_content.asString) + 1;
       case VALUE_IS_OWNED_RAW:
-        return _content.asRaw.size;
+        // We always add a zero at the end: the deduplication function uses it
+        // to detect the beginning of the next string.
+        return _content.asRaw.size + 1;
       case VALUE_IS_OBJECT:
       case VALUE_IS_ARRAY:
         return _content.asCollection.memoryUsage();