diff --git a/extras/tests/JsonVariant/CMakeLists.txt b/extras/tests/JsonVariant/CMakeLists.txt index 134c6751..5294295c 100644 --- a/extras/tests/JsonVariant/CMakeLists.txt +++ b/extras/tests/JsonVariant/CMakeLists.txt @@ -8,11 +8,12 @@ add_executable(JsonVariantTests clear.cpp compare.cpp containsKey.cpp - copy.cpp converters.cpp + copy.cpp createNested.cpp is.cpp isnull.cpp + link.cpp memoryUsage.cpp misc.cpp nesting.cpp diff --git a/extras/tests/JsonVariant/link.cpp b/extras/tests/JsonVariant/link.cpp new file mode 100644 index 00000000..2cf6ec5c --- /dev/null +++ b/extras/tests/JsonVariant/link.cpp @@ -0,0 +1,25 @@ +// ArduinoJson - https://arduinojson.org +// Copyright © 2014-2022, Benoit BLANCHON +// MIT License + +#include +#include + +TEST_CASE("JsonVariant::link()") { + StaticJsonDocument<1024> doc1, doc2; + JsonVariant variant = doc1.to(); + + SECTION("JsonVariant::link(JsonDocument&)") { + doc2["hello"] = "world"; + + variant.link(doc2.as()); + + CHECK(variant.as() == "{\"hello\":\"world\"}"); + CHECK(variant.memoryUsage() == 0); + + // altering the linked document should change the result + doc2["hello"] = "WORLD!"; + + CHECK(variant.as() == "{\"hello\":\"WORLD!\"}"); + } +} diff --git a/extras/tests/JsonVariant/memoryUsage.cpp b/extras/tests/JsonVariant/memoryUsage.cpp index 0516bc6d..f8d47ddf 100644 --- a/extras/tests/JsonVariant/memoryUsage.cpp +++ b/extras/tests/JsonVariant/memoryUsage.cpp @@ -42,8 +42,8 @@ TEST_CASE("JsonVariant::memoryUsage()") { SECTION("ignore size of link document") { StaticJsonDocument<128> doc2; doc2["hello"] = "world"; - var.add(&doc2); - CHECK(var.memoryUsage() == JSON_ARRAY_SIZE(1)); + var.link(doc2.as()); + CHECK(var.memoryUsage() == 0); CHECK(var.memoryUsage() == doc.memoryUsage()); } } diff --git a/extras/tests/JsonVariant/set.cpp b/extras/tests/JsonVariant/set.cpp index 8f27d0e0..9cd135ee 100644 --- a/extras/tests/JsonVariant/set.cpp +++ b/extras/tests/JsonVariant/set.cpp @@ -172,18 +172,4 @@ TEST_CASE("Copy/link from other document") { CHECK(variant.as() == "{\"hello\":\"world\"}"); } - - SECTION("JsonVariant::set(JsonDocument*)") { - doc2["hello"] = "world"; - - variant.set(&doc2); - - CHECK(variant.as() == "{\"hello\":\"world\"}"); - CHECK(variant.memoryUsage() == 0); - - // altering the links document should change the result - doc2["hello"] = "WORLD!"; - - CHECK(variant.as() == "{\"hello\":\"WORLD!\"}"); - } } diff --git a/src/ArduinoJson/Variant/ConverterImpl.hpp b/src/ArduinoJson/Variant/ConverterImpl.hpp index b6786b51..baf1187a 100644 --- a/src/ArduinoJson/Variant/ConverterImpl.hpp +++ b/src/ArduinoJson/Variant/ConverterImpl.hpp @@ -303,9 +303,4 @@ inline bool canConvertFromJson(VariantConstRef src, const std::string_view&) { #endif -inline void convertToJson(JsonDocument* src, VariantRef dst) { - VariantData* data = getData(dst); - data->setPointer(getData(src->as())); -} - } // namespace ARDUINOJSON_NAMESPACE diff --git a/src/ArduinoJson/Variant/VariantRef.hpp b/src/ArduinoJson/Variant/VariantRef.hpp index 31fd0219..41b53fbb 100644 --- a/src/ArduinoJson/Variant/VariantRef.hpp +++ b/src/ArduinoJson/Variant/VariantRef.hpp @@ -198,6 +198,11 @@ class VariantRef : public VariantRefBase, template FORCE_INLINE VariantRef getOrAddMember(const TString &) const; + FORCE_INLINE void link(VariantRef var) { + if (_data) + _data->setPointer(var._data); + } + FORCE_INLINE void remove(size_t index) const { if (_data) _data->remove(index);