diff --git a/CHANGELOG.md b/CHANGELOG.md index 6276029c..526c10e5 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,6 +1,12 @@ ArduinoJson: change log ======================= +HEAD +---- + +* Fixed `invalid application of 'sizeof' to incomplete type '__FlashStringHelper'` (issue #783) +* Fixed `char[]` not duplicated when passed to `JsonVariant::operator[]` + v6.2.1-beta ----------- diff --git a/src/ArduinoJson/JsonObjectSubscript.hpp b/src/ArduinoJson/JsonObjectSubscript.hpp index e9d8c0dc..fba6e6d4 100644 --- a/src/ArduinoJson/JsonObjectSubscript.hpp +++ b/src/ArduinoJson/JsonObjectSubscript.hpp @@ -111,17 +111,17 @@ inline typename enable_if::value, template template -inline typename enable_if::value, - JsonObjectSubscript >::type - JsonVariantSubscripts::operator[](const TString *key) { +inline typename enable_if::value, + JsonObjectSubscript >::type + JsonVariantSubscripts::operator[](TString *key) { return impl()->template as()[key]; } template template inline typename enable_if::value, - const JsonObjectSubscript >::type - JsonVariantSubscripts::operator[](const TString *key) const { + const JsonObjectSubscript >::type + JsonVariantSubscripts::operator[](TString *key) const { return impl()->template as()[key]; } diff --git a/src/ArduinoJson/JsonVariantSubscripts.hpp b/src/ArduinoJson/JsonVariantSubscripts.hpp index d2aa3aa5..3b5a5eb1 100644 --- a/src/ArduinoJson/JsonVariantSubscripts.hpp +++ b/src/ArduinoJson/JsonVariantSubscripts.hpp @@ -57,17 +57,16 @@ class JsonVariantSubscripts { // JsonObjectSubscript operator[](TKey); // TKey = const char*, const char[N], const FlashStringHelper* template - FORCE_INLINE typename enable_if::value, - JsonObjectSubscript >::type - operator[](const TString *key); + FORCE_INLINE typename enable_if::value, + JsonObjectSubscript >::type + operator[](TString *key); // // JsonObjectSubscript operator[](TKey); // TKey = const char*, const char[N], const FlashStringHelper* template - FORCE_INLINE - typename enable_if::value, - const JsonObjectSubscript >::type - operator[](const TString *key) const; + FORCE_INLINE typename enable_if::value, + const JsonObjectSubscript >::type + operator[](TString *key) const; private: const TImpl *impl() const { diff --git a/test/JsonVariant/subscript.cpp b/test/JsonVariant/subscript.cpp index c2192466..24227252 100644 --- a/test/JsonVariant/subscript.cpp +++ b/test/JsonVariant/subscript.cpp @@ -6,79 +6,89 @@ #include TEST_CASE("JsonVariant::operator[]") { - SECTION("Array") { - DynamicJsonDocument doc; - JsonArray array = doc.to(); - array.add("element at index 0"); - array.add("element at index 1"); - - JsonVariant var = array; - - REQUIRE(2 == var.size()); - REQUIRE(std::string("element at index 0") == var[0]); - REQUIRE(std::string("element at index 1") == var[1]); - REQUIRE(std::string("element at index 0") == - var[static_cast(0)]); // issue #381 - REQUIRE(var[666].isNull()); - REQUIRE(var[3].isNull()); - REQUIRE(var["0"].isNull()); - } - - SECTION("Object") { - DynamicJsonDocument doc; - JsonObject object = doc.to(); - object["a"] = "element at key \"a\""; - object["b"] = "element at key \"b\""; - - JsonVariant var = object; - - REQUIRE(2 == var.size()); - REQUIRE(std::string("element at key \"a\"") == var["a"]); - REQUIRE(std::string("element at key \"b\"") == var["b"]); - REQUIRE(var["c"].isNull()); - REQUIRE(var[0].isNull()); - } - - SECTION("Undefined") { + SECTION("The JsonVariant is undefined") { JsonVariant var = JsonVariant(); REQUIRE(0 == var.size()); REQUIRE(var["0"].isNull()); REQUIRE(var[0].isNull()); } - SECTION("String") { + SECTION("The JsonVariant is a string") { JsonVariant var = "hello world"; REQUIRE(0 == var.size()); REQUIRE(var["0"].isNull()); REQUIRE(var[0].isNull()); } - SECTION("ObjectSetValue") { + SECTION("The JsonVariant is a JsonArray") { DynamicJsonDocument doc; - JsonObject obj = doc.to(); - JsonVariant var = obj; - var["hello"] = "world"; - REQUIRE(1 == var.size()); - REQUIRE(std::string("world") == var["hello"]); + JsonArray array = doc.to(); + JsonVariant var = array; + + SECTION("get value") { + array.add("element at index 0"); + array.add("element at index 1"); + + REQUIRE(2 == var.size()); + REQUIRE(std::string("element at index 0") == var[0]); + REQUIRE(std::string("element at index 1") == var[1]); + REQUIRE(std::string("element at index 0") == + var[static_cast(0)]); // issue #381 + REQUIRE(var[666].isNull()); + REQUIRE(var[3].isNull()); + REQUIRE(var["0"].isNull()); + } + + SECTION("set value") { + array.add("hello"); + + var[0] = "world"; + + REQUIRE(1 == var.size()); + REQUIRE(std::string("world") == var[0]); + } + + SECTION("set value in a nested object") { + array.createNestedObject(); + + var[0]["hello"] = "world"; + + REQUIRE(1 == var.size()); + REQUIRE(1 == var[0].size()); + REQUIRE(std::string("world") == var[0]["hello"]); + } } - SECTION("ArraySetValue") { + SECTION("The JsonVariant is a JsonObject") { DynamicJsonDocument doc; - JsonArray arr = doc.to(); - arr.add("hello"); - JsonVariant var = arr; - var[0] = "world"; - REQUIRE(1 == var.size()); - REQUIRE(std::string("world") == var[0]); - } + JsonObject object = doc.to(); + JsonVariant var = object; - SECTION("NestedObjectSetValue") { - DynamicJsonDocument doc; - deserializeJson(doc, "[{}]"); - JsonVariant var = doc.as(); - var[0]["hello"] = "world"; - REQUIRE(1 == var.size()); - REQUIRE(1 == var[0].size()); - REQUIRE(std::string("world") == var[0]["hello"]); + SECTION("get value") { + object["a"] = "element at key \"a\""; + object["b"] = "element at key \"b\""; + + REQUIRE(2 == var.size()); + REQUIRE(std::string("element at key \"a\"") == var["a"]); + REQUIRE(std::string("element at key \"b\"") == var["b"]); + REQUIRE(var["c"].isNull()); + REQUIRE(var[0].isNull()); + } + + SECTION("set value, key is a const char*") { + var["hello"] = "world"; + + REQUIRE(1 == var.size()); + REQUIRE(std::string("world") == var["hello"]); + } + + SECTION("set value, key is a char[]") { + char key[] = "hello"; + var[key] = "world"; + key[0] = '!'; // make sure the key is duplicated + + REQUIRE(1 == var.size()); + REQUIRE(std::string("world") == var["hello"]); + } } }