From a096098c1f07a4ef64dc2e083f9093b2c9f7bb18 Mon Sep 17 00:00:00 2001 From: Benoit Blanchon Date: Sun, 22 Jan 2017 11:10:45 +0100 Subject: [PATCH] Fixed error when the key of a `JsonObject` is a `char[]` and reduced code size when using const references (issue #423) --- CHANGELOG.md | 2 ++ include/ArduinoJson/JsonArray.hpp | 4 +--- include/ArduinoJson/JsonArraySubscript.hpp | 4 ++++ include/ArduinoJson/JsonObject.hpp | 18 ++++++++++++------ test/JsonObject_Subscript_Tests.cpp | 6 ++++++ 5 files changed, 25 insertions(+), 9 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 6749e2656..370be3efc 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -7,6 +7,8 @@ HEAD * Fixed parsing of comments (issue #421) * Fixed ignored `Stream` timeout (issue #422) * Made sure we don't read more that necessary (issue #422) +* Fixed error when the key of a `JsonObject` is a `char[]` (issue #423) +* Reduced code size when using `const` references v5.8.1 ------ diff --git a/include/ArduinoJson/JsonArray.hpp b/include/ArduinoJson/JsonArray.hpp index 949e336e2..88a90c69c 100644 --- a/include/ArduinoJson/JsonArray.hpp +++ b/include/ArduinoJson/JsonArray.hpp @@ -49,9 +49,7 @@ class JsonArray : public Internals::JsonPrintable, : Internals::List(buffer) {} // Gets the value at the specified index - JsonVariant operator[](size_t index) const { - return get(index); - } + const JsonArraySubscript operator[](size_t index) const; // Gets or sets the value at specified index JsonArraySubscript operator[](size_t index); diff --git a/include/ArduinoJson/JsonArraySubscript.hpp b/include/ArduinoJson/JsonArraySubscript.hpp index 4bb38f155..d2aa1ac05 100644 --- a/include/ArduinoJson/JsonArraySubscript.hpp +++ b/include/ArduinoJson/JsonArraySubscript.hpp @@ -101,6 +101,10 @@ inline JsonArraySubscript JsonArray::operator[](size_t index) { return JsonArraySubscript(*this, index); } +inline const JsonArraySubscript JsonArray::operator[](size_t index) const { + return JsonArraySubscript(*const_cast(this), index); +} + template inline JsonArraySubscript JsonVariantBase::operator[](int index) { return asArray()[index]; diff --git a/include/ArduinoJson/JsonObject.hpp b/include/ArduinoJson/JsonObject.hpp index 47202a1df..0a4c80aac 100644 --- a/include/ArduinoJson/JsonObject.hpp +++ b/include/ArduinoJson/JsonObject.hpp @@ -66,18 +66,24 @@ class JsonObject : public Internals::JsonPrintable, // Gets the value associated with the specified key. // - // JsonVariant operator[](TKey) const; + // const JsonObjectSubscript operator[](TKey) const; // TKey = const std::string&, const String& template - const JsonVariant operator[](const TString& key) const { - return get_impl(key); + typename TypeTraits::EnableIf< + !TypeTraits::IsArray::value, + const JsonObjectSubscript >::type + operator[](const TString& key) const { + return JsonObjectSubscript(*const_cast(this), + key); } // - // JsonVariant operator[](TKey) const; + // const JsonObjectSubscript operator[](TKey) const; // TKey = const char*, const char[N], const FlashStringHelper* template - const JsonVariant operator[](const TString* key) const { - return get_impl(key); + const JsonObjectSubscript operator[]( + const TString* key) const { + return JsonObjectSubscript(*const_cast(this), + key); } // Sets the specified key with the specified value. diff --git a/test/JsonObject_Subscript_Tests.cpp b/test/JsonObject_Subscript_Tests.cpp index 6cadd3f0d..c829afca8 100644 --- a/test/JsonObject_Subscript_Tests.cpp +++ b/test/JsonObject_Subscript_Tests.cpp @@ -131,3 +131,9 @@ TEST_(StoreObjectSubscript) { EXPECT_EQ(42, _object["a"]); } + +TEST_(KeyAsCharArray) { // issue #423 + char key[] = "hello"; + _object[key] = 42; + EXPECT_EQ(42, _object[key]); +}