diff --git a/JsonGenerator/EscapedString.h b/JsonGenerator/EscapedString.h index 1887db5a..5975d9ad 100644 --- a/JsonGenerator/EscapedString.h +++ b/JsonGenerator/EscapedString.h @@ -6,6 +6,7 @@ #pragma once #include "Print.h" +#include // for strcmp namespace ArduinoJson { @@ -21,6 +22,11 @@ namespace ArduinoJson } size_t printTo(Print&) const; + + bool equals(char const* s) + { + return strcmp(s, rawString) == 0; + } private: const char* rawString; diff --git a/JsonGenerator/JsonObjectBase.cpp b/JsonGenerator/JsonObjectBase.cpp index 9c99e4a6..0fdc23e3 100644 --- a/JsonGenerator/JsonObjectBase.cpp +++ b/JsonGenerator/JsonObjectBase.cpp @@ -33,4 +33,21 @@ size_t JsonObjectBase::printTo(Print& p) const n += p.write('}'); return n; +} + +JsonObjectBase::KeyValuePair* JsonObjectBase::getMatchingPair(char const* key) +{ + for (int i = 0; i < count; ++i) + { + if (items[i].key.equals(key)) + { + return &items[i]; + } + } + + if (count >= capacity) return 0; + + KeyValuePair* p = &items[count++]; + p->key.set(key); + return p; } \ No newline at end of file diff --git a/JsonGenerator/JsonObjectBase.h b/JsonGenerator/JsonObjectBase.h index 89f38da7..bf0328b1 100644 --- a/JsonGenerator/JsonObjectBase.h +++ b/JsonGenerator/JsonObjectBase.h @@ -19,22 +19,20 @@ namespace ArduinoJson template void add(const char* key, T value) { - if (count >= capacity) return; + KeyValuePair* pair = getMatchingPair(key); + if (!pair) return; - items[count].key.set(key); - items[count].value.set(value); - count++; + pair->value.set(value); } template void add(const char* key, double value) { - if (count >= capacity) return; + KeyValuePair* pair = getMatchingPair(key); + if (!pair) return; - items[count].key.set(key); - items[count].value.set(value); - count++; - } + pair->value.set(value); + } using JsonPrintable::printTo; @@ -53,6 +51,8 @@ namespace ArduinoJson { } + KeyValuePair* getMatchingPair(const char* key); + private: KeyValuePair* items; int capacity, count;