mirror of
https://github.com/bblanchon/ArduinoJson.git
synced 2025-07-18 04:52:22 +02:00
Added JsonObjectBase::remove()
This commit is contained in:
@ -82,3 +82,11 @@ bool JsonObjectBase::containsKey(JsonKey key) const
|
|||||||
{
|
{
|
||||||
return getMatchingPair(key) != 0;
|
return getMatchingPair(key) != 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void JsonObjectBase::remove(JsonKey key)
|
||||||
|
{
|
||||||
|
KeyValuePair* match = getMatchingPair(key);
|
||||||
|
if (match == 0) return;
|
||||||
|
|
||||||
|
*match = items[--count];
|
||||||
|
}
|
@ -18,8 +18,8 @@ namespace ArduinoJson
|
|||||||
{
|
{
|
||||||
public:
|
public:
|
||||||
JsonValue& operator[](JsonKey);
|
JsonValue& operator[](JsonKey);
|
||||||
|
|
||||||
bool containsKey(JsonKey) const;
|
bool containsKey(JsonKey) const;
|
||||||
|
void remove(JsonKey key);
|
||||||
|
|
||||||
template<typename T>
|
template<typename T>
|
||||||
void add(JsonKey key, T value)
|
void add(JsonKey key, T value)
|
||||||
@ -53,7 +53,6 @@ namespace ArduinoJson
|
|||||||
private:
|
private:
|
||||||
KeyValuePair* items;
|
KeyValuePair* items;
|
||||||
int capacity, count;
|
int capacity, count;
|
||||||
|
|
||||||
static JsonValue nullValue;
|
static JsonValue nullValue;
|
||||||
|
|
||||||
KeyValuePair* getMatchingPair(JsonKey key) const;
|
KeyValuePair* getMatchingPair(JsonKey key) const;
|
||||||
|
@ -23,13 +23,34 @@ namespace JsonGeneratorTests
|
|||||||
mustNotContain("key");
|
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:
|
private:
|
||||||
|
|
||||||
|
@ -38,6 +38,33 @@ namespace JsonGeneratorTests
|
|||||||
outputMustBe("{\"key1\":\"value1\",\"key2\":\"value2\"}");
|
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)
|
TEST_METHOD(ReplaceExistingKey)
|
||||||
{
|
{
|
||||||
object["key"] = "value1";
|
object["key"] = "value1";
|
||||||
|
Reference in New Issue
Block a user