diff --git a/JsonGenerator/JsonObjectBase.cpp b/JsonGenerator/JsonObjectBase.cpp index 8bcd9510..32772d19 100644 --- a/JsonGenerator/JsonObjectBase.cpp +++ b/JsonGenerator/JsonObjectBase.cpp @@ -9,6 +9,10 @@ using namespace ArduinoJson::Generator; using namespace ArduinoJson::Internals; + +JsonValue JsonObjectBase::nullValue; + + size_t JsonObjectBase::printTo(Print& p) const { size_t n = 0; @@ -51,10 +55,19 @@ JsonValue& JsonObjectBase::operator[](char const* key) p++; } - if (count >= capacity) - return JsonValue::null(); - - count++; - p->key = key; - return p->value; + JsonValue* value; + + if (count < capacity) + { + count++; + p->key = key; + value = &p->value; + } + else + { + value = &nullValue; + } + + value->reset(); + return *value; } \ No newline at end of file diff --git a/JsonGenerator/JsonObjectBase.h b/JsonGenerator/JsonObjectBase.h index 26f59fee..4bcb4ad3 100644 --- a/JsonGenerator/JsonObjectBase.h +++ b/JsonGenerator/JsonObjectBase.h @@ -50,6 +50,8 @@ namespace ArduinoJson private: KeyValuePair* items; int capacity, count; + + static JsonValue nullValue; }; } } \ No newline at end of file diff --git a/JsonGenerator/JsonValue.cpp b/JsonGenerator/JsonValue.cpp index 28dbf357..d7de4673 100644 --- a/JsonGenerator/JsonValue.cpp +++ b/JsonGenerator/JsonValue.cpp @@ -9,8 +9,6 @@ using namespace ArduinoJson::Generator; 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 0cb2a905..ab3283eb 100644 --- a/JsonGenerator/JsonValue.h +++ b/JsonGenerator/JsonValue.h @@ -100,9 +100,10 @@ namespace ArduinoJson return printToImpl(content, p); } - static JsonValue& null() + void reset() { - return nullInstance; + content.asDouble = 0; + printToImpl = printStringTo; } private: @@ -129,8 +130,6 @@ namespace ArduinoJson { return p.print(c.asDouble, DIGITS); } - - static JsonValue nullInstance; }; } } \ No newline at end of file diff --git a/JsonGeneratorTests/JsonObject_Indexer_Tests.cpp b/JsonGeneratorTests/JsonObject_Indexer_Tests.cpp index 73fa9dde..33e714ef 100644 --- a/JsonGeneratorTests/JsonObject_Indexer_Tests.cpp +++ b/JsonGeneratorTests/JsonObject_Indexer_Tests.cpp @@ -18,10 +18,10 @@ namespace JsonGeneratorTests public: - /* TEST_METHOD(Empty) + TEST_METHOD(Empty) { mustNotContain("key"); - }*/ + } TEST_METHOD(OneString) { @@ -35,8 +35,14 @@ namespace JsonGeneratorTests void mustContain(const char* key, const char* expected) { - auto actual = (const char*) object[key]; + const char* actual = object[key]; Assert::AreEqual(expected, actual); } + + void mustNotContain(const char* key) + { + const char* actual = object[key]; + Assert::IsNull(actual); + } }; } \ No newline at end of file