Przeglądaj źródła

Added operator[]

Benoit Blanchon 11 lat temu
rodzic
commit
60c6f2db47

+ 1 - 1
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)
     {

+ 4 - 4
JsonGenerator/JsonObjectBase.h

@@ -16,16 +16,18 @@ namespace ArduinoJson
         {
         public:
 
+            JsonValue& operator[](const char*);
+
             template<typename T>
             void add(const char* key, T value)
             {
-                getValue(key) = value;
+                operator[](key) = value;
             }
 
             template<int DIGITS>
             void add(const char* key, double value)
             {
-                getValue(key).set<DIGITS>(value);
+                operator[](key).set<DIGITS>(value);
             }
 
             using JsonPrintable::printTo;
@@ -45,8 +47,6 @@ namespace ArduinoJson
             {
             }
 
-            JsonValue& getValue(const char* key);
-
         private:
             KeyValuePair* items;
             int capacity, count;

+ 3 - 4
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<Printable&>(key, value);
+            hash[key] = value;
         }
 
         template<typename T>
         void add(const char* key, T value)
         {
-            hash.add(key, value);
+            hash[key] = value;
         }
 
         template<int DIGITS>
         void add(const char* key, double value)
         {
-            hash.add<DIGITS>(key, value);
+            hash[key].set<DIGITS>(value);
         }
 
         void outputMustBe(const char* expected)