Change link() to shallowCopy() (issue #1343)

Instead of storing a pointer, the function copies the `VariantData`.

Benefits:
* smaller code
* no impact on programs that don't use this feature

Drawbacks:
* changes to the original variant are not always reflected on the copy
* modifying the original from the shallow copy leads to UB
This commit is contained in:
Benoit Blanchon
2022-07-05 17:07:43 +02:00
parent 3b3ab8c4e1
commit cd8373ad32
36 changed files with 78 additions and 543 deletions

View File

@@ -246,10 +246,10 @@ TEST_CASE("ElementProxy cast to JsonVariant") {
CHECK(doc.as<std::string>() == "[\"toto\"]");
}
TEST_CASE("ElementProxy::link()") {
TEST_CASE("ElementProxy::shallowCopy()") {
StaticJsonDocument<1024> doc1, doc2;
doc1[0].link(doc2);
doc2["hello"] = "world";
doc1[0].shallowCopy(doc2);
CHECK(doc1.as<std::string>() == "[{\"hello\":\"world\"}]");
}

View File

@@ -318,10 +318,10 @@ TEST_CASE("MemberProxy::createNestedObject(key)") {
CHECK(doc["status"]["weather"]["temp"] == 42);
}
TEST_CASE("MemberProxy::link()") {
TEST_CASE("MemberProxy::shallowCopy()") {
StaticJsonDocument<1024> doc1, doc2;
doc1["obj"].link(doc2);
doc2["hello"] = "world";
doc1["obj"].shallowCopy(doc2);
CHECK(doc1.as<std::string>() == "{\"obj\":{\"hello\":\"world\"}}");
}

View File

@@ -25,21 +25,4 @@ TEST_CASE("JsonDocument::size()") {
REQUIRE(doc.size() == 2);
}
SECTION("linked array") {
StaticJsonDocument<128> doc2;
doc2.add(1);
doc2.add(2);
doc.as<JsonVariant>().link(doc2);
REQUIRE(doc.size() == 2);
}
SECTION("linked object") {
StaticJsonDocument<128> doc2;
doc2["hello"] = "world";
doc.as<JsonVariant>().link(doc2);
REQUIRE(doc.size() == 1);
}
}