diff --git a/JsonGenerator/JsonObjectBase.cpp b/JsonGenerator/JsonObjectBase.cpp index 0fdc23e3..553b1bf1 100644 --- a/JsonGenerator/JsonObjectBase.cpp +++ b/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; } \ No newline at end of file diff --git a/JsonGenerator/JsonObjectBase.h b/JsonGenerator/JsonObjectBase.h index bf0328b1..de6d6c86 100644 --- a/JsonGenerator/JsonObjectBase.h +++ b/JsonGenerator/JsonObjectBase.h @@ -19,19 +19,13 @@ namespace ArduinoJson template void add(const char* key, T value) { - KeyValuePair* pair = getMatchingPair(key); - if (!pair) return; - - pair->value.set(value); + getValue(key).set(value); } template void add(const char* key, double value) { - KeyValuePair* pair = getMatchingPair(key); - if (!pair) return; - - pair->value.set(value); + getValue(key).set(value); } using JsonPrintable::printTo; @@ -51,7 +45,7 @@ namespace ArduinoJson { } - KeyValuePair* getMatchingPair(const char* key); + Internals::JsonValue& getValue(const char* key); private: KeyValuePair* items; diff --git a/JsonGenerator/JsonValue.cpp b/JsonGenerator/JsonValue.cpp index 34850b21..f039abb2 100644 --- a/JsonGenerator/JsonValue.cpp +++ b/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"); diff --git a/JsonGenerator/JsonValue.h b/JsonGenerator/JsonValue.h index 51be76b4..3f24774a 100644 --- a/JsonGenerator/JsonValue.h +++ b/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; }; } } \ No newline at end of file