Skip to content

Commit

Permalink
Fixed error when the key of a JsonObject is a char[] and reduced …
Browse files Browse the repository at this point in the history
…code size when using const references (issue #423)
  • Loading branch information
bblanchon committed Jan 22, 2017
1 parent cc8c047 commit a096098
Show file tree
Hide file tree
Showing 5 changed files with 25 additions and 9 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
------
Expand Down
4 changes: 1 addition & 3 deletions include/ArduinoJson/JsonArray.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -49,9 +49,7 @@ class JsonArray : public Internals::JsonPrintable<JsonArray>,
: Internals::List<JsonVariant>(buffer) {}

// Gets the value at the specified index
JsonVariant operator[](size_t index) const {
return get<JsonVariant>(index);
}
const JsonArraySubscript operator[](size_t index) const;

// Gets or sets the value at specified index
JsonArraySubscript operator[](size_t index);
Expand Down
4 changes: 4 additions & 0 deletions include/ArduinoJson/JsonArraySubscript.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -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<JsonArray*>(this), index);
}

template <typename TImplem>
inline JsonArraySubscript JsonVariantBase<TImplem>::operator[](int index) {
return asArray()[index];
Expand Down
18 changes: 12 additions & 6 deletions include/ArduinoJson/JsonObject.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -66,18 +66,24 @@ class JsonObject : public Internals::JsonPrintable<JsonObject>,

// Gets the value associated with the specified key.
//
// JsonVariant operator[](TKey) const;
// const JsonObjectSubscript operator[](TKey) const;
// TKey = const std::string&, const String&
template <typename TString>
const JsonVariant operator[](const TString& key) const {
return get_impl<const TString&, JsonVariant>(key);
typename TypeTraits::EnableIf<
!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*
template <typename TString>
const JsonVariant operator[](const TString* key) const {
return get_impl<const TString*, JsonVariant>(key);
const JsonObjectSubscript<const TString*> operator[](
const TString* key) const {
return JsonObjectSubscript<const TString*>(*const_cast<JsonObject*>(this),
key);
}

// Sets the specified key with the specified value.
Expand Down
6 changes: 6 additions & 0 deletions test/JsonObject_Subscript_Tests.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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]);
}

0 comments on commit a096098

Please sign in to comment.