mirror of
				https://github.com/eledio-devices/thirdparty-ArduinoJson.git
				synced 2025-10-31 16:14:11 +01:00 
			
		
		
		
	Return a JsonValue& instead of a KeyValuePair* (+40 bytes)
This commit is contained in:
		| @@ -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; | ||||
| } | ||||
| @@ -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; | ||||
|   | ||||
| @@ -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"); | ||||
|   | ||||
| @@ -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; | ||||
|         }; | ||||
|     } | ||||
| } | ||||
		Reference in New Issue
	
	Block a user