mirror of
https://github.com/eledio-devices/thirdparty-ArduinoJson.git
synced 2025-11-01 08:48:30 +01:00
Replace with JsonVariant::link()
This commit is contained in:
@@ -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
|
||||
|
||||
25
extras/tests/JsonVariant/link.cpp
Normal file
25
extras/tests/JsonVariant/link.cpp
Normal file
@@ -0,0 +1,25 @@
|
||||
// ArduinoJson - https://arduinojson.org
|
||||
// Copyright © 2014-2022, Benoit BLANCHON
|
||||
// MIT License
|
||||
|
||||
#include <ArduinoJson.h>
|
||||
#include <catch.hpp>
|
||||
|
||||
TEST_CASE("JsonVariant::link()") {
|
||||
StaticJsonDocument<1024> doc1, doc2;
|
||||
JsonVariant variant = doc1.to<JsonVariant>();
|
||||
|
||||
SECTION("JsonVariant::link(JsonDocument&)") {
|
||||
doc2["hello"] = "world";
|
||||
|
||||
variant.link(doc2.as<JsonVariant>());
|
||||
|
||||
CHECK(variant.as<std::string>() == "{\"hello\":\"world\"}");
|
||||
CHECK(variant.memoryUsage() == 0);
|
||||
|
||||
// altering the linked document should change the result
|
||||
doc2["hello"] = "WORLD!";
|
||||
|
||||
CHECK(variant.as<std::string>() == "{\"hello\":\"WORLD!\"}");
|
||||
}
|
||||
}
|
||||
@@ -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<JsonVariant>());
|
||||
CHECK(var.memoryUsage() == 0);
|
||||
CHECK(var.memoryUsage() == doc.memoryUsage());
|
||||
}
|
||||
}
|
||||
|
||||
@@ -172,18 +172,4 @@ TEST_CASE("Copy/link from other document") {
|
||||
|
||||
CHECK(variant.as<std::string>() == "{\"hello\":\"world\"}");
|
||||
}
|
||||
|
||||
SECTION("JsonVariant::set(JsonDocument*)") {
|
||||
doc2["hello"] = "world";
|
||||
|
||||
variant.set(&doc2);
|
||||
|
||||
CHECK(variant.as<std::string>() == "{\"hello\":\"world\"}");
|
||||
CHECK(variant.memoryUsage() == 0);
|
||||
|
||||
// altering the links document should change the result
|
||||
doc2["hello"] = "WORLD!";
|
||||
|
||||
CHECK(variant.as<std::string>() == "{\"hello\":\"WORLD!\"}");
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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<VariantRef>()));
|
||||
}
|
||||
|
||||
} // namespace ARDUINOJSON_NAMESPACE
|
||||
|
||||
@@ -198,6 +198,11 @@ class VariantRef : public VariantRefBase<VariantData>,
|
||||
template <typename TString>
|
||||
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);
|
||||
|
||||
Reference in New Issue
Block a user