forked from bblanchon/ArduinoJson
Removed duplication when one replaces a value in a (PR #232 by @ulion)
This commit is contained in:
@ -1,6 +1,11 @@
|
||||
ArduinoJson: change log
|
||||
=======================
|
||||
|
||||
HEAD
|
||||
----
|
||||
|
||||
* Removed `String` duplication when one replaces a value in a `JsonObject` (PR #232 by @ulion)
|
||||
|
||||
v5.1.0
|
||||
------
|
||||
|
||||
|
@ -55,8 +55,12 @@ inline void JsonObject::remove(JsonObjectKey key) {
|
||||
template <typename T>
|
||||
inline bool JsonObject::setNodeAt(JsonObjectKey key, T value) {
|
||||
node_type *node = getNodeAt(key.c_str());
|
||||
if (!node) node = addNewNode();
|
||||
return node && setNodeKey(node, key) && setNodeValue<T>(node, value);
|
||||
if (!node) {
|
||||
node = addNewNode();
|
||||
if (!node || !setNodeKey(node, key))
|
||||
return false;
|
||||
}
|
||||
return setNodeValue<T>(node, value);
|
||||
}
|
||||
|
||||
inline bool JsonObject::setNodeKey(node_type *node, JsonObjectKey key) {
|
||||
|
@ -219,3 +219,27 @@ TEST_F(ArduinoStringTests, JsonObject_PrettyPrintTo) {
|
||||
object.prettyPrintTo(json);
|
||||
ASSERT_EQ(String("{\r\n \"key\": \"value\"\r\n}"), json);
|
||||
}
|
||||
|
||||
TEST_F(ArduinoStringTests, JsonBuffer_GrowWhenAddingNewKey) {
|
||||
JsonObject &object = _jsonBuffer.createObject();
|
||||
String key1("hello"), key2("world");
|
||||
|
||||
object[key1] = 1;
|
||||
size_t sizeBefore = _jsonBuffer.size();
|
||||
object[key2] = 2;
|
||||
size_t sizeAfter = _jsonBuffer.size();
|
||||
|
||||
ASSERT_GT(sizeAfter - sizeBefore, key2.size());
|
||||
}
|
||||
|
||||
TEST_F(ArduinoStringTests, JsonBuffer_DontGrowWhenReusingKey) {
|
||||
JsonObject &object = _jsonBuffer.createObject();
|
||||
String key("hello");
|
||||
|
||||
object[key] = 1;
|
||||
size_t sizeBefore = _jsonBuffer.size();
|
||||
object[key] = 2;
|
||||
size_t sizeAfter = _jsonBuffer.size();
|
||||
|
||||
ASSERT_EQ(sizeBefore, sizeAfter);
|
||||
}
|
||||
|
Reference in New Issue
Block a user