diff --git a/CHANGELOG.md b/CHANGELOG.md index 3ef8b7ec..1988a35e 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -5,6 +5,7 @@ HEAD ---- * Fixed `array[idx].as()` and `object[key].as()` +* Fixed return value of `JsonObject::set()` (issue #350) v5.6.6 ------ diff --git a/include/ArduinoJson/JsonObject.ipp b/include/ArduinoJson/JsonObject.ipp index 97e79caa..f04d53b5 100644 --- a/include/ArduinoJson/JsonObject.ipp +++ b/include/ArduinoJson/JsonObject.ipp @@ -15,14 +15,16 @@ namespace ArduinoJson { template <> inline bool JsonObject::setNodeValue(node_type *node, String &value) { - node->content.value = _buffer->strdup(value); - return node->content.value; + const char *dup = _buffer->strdup(value); + node->content.value = dup; + return dup; } template <> inline bool JsonObject::setNodeValue(node_type *node, const String &value) { - node->content.value = _buffer->strdup(value); - return node->content.value; + const char *dup = _buffer->strdup(value); + node->content.value = dup; + return dup; } template <> diff --git a/test/JsonObject_Set_Tests.cpp b/test/JsonObject_Set_Tests.cpp index 282c5f73..cc49b0e1 100644 --- a/test/JsonObject_Set_Tests.cpp +++ b/test/JsonObject_Set_Tests.cpp @@ -107,3 +107,21 @@ TEST_(StoreObjectSubscript) { EXPECT_EQ(42, _object["a"]); } + +TEST_(ShouldReturnTrue_WhenAllocationSucceeds) { + StaticJsonBuffer jsonBuffer; + JsonObject& obj = jsonBuffer.createObject(); + + bool result = obj.set(String("hello"), String("world")); + + ASSERT_TRUE(result); +} + +TEST_(ShouldReturnFalse_WhenAllocationFails) { + StaticJsonBuffer jsonBuffer; + JsonObject& obj = jsonBuffer.createObject(); + + bool result = obj.set(String("hello"), String("world")); + + ASSERT_FALSE(result); +}