mirror of
				https://github.com/eledio-devices/thirdparty-ArduinoJson.git
				synced 2025-10-31 16:14:11 +01:00 
			
		
		
		
	Fixed invalid application of 'sizeof' to incomplete type (closes #783)
This commit is contained in:
		| @@ -1,6 +1,12 @@ | |||||||
| ArduinoJson: change log | 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 | v6.2.1-beta | ||||||
| ----------- | ----------- | ||||||
|  |  | ||||||
|   | |||||||
| @@ -111,17 +111,17 @@ inline typename enable_if<IsString<TString>::value, | |||||||
|  |  | ||||||
| template <typename TImpl> | template <typename TImpl> | ||||||
| template <typename TString> | template <typename TString> | ||||||
| inline typename enable_if<IsString<const TString *>::value, | inline typename enable_if<IsString<TString *>::value, | ||||||
|                           JsonObjectSubscript<const TString *> >::type |                           JsonObjectSubscript<TString *> >::type | ||||||
|     JsonVariantSubscripts<TImpl>::operator[](const TString *key) { |     JsonVariantSubscripts<TImpl>::operator[](TString *key) { | ||||||
|   return impl()->template as<JsonObject>()[key]; |   return impl()->template as<JsonObject>()[key]; | ||||||
| } | } | ||||||
|  |  | ||||||
| template <typename TImpl> | template <typename TImpl> | ||||||
| template <typename TString> | template <typename TString> | ||||||
| inline typename enable_if<IsString<TString *>::value, | inline typename enable_if<IsString<TString *>::value, | ||||||
|                           const JsonObjectSubscript<const TString *> >::type |                           const JsonObjectSubscript<TString *> >::type | ||||||
|     JsonVariantSubscripts<TImpl>::operator[](const TString *key) const { |     JsonVariantSubscripts<TImpl>::operator[](TString *key) const { | ||||||
|   return impl()->template as<JsonObject>()[key]; |   return impl()->template as<JsonObject>()[key]; | ||||||
| } | } | ||||||
|  |  | ||||||
|   | |||||||
| @@ -57,17 +57,16 @@ class JsonVariantSubscripts { | |||||||
|   // JsonObjectSubscript operator[](TKey); |   // JsonObjectSubscript operator[](TKey); | ||||||
|   // TKey = const char*, const char[N], const FlashStringHelper* |   // TKey = const char*, const char[N], const FlashStringHelper* | ||||||
|   template <typename TString> |   template <typename TString> | ||||||
|   FORCE_INLINE typename enable_if<IsString<const TString *>::value, |   FORCE_INLINE typename enable_if<IsString<TString *>::value, | ||||||
|                                   JsonObjectSubscript<const TString *> >::type |                                   JsonObjectSubscript<TString *> >::type | ||||||
|   operator[](const TString *key); |   operator[](TString *key); | ||||||
|   // |   // | ||||||
|   // JsonObjectSubscript operator[](TKey); |   // JsonObjectSubscript operator[](TKey); | ||||||
|   // TKey = const char*, const char[N], const FlashStringHelper* |   // TKey = const char*, const char[N], const FlashStringHelper* | ||||||
|   template <typename TString> |   template <typename TString> | ||||||
|   FORCE_INLINE |   FORCE_INLINE typename enable_if<IsString<TString *>::value, | ||||||
|       typename enable_if<IsString<TString *>::value, |                                   const JsonObjectSubscript<TString *> >::type | ||||||
|                          const JsonObjectSubscript<const TString *> >::type |   operator[](TString *key) const; | ||||||
|       operator[](const TString *key) const; |  | ||||||
|  |  | ||||||
|  private: |  private: | ||||||
|   const TImpl *impl() const { |   const TImpl *impl() const { | ||||||
|   | |||||||
| @@ -6,79 +6,89 @@ | |||||||
| #include <catch.hpp> | #include <catch.hpp> | ||||||
|  |  | ||||||
| TEST_CASE("JsonVariant::operator[]") { | TEST_CASE("JsonVariant::operator[]") { | ||||||
|   SECTION("Array") { |   SECTION("The JsonVariant is undefined") { | ||||||
|     DynamicJsonDocument doc; |  | ||||||
|     JsonArray array = doc.to<JsonArray>(); |  | ||||||
|     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<unsigned char>(0)]);  // issue #381 |  | ||||||
|     REQUIRE(var[666].isNull()); |  | ||||||
|     REQUIRE(var[3].isNull()); |  | ||||||
|     REQUIRE(var["0"].isNull()); |  | ||||||
|   } |  | ||||||
|  |  | ||||||
|   SECTION("Object") { |  | ||||||
|     DynamicJsonDocument doc; |  | ||||||
|     JsonObject object = doc.to<JsonObject>(); |  | ||||||
|     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") { |  | ||||||
|     JsonVariant var = JsonVariant(); |     JsonVariant var = JsonVariant(); | ||||||
|     REQUIRE(0 == var.size()); |     REQUIRE(0 == var.size()); | ||||||
|     REQUIRE(var["0"].isNull()); |     REQUIRE(var["0"].isNull()); | ||||||
|     REQUIRE(var[0].isNull()); |     REQUIRE(var[0].isNull()); | ||||||
|   } |   } | ||||||
|  |  | ||||||
|   SECTION("String") { |   SECTION("The JsonVariant is a string") { | ||||||
|     JsonVariant var = "hello world"; |     JsonVariant var = "hello world"; | ||||||
|     REQUIRE(0 == var.size()); |     REQUIRE(0 == var.size()); | ||||||
|     REQUIRE(var["0"].isNull()); |     REQUIRE(var["0"].isNull()); | ||||||
|     REQUIRE(var[0].isNull()); |     REQUIRE(var[0].isNull()); | ||||||
|   } |   } | ||||||
|  |  | ||||||
|   SECTION("ObjectSetValue") { |   SECTION("The JsonVariant is a JsonArray") { | ||||||
|     DynamicJsonDocument doc; |     DynamicJsonDocument doc; | ||||||
|     JsonObject obj = doc.to<JsonObject>(); |     JsonArray array = doc.to<JsonArray>(); | ||||||
|     JsonVariant var = obj; |     JsonVariant var = array; | ||||||
|     var["hello"] = "world"; |  | ||||||
|     REQUIRE(1 == var.size()); |     SECTION("get value") { | ||||||
|     REQUIRE(std::string("world") == var["hello"]); |       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<unsigned char>(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; |     DynamicJsonDocument doc; | ||||||
|     JsonArray arr = doc.to<JsonArray>(); |     JsonObject object = doc.to<JsonObject>(); | ||||||
|     arr.add("hello"); |     JsonVariant var = object; | ||||||
|     JsonVariant var = arr; |  | ||||||
|     var[0] = "world"; |  | ||||||
|     REQUIRE(1 == var.size()); |  | ||||||
|     REQUIRE(std::string("world") == var[0]); |  | ||||||
|   } |  | ||||||
|  |  | ||||||
|   SECTION("NestedObjectSetValue") { |     SECTION("get value") { | ||||||
|     DynamicJsonDocument doc; |       object["a"] = "element at key \"a\""; | ||||||
|     deserializeJson(doc, "[{}]"); |       object["b"] = "element at key \"b\""; | ||||||
|     JsonVariant var = doc.as<JsonVariant>(); |  | ||||||
|     var[0]["hello"] = "world"; |       REQUIRE(2 == var.size()); | ||||||
|     REQUIRE(1 == var.size()); |       REQUIRE(std::string("element at key \"a\"") == var["a"]); | ||||||
|     REQUIRE(1 == var[0].size()); |       REQUIRE(std::string("element at key \"b\"") == var["b"]); | ||||||
|     REQUIRE(std::string("world") == var[0]["hello"]); |       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"]); | ||||||
|  |     } | ||||||
|   } |   } | ||||||
| } | } | ||||||
|   | |||||||
		Reference in New Issue
	
	Block a user