diff --git a/CHANGELOG.md b/CHANGELOG.md index 4f222fee..65f91915 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -5,6 +5,7 @@ HEAD ---- * Forbid `deserializeJson(JsonArray|JsonObject, ...)` (issue #2135) +* Fix VLA support in `JsonDocument::set()` v7.2.0 (2024-09-18) ------ diff --git a/extras/tests/JsonDocument/CMakeLists.txt b/extras/tests/JsonDocument/CMakeLists.txt index 0caa7b9e..e85db025 100644 --- a/extras/tests/JsonDocument/CMakeLists.txt +++ b/extras/tests/JsonDocument/CMakeLists.txt @@ -16,6 +16,7 @@ add_executable(JsonDocumentTests nesting.cpp overflowed.cpp remove.cpp + set.cpp shrinkToFit.cpp size.cpp subscript.cpp diff --git a/extras/tests/JsonDocument/set.cpp b/extras/tests/JsonDocument/set.cpp new file mode 100644 index 00000000..ce9de42a --- /dev/null +++ b/extras/tests/JsonDocument/set.cpp @@ -0,0 +1,79 @@ +#define ARDUINOJSON_ENABLE_ARDUINO_STRING 1 +#define ARDUINOJSON_ENABLE_PROGMEM 1 +#include + +#include + +#include "Allocators.hpp" +#include "Literals.hpp" + +TEST_CASE("JsonDocument::set()") { + SpyingAllocator spy; + JsonDocument doc(&spy); + + SECTION("integer") { + doc.set(42); + + REQUIRE(doc.as() == "42"); + REQUIRE(spy.log() == AllocatorLog{}); + } + + SECTION("const char*") { + doc.set("example"); + + REQUIRE(doc.as() == "example"_s); + REQUIRE(spy.log() == AllocatorLog{}); + } + + SECTION("std::string") { + doc.set("example"_s); + + REQUIRE(doc.as() == "example"_s); + REQUIRE(spy.log() == AllocatorLog{ + Allocate(sizeofString("example")), + }); + } + + SECTION("char*") { + char value[] = "example"; + doc.set(value); + + REQUIRE(doc.as() == "example"_s); + REQUIRE(spy.log() == AllocatorLog{ + Allocate(sizeofString("example")), + }); + } + + SECTION("Arduino String") { + doc.set(String("example")); + + REQUIRE(doc.as() == "example"_s); + REQUIRE(spy.log() == AllocatorLog{ + Allocate(sizeofString("example")), + }); + } + + SECTION("Flash string") { + doc.set(F("example")); + + REQUIRE(doc.as() == "example"_s); + REQUIRE(spy.log() == AllocatorLog{ + Allocate(sizeofString("example")), + }); + } + +#ifdef HAS_VARIABLE_LENGTH_ARRAY + SECTION("VLA") { + size_t i = 16; + char vla[i]; + strcpy(vla, "example"); + + doc.set(vla); + + REQUIRE(doc.as() == "example"_s); + REQUIRE(spy.log() == AllocatorLog{ + Allocate(sizeofString("example")), + }); + } +#endif +} diff --git a/src/ArduinoJson/Document/JsonDocument.hpp b/src/ArduinoJson/Document/JsonDocument.hpp index 5ba62f48..10b0c808 100644 --- a/src/ArduinoJson/Document/JsonDocument.hpp +++ b/src/ArduinoJson/Document/JsonDocument.hpp @@ -142,6 +142,13 @@ class JsonDocument : public detail::VariantOperators { return to().set(src); } + // Replaces the root with the specified value. + // https://arduinojson.org/v7/api/jsondocument/set/ + template + bool set(TChar* src) { + return to().set(src); + } + // Clears the document and converts it to the specified type. // https://arduinojson.org/v7/api/jsondocument/to/ template