From 60c6f2db472494582977e6a076a398ea9ff686eb Mon Sep 17 00:00:00 2001 From: Benoit Blanchon Date: Thu, 31 Jul 2014 20:11:55 +0200 Subject: [PATCH] Added operator[] --- JsonGenerator/JsonObjectBase.cpp | 2 +- JsonGenerator/JsonObjectBase.h | 8 ++++---- JsonGeneratorTests/JsonHashTableTests.cpp | 7 +++---- 3 files changed, 8 insertions(+), 9 deletions(-) diff --git a/JsonGenerator/JsonObjectBase.cpp b/JsonGenerator/JsonObjectBase.cpp index 553b1bf1..dc708ae7 100644 --- a/JsonGenerator/JsonObjectBase.cpp +++ b/JsonGenerator/JsonObjectBase.cpp @@ -36,7 +36,7 @@ size_t JsonObjectBase::printTo(Print& p) const return n; } -JsonValue& JsonObjectBase::getValue(char const* key) +JsonValue& JsonObjectBase::operator[](char const* key) { for (int i = 0; i < count; ++i) { diff --git a/JsonGenerator/JsonObjectBase.h b/JsonGenerator/JsonObjectBase.h index e76ba7c3..e8e13188 100644 --- a/JsonGenerator/JsonObjectBase.h +++ b/JsonGenerator/JsonObjectBase.h @@ -16,16 +16,18 @@ namespace ArduinoJson { public: + JsonValue& operator[](const char*); + template void add(const char* key, T value) { - getValue(key) = value; + operator[](key) = value; } template void add(const char* key, double value) { - getValue(key).set(value); + operator[](key).set(value); } using JsonPrintable::printTo; @@ -45,8 +47,6 @@ namespace ArduinoJson { } - JsonValue& getValue(const char* key); - private: KeyValuePair* items; int capacity, count; diff --git a/JsonGeneratorTests/JsonHashTableTests.cpp b/JsonGeneratorTests/JsonHashTableTests.cpp index 5b6f0a63..61646828 100644 --- a/JsonGeneratorTests/JsonHashTableTests.cpp +++ b/JsonGeneratorTests/JsonHashTableTests.cpp @@ -34,7 +34,6 @@ namespace JsonGeneratorTests { add("key1", "value1"); add("key2", "value2"); - outputMustBe("{\"key1\":\"value1\",\"key2\":\"value2\"}"); } @@ -106,19 +105,19 @@ namespace JsonGeneratorTests void addNested(const char* key, Printable& value) { - hash.add(key, value); + hash[key] = value; } template void add(const char* key, T value) { - hash.add(key, value); + hash[key] = value; } template void add(const char* key, double value) { - hash.add(key, value); + hash[key].set(value); } void outputMustBe(const char* expected)