Browse Source

Added typedef JsonKey

Benoit Blanchon 11 năm trước cách đây
mục cha
commit
817cc09975
2 tập tin đã thay đổi với 13 bổ sung11 xóa
  1. 5 4
      JsonGenerator/JsonObjectBase.cpp
  2. 8 7
      JsonGenerator/JsonObjectBase.h

+ 5 - 4
JsonGenerator/JsonObjectBase.cpp

@@ -39,7 +39,7 @@ size_t JsonObjectBase::printTo(Print& p) const
     return n;
 }
 
-JsonObjectBase::KeyValuePair* JsonObjectBase::getMatchingPair(char const* key) const
+JsonObjectBase::KeyValuePair* JsonObjectBase::getMatchingPair(JsonKey key) const
 {
     KeyValuePair* p = items;
 
@@ -54,14 +54,15 @@ JsonObjectBase::KeyValuePair* JsonObjectBase::getMatchingPair(char const* key) c
     return 0;
 }
 
-JsonValue& JsonObjectBase::operator[](char const* key)
+JsonValue& JsonObjectBase::operator[](JsonKey key)
 {
     KeyValuePair* match = getMatchingPair(key);
-    JsonValue* value;
 
     if (match)
         return match->value;
 
+    JsonValue* value;
+
     if (count < capacity)
     {
         items[count].key = key;
@@ -77,7 +78,7 @@ JsonValue& JsonObjectBase::operator[](char const* key)
     return *value;
 }
 
-bool JsonObjectBase::containsKey(char const* key) const
+bool JsonObjectBase::containsKey(JsonKey key) const
 {
     return getMatchingPair(key) != 0;
 }

+ 8 - 7
JsonGenerator/JsonObjectBase.h

@@ -12,22 +12,23 @@ namespace ArduinoJson
 {
     namespace Generator
     {
+        typedef const char* JsonKey;
+
         class JsonObjectBase : public JsonPrintable
         {
         public:
+            JsonValue& operator[](JsonKey);
 
-            JsonValue& operator[](const char*);
-
-            bool containsKey(const char*) const;
+            bool containsKey(JsonKey) const;
 
             template<typename T>
-            void add(const char* key, T value)
+            void add(JsonKey key, T value)
             {
                 operator[](key) = value;
             }
 
             template<int DIGITS>
-            void add(const char* key, double value)
+            void add(JsonKey key, double value)
             {
                 operator[](key).set<DIGITS>(value);
             }
@@ -40,7 +41,7 @@ namespace ArduinoJson
 
             struct KeyValuePair
             {
-                const char* key;
+                JsonKey     key;
                 JsonValue   value;
             };
 
@@ -55,7 +56,7 @@ namespace ArduinoJson
 
             static JsonValue nullValue;
 
-            KeyValuePair* getMatchingPair(const char* key) const;
+            KeyValuePair* getMatchingPair(JsonKey key) const;
         };
     }
 }