Fixed error when the key of a JsonObject is a char[] and reduced code size when using const references (issue #423)

This commit is contained in:
Benoit Blanchon
2017-01-22 11:10:45 +01:00
parent cc8c0472ca
commit a096098c1f
5 changed files with 25 additions and 9 deletions

View File

@ -7,6 +7,8 @@ HEAD
* Fixed parsing of comments (issue #421) * Fixed parsing of comments (issue #421)
* Fixed ignored `Stream` timeout (issue #422) * Fixed ignored `Stream` timeout (issue #422)
* Made sure we don't read more that necessary (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 v5.8.1
------ ------

View File

@ -49,9 +49,7 @@ class JsonArray : public Internals::JsonPrintable<JsonArray>,
: Internals::List<JsonVariant>(buffer) {} : Internals::List<JsonVariant>(buffer) {}
// Gets the value at the specified index // Gets the value at the specified index
JsonVariant operator[](size_t index) const { const JsonArraySubscript operator[](size_t index) const;
return get<JsonVariant>(index);
}
// Gets or sets the value at specified index // Gets or sets the value at specified index
JsonArraySubscript operator[](size_t index); JsonArraySubscript operator[](size_t index);

View File

@ -101,6 +101,10 @@ inline JsonArraySubscript JsonArray::operator[](size_t index) {
return JsonArraySubscript(*this, index); return JsonArraySubscript(*this, index);
} }
inline const JsonArraySubscript JsonArray::operator[](size_t index) const {
return JsonArraySubscript(*const_cast<JsonArray*>(this), index);
}
template <typename TImplem> template <typename TImplem>
inline JsonArraySubscript JsonVariantBase<TImplem>::operator[](int index) { inline JsonArraySubscript JsonVariantBase<TImplem>::operator[](int index) {
return asArray()[index]; return asArray()[index];

View File

@ -66,18 +66,24 @@ class JsonObject : public Internals::JsonPrintable<JsonObject>,
// Gets the value associated with the specified key. // Gets the value associated with the specified key.
// //
// JsonVariant operator[](TKey) const; // const JsonObjectSubscript operator[](TKey) const;
// TKey = const std::string&, const String& // TKey = const std::string&, const String&
template <typename TString> template <typename TString>
const JsonVariant operator[](const TString& key) const { typename TypeTraits::EnableIf<
return get_impl<const TString&, JsonVariant>(key); !TypeTraits::IsArray<TString>::value,
const JsonObjectSubscript<const TString&> >::type
operator[](const TString& key) const {
return JsonObjectSubscript<const TString&>(*const_cast<JsonObject*>(this),
key);
} }
// //
// JsonVariant operator[](TKey) const; // const JsonObjectSubscript operator[](TKey) const;
// TKey = const char*, const char[N], const FlashStringHelper* // TKey = const char*, const char[N], const FlashStringHelper*
template <typename TString> template <typename TString>
const JsonVariant operator[](const TString* key) const { const JsonObjectSubscript<const TString*> operator[](
return get_impl<const TString*, JsonVariant>(key); const TString* key) const {
return JsonObjectSubscript<const TString*>(*const_cast<JsonObject*>(this),
key);
} }
// Sets the specified key with the specified value. // Sets the specified key with the specified value.

View File

@ -131,3 +131,9 @@ TEST_(StoreObjectSubscript) {
EXPECT_EQ(42, _object["a"]); EXPECT_EQ(42, _object["a"]);
} }
TEST_(KeyAsCharArray) { // issue #423
char key[] = "hello";
_object[key] = 42;
EXPECT_EQ(42, _object[key]);
}