Skip to content

Commit 1110d62

Browse files
committed
Fix VLA support in JsonDocument::set()
1 parent c6c0649 commit 1110d62

File tree

4 files changed

+88
-0
lines changed

4 files changed

+88
-0
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ HEAD
55
----
66

77
* Forbid `deserializeJson(JsonArray|JsonObject, ...)` (issue #2135)
8+
* Fix VLA support in `JsonDocument::set()`
89

910
v7.2.0 (2024-09-18)
1011
------

extras/tests/JsonDocument/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ add_executable(JsonDocumentTests
1616
nesting.cpp
1717
overflowed.cpp
1818
remove.cpp
19+
set.cpp
1920
shrinkToFit.cpp
2021
size.cpp
2122
subscript.cpp

extras/tests/JsonDocument/set.cpp

Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
#define ARDUINOJSON_ENABLE_ARDUINO_STRING 1
2+
#define ARDUINOJSON_ENABLE_PROGMEM 1
3+
#include <ArduinoJson.h>
4+
5+
#include <catch.hpp>
6+
7+
#include "Allocators.hpp"
8+
#include "Literals.hpp"
9+
10+
TEST_CASE("JsonDocument::set()") {
11+
SpyingAllocator spy;
12+
JsonDocument doc(&spy);
13+
14+
SECTION("integer") {
15+
doc.set(42);
16+
17+
REQUIRE(doc.as<std::string>() == "42");
18+
REQUIRE(spy.log() == AllocatorLog{});
19+
}
20+
21+
SECTION("const char*") {
22+
doc.set("example");
23+
24+
REQUIRE(doc.as<const char*>() == "example"_s);
25+
REQUIRE(spy.log() == AllocatorLog{});
26+
}
27+
28+
SECTION("std::string") {
29+
doc.set("example"_s);
30+
31+
REQUIRE(doc.as<const char*>() == "example"_s);
32+
REQUIRE(spy.log() == AllocatorLog{
33+
Allocate(sizeofString("example")),
34+
});
35+
}
36+
37+
SECTION("char*") {
38+
char value[] = "example";
39+
doc.set(value);
40+
41+
REQUIRE(doc.as<const char*>() == "example"_s);
42+
REQUIRE(spy.log() == AllocatorLog{
43+
Allocate(sizeofString("example")),
44+
});
45+
}
46+
47+
SECTION("Arduino String") {
48+
doc.set(String("example"));
49+
50+
REQUIRE(doc.as<const char*>() == "example"_s);
51+
REQUIRE(spy.log() == AllocatorLog{
52+
Allocate(sizeofString("example")),
53+
});
54+
}
55+
56+
SECTION("Flash string") {
57+
doc.set(F("example"));
58+
59+
REQUIRE(doc.as<const char*>() == "example"_s);
60+
REQUIRE(spy.log() == AllocatorLog{
61+
Allocate(sizeofString("example")),
62+
});
63+
}
64+
65+
#ifdef HAS_VARIABLE_LENGTH_ARRAY
66+
SECTION("VLA") {
67+
size_t i = 16;
68+
char vla[i];
69+
strcpy(vla, "example");
70+
71+
doc.set(vla);
72+
73+
REQUIRE(doc.as<const char*>() == "example"_s);
74+
REQUIRE(spy.log() == AllocatorLog{
75+
Allocate(sizeofString("example")),
76+
});
77+
}
78+
#endif
79+
}

src/ArduinoJson/Document/JsonDocument.hpp

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -142,6 +142,13 @@ class JsonDocument : public detail::VariantOperators<const JsonDocument&> {
142142
return to<JsonVariant>().set(src);
143143
}
144144

145+
// Replaces the root with the specified value.
146+
// https://arduinojson.org/v7/api/jsondocument/set/
147+
template <typename TChar>
148+
bool set(TChar* src) {
149+
return to<JsonVariant>().set(src);
150+
}
151+
145152
// Clears the document and converts it to the specified type.
146153
// https://arduinojson.org/v7/api/jsondocument/to/
147154
template <typename T>

0 commit comments

Comments
 (0)