Procházet zdrojové kódy

Return a JsonValue& instead of a KeyValuePair* (+40 bytes)

Benoit Blanchon před 11 roky
rodič
revize
2c29327ebd

+ 7 - 6
JsonGenerator/JsonObjectBase.cpp

@@ -6,6 +6,7 @@
 #include "JsonObjectBase.h"
 
 using namespace ArduinoJson::Generator;
+using namespace ArduinoJson::Internals;
 
 size_t JsonObjectBase::printTo(Print& p) const
 {
@@ -35,19 +36,19 @@ size_t JsonObjectBase::printTo(Print& p) const
     return n;
 }
 
-JsonObjectBase::KeyValuePair* JsonObjectBase::getMatchingPair(char const* key)
+JsonValue& JsonObjectBase::getValue(char const* key)
 {
     for (int i = 0; i < count; ++i)
     {
         if (items[i].key.equals(key))
         {
-            return &items[i];
+            return items[i].value;
         }
     }
 
-    if (count >= capacity) return 0;
+    if (count >= capacity)        
+        return JsonValue::null();
     
-    KeyValuePair* p = &items[count++];
-    p->key.set(key);
-    return p;
+    items[count].key.set(key);
+    return items[count++].value;
 }

+ 3 - 9
JsonGenerator/JsonObjectBase.h

@@ -19,19 +19,13 @@ namespace ArduinoJson
             template<typename T>
             void add(const char* key, T value)
             {
-                KeyValuePair* pair = getMatchingPair(key);
-                if (!pair) return;
-
-                pair->value.set(value);
+                getValue(key).set(value);
             }
 
             template<int DIGITS>
             void add(const char* key, double value)
             {
-                KeyValuePair* pair = getMatchingPair(key);
-                if (!pair) return;
-
-                pair->value.set<DIGITS>(value);
+                getValue(key).set<DIGITS>(value);
             }           
 
             using JsonPrintable::printTo;
@@ -51,7 +45,7 @@ namespace ArduinoJson
             {
             }
 
-            KeyValuePair* getMatchingPair(const char* key);
+            Internals::JsonValue& getValue(const char* key);
 
         private:
             KeyValuePair* items;

+ 2 - 0
JsonGenerator/JsonValue.cpp

@@ -8,6 +8,8 @@
 
 using namespace ArduinoJson::Internals;
 
+JsonValue JsonValue::nullInstance;
+
 size_t JsonValue::printBoolTo(const Content& c, Print& p)
 {
     return p.print(c.asBool ? "true" : "false");

+ 7 - 0
JsonGenerator/JsonValue.h

@@ -65,6 +65,11 @@ namespace ArduinoJson
                 return printToImpl(content, p);
             }
 
+            static JsonValue& null()
+            {
+                return nullInstance;
+            }
+
         private:
             union Content
             {
@@ -89,6 +94,8 @@ namespace ArduinoJson
             {
                 return p.print(c.asDouble, DIGITS);
             }
+
+            static JsonValue nullInstance;
         };
     }
 }