Skip to content

Commit

Permalink
Duplicate key String in JsonObjectSubscript
Browse files Browse the repository at this point in the history
  • Loading branch information
bblanchon committed Jul 23, 2015
1 parent a251f45 commit 4251c29
Show file tree
Hide file tree
Showing 5 changed files with 49 additions and 25 deletions.
7 changes: 4 additions & 3 deletions include/ArduinoJson/JsonObject.hpp
Expand Up @@ -23,7 +23,6 @@ namespace ArduinoJson {
// Forward declarations
class JsonArray;
class JsonBuffer;
class JsonObjectSubscript;

// A dictionary of JsonVariant indexed by string (char*)
//
Expand All @@ -43,10 +42,12 @@ class JsonObject : public Internals::JsonPrintable<JsonObject>,
: Internals::List<JsonPair>(buffer) {}

// Gets or sets the value associated with the specified key.
FORCE_INLINE JsonObjectSubscript operator[](JsonObjectKey key);
FORCE_INLINE JsonObjectSubscript<const char *> operator[](const char *key);
FORCE_INLINE JsonObjectSubscript<const String &> operator[](
const String &key);

// Gets the value associated with the specified key.
FORCE_INLINE const JsonObjectSubscript operator[](JsonObjectKey key) const;
FORCE_INLINE JsonVariant operator[](JsonObjectKey key) const;

// Sets the specified key with the specified value.
FORCE_INLINE bool set(const char *key, const JsonVariant value);
Expand Down
23 changes: 14 additions & 9 deletions include/ArduinoJson/JsonObject.ipp
Expand Up @@ -28,13 +28,18 @@ inline bool JsonObject::is(JsonObjectKey key) const {
return node ? node->content.value.is<T>() : false;
}

inline JsonObjectSubscript JsonObject::operator[](JsonObjectKey key) {
return JsonObjectSubscript(*this, key);
inline JsonObjectSubscript<const char *> JsonObject::operator[](
const char *key) {
return JsonObjectSubscript<const char *>(*this, key);
}

inline const JsonObjectSubscript JsonObject::operator[](
JsonObjectKey key) const {
return JsonObjectSubscript(*const_cast<JsonObject *>(this), key);
inline JsonObjectSubscript<const String &> JsonObject::operator[](
const String &key) {
return JsonObjectSubscript<const String &>(*this, key);
}

inline JsonVariant JsonObject::operator[](JsonObjectKey key) const {
return get(key);
}

inline bool JsonObject::containsKey(JsonObjectKey key) const {
Expand Down Expand Up @@ -116,14 +121,14 @@ inline void JsonObject::setNodeValue(node_type *node, const String &value) {
}

template <typename TImplem>
inline const JsonObjectSubscript JsonVariantBase<TImplem>::operator[](
const char *key) const {
inline const JsonObjectSubscript<const char *> JsonVariantBase<TImplem>::
operator[](const char *key) const {
return asObject()[key];
}

template <typename TImplem>
inline const JsonObjectSubscript JsonVariantBase<TImplem>::operator[](
const String &key) const {
inline const JsonObjectSubscript<const String &> JsonVariantBase<TImplem>::
operator[](const String &key) const {
return asObject()[key];
}

Expand Down
29 changes: 18 additions & 11 deletions include/ArduinoJson/JsonObjectSubscript.hpp
Expand Up @@ -9,9 +9,10 @@
#include "JsonVariantBase.hpp"

namespace ArduinoJson {
class JsonObjectSubscript : public JsonVariantBase<JsonObjectSubscript> {
template <typename TKey>
class JsonObjectSubscript : public JsonVariantBase<JsonObjectSubscript<TKey> > {
public:
FORCE_INLINE JsonObjectSubscript(JsonObject& object, JsonObjectKey key)
FORCE_INLINE JsonObjectSubscript(JsonObject& object, TKey key)
: _object(object), _key(key) {}

FORCE_INLINE JsonObjectSubscript& operator=(const JsonVariant& value) {
Expand All @@ -31,27 +32,33 @@ class JsonObjectSubscript : public JsonVariantBase<JsonObjectSubscript> {

FORCE_INLINE operator JsonVariant() const { return _object.get(_key); }

template <typename T>
FORCE_INLINE T as() const {
return _object.get<T>(_key);
template <typename TValue>
FORCE_INLINE TValue as() const {
return _object.get<TValue>(_key);
}

template <typename T>
FORCE_INLINE T is() const {
return _object.is<T>(_key);
template <typename TValue>
FORCE_INLINE TValue is() const {
return _object.is<TValue>(_key);
}

void writeTo(Internals::JsonWriter &writer) const {
void writeTo(Internals::JsonWriter& writer) const {
_object.get(_key).writeTo(writer);
}

private:
JsonObject& _object;
JsonObjectKey _key;
TKey _key;
};

#ifdef ARDUINOJSON_ENABLE_STD_STREAM
inline std::ostream& operator<<(std::ostream& os, const JsonObjectSubscript& source) {
inline std::ostream& operator<<(
std::ostream& os, const JsonObjectSubscript<const String&>& source) {
return source.printTo(os);
}

inline std::ostream& operator<<(
std::ostream& os, const JsonObjectSubscript<const char*>& source) {
return source.printTo(os);
}
#endif
Expand Down
7 changes: 5 additions & 2 deletions include/ArduinoJson/JsonVariantBase.hpp
Expand Up @@ -13,6 +13,7 @@ namespace ArduinoJson {

// Forward declarations.
class JsonArraySubscript;
template <typename TKey>
class JsonObjectSubscript;

template <typename TImpl>
Expand Down Expand Up @@ -76,8 +77,10 @@ class JsonVariantBase : public Internals::JsonPrintable<TImpl> {
// Returns the value associated with the specified key if the variant is
// an object.
// Return JsonVariant::invalid() if the variant is not an object.
FORCE_INLINE const JsonObjectSubscript operator[](const char *key) const;
FORCE_INLINE const JsonObjectSubscript operator[](const String &key) const;
FORCE_INLINE const JsonObjectSubscript<const char *> operator[](
const char *key) const;
FORCE_INLINE const JsonObjectSubscript<const String &> operator[](
const String &key) const;

// Serialize the variant to a JsonWriter
void writeTo(Internals::JsonWriter &writer) const;
Expand Down
8 changes: 8 additions & 0 deletions test/ArduinoString_Tests.cpp
Expand Up @@ -130,3 +130,11 @@ TEST_F(ArduinoStringTests, JsonObject_Remove) {
object.remove(String("key"));
ASSERT_EQ(0, object.size());
}

TEST_F(ArduinoStringTests, JsonObjectSubscript_SetKey) {
JsonObject &object = _jsonBuffer.createObject();
String key("hello");
object[key] = "world";
eraseString(key);
ASSERT_STREQ("world", object["hello"]);
}

0 comments on commit 4251c29

Please sign in to comment.