diff --git a/JsonGenerator/JsonObjectBase.cpp b/JsonGenerator/JsonObjectBase.cpp index 23efe612..45fdb6bf 100644 --- a/JsonGenerator/JsonObjectBase.cpp +++ b/JsonGenerator/JsonObjectBase.cpp @@ -81,4 +81,12 @@ JsonValue& JsonObjectBase::operator[](JsonKey key) bool JsonObjectBase::containsKey(JsonKey key) const { return getMatchingPair(key) != 0; +} + +void JsonObjectBase::remove(JsonKey key) +{ + KeyValuePair* match = getMatchingPair(key); + if (match == 0) return; + + *match = items[--count]; } \ No newline at end of file diff --git a/JsonGenerator/JsonObjectBase.h b/JsonGenerator/JsonObjectBase.h index 2cc293f7..ccfed649 100644 --- a/JsonGenerator/JsonObjectBase.h +++ b/JsonGenerator/JsonObjectBase.h @@ -18,8 +18,8 @@ namespace ArduinoJson { public: JsonValue& operator[](JsonKey); - bool containsKey(JsonKey) const; + void remove(JsonKey key); template void add(JsonKey key, T value) @@ -53,7 +53,6 @@ namespace ArduinoJson private: KeyValuePair* items; int capacity, count; - static JsonValue nullValue; KeyValuePair* getMatchingPair(JsonKey key) const; diff --git a/JsonGeneratorTests/JsonObject_Indexer_Tests.cpp b/JsonGeneratorTests/JsonObject_Indexer_Tests.cpp index 69a67de0..f3138331 100644 --- a/JsonGeneratorTests/JsonObject_Indexer_Tests.cpp +++ b/JsonGeneratorTests/JsonObject_Indexer_Tests.cpp @@ -22,14 +22,35 @@ namespace JsonGeneratorTests { mustNotContain("key"); } - - TEST_METHOD(OneString) + + TEST_METHOD(TwoStrings) { - object["key"] = "value"; + object["key1"] = "value1"; + object["key2"] = "value2"; - mustContain("key", "value"); + mustContain("key1", "value1"); + mustContain("key2", "value2"); } + TEST_METHOD(RemoveFirst) + { + object["key1"] = "value1"; + object["key2"] = "value2"; + object.remove("key1"); + + mustNotContain("key1"); + mustContain("key2", "value2"); + } + + TEST_METHOD(RemoveLast) + { + object["key1"] = "value1"; + object["key2"] = "value2"; + object.remove("key2"); + + mustContain("key1", "value1"); + mustNotContain("key2"); + } private: diff --git a/JsonGeneratorTests/JsonObject_PrintTo_Tests.cpp b/JsonGeneratorTests/JsonObject_PrintTo_Tests.cpp index 69d92029..277ab2f0 100644 --- a/JsonGeneratorTests/JsonObject_PrintTo_Tests.cpp +++ b/JsonGeneratorTests/JsonObject_PrintTo_Tests.cpp @@ -38,6 +38,33 @@ namespace JsonGeneratorTests outputMustBe("{\"key1\":\"value1\",\"key2\":\"value2\"}"); } + TEST_METHOD(RemoveFirst) + { + object["key1"] = "value1"; + object["key2"] = "value2"; + object.remove("key1"); + + outputMustBe("{\"key2\":\"value2\"}"); + } + + TEST_METHOD(RemoveLast) + { + object["key1"] = "value1"; + object["key2"] = "value2"; + object.remove("key2"); + + outputMustBe("{\"key1\":\"value1\"}"); + } + + TEST_METHOD(RemoveUnexistingKey) + { + object["key1"] = "value1"; + object["key2"] = "value2"; + object.remove("key3"); + + outputMustBe("{\"key1\":\"value1\",\"key2\":\"value2\"}"); + } + TEST_METHOD(ReplaceExistingKey) { object["key"] = "value1";