Skip to content

Commit f0e84e4

Browse files
committed
Fix support for const char[]
Fixes #2166
1 parent 91397f9 commit f0e84e4

File tree

5 files changed

+35
-0
lines changed

5 files changed

+35
-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
* Optimize storage of tiny strings (up to 3 characters)
8+
* Fix support for `const char[]` (issue #2166)
89

910
v7.3.1 (2025-02-27)
1011
------

extras/tests/Misc/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ add_executable(MiscTests
77
conflicts.cpp
88
issue1967.cpp
99
issue2129.cpp
10+
issue2166.cpp
1011
JsonString.cpp
1112
NoArduinoHeader.cpp
1213
printable.cpp

extras/tests/Misc/StringAdapters.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -128,6 +128,7 @@ TEST_CASE("IsString<T>") {
128128
CHECK(IsString<const __FlashStringHelper*>::value == true);
129129
CHECK(IsString<const char*>::value == true);
130130
CHECK(IsString<const char[8]>::value == true);
131+
CHECK(IsString<const char[]>::value == true);
131132
CHECK(IsString<::String>::value == true);
132133
CHECK(IsString<::StringSumHelper>::value == true);
133134
CHECK(IsString<const EmptyStruct*>::value == false);

extras/tests/Misc/issue2166.cpp

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
// ArduinoJson - https://arduinojson.org
2+
// Copyright © 2014-2025, Benoit BLANCHON
3+
// MIT License
4+
5+
#include <ArduinoJson.h>
6+
#include <catch.hpp>
7+
8+
struct CCLASS {
9+
static const char mszKey[];
10+
};
11+
12+
TEST_CASE("Issue #2166") {
13+
JsonDocument doc;
14+
doc[CCLASS::mszKey] = 12;
15+
REQUIRE(doc.as<std::string>() == "{\"test3\":12}");
16+
17+
JsonObject obj = doc.to<JsonObject>();
18+
obj[CCLASS::mszKey] = 12;
19+
REQUIRE(doc.as<std::string>() == "{\"test3\":12}");
20+
}
21+
22+
const char CCLASS::mszKey[] = "test3";

src/ArduinoJson/Strings/Adapters/RamString.hpp

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,16 @@ struct StringAdapter<TChar*, enable_if_t<IsChar<TChar>::value>> {
7676
}
7777
};
7878

79+
template <typename TChar>
80+
struct StringAdapter<TChar[], enable_if_t<IsChar<TChar>::value>> {
81+
using AdaptedString = RamString;
82+
83+
static AdaptedString adapt(const TChar* p) {
84+
auto str = reinterpret_cast<const char*>(p);
85+
return AdaptedString(str, str ? ::strlen(str) : 0);
86+
}
87+
};
88+
7989
template <size_t N>
8090
struct StringAdapter<const char (&)[N]> {
8191
using AdaptedString = RamString;

0 commit comments

Comments
 (0)