Fix support for const char[]

Fixes #2166
This commit is contained in:
Benoit Blanchon
2025-04-07 09:46:02 +02:00
parent 91397f9f06
commit f0e84e4933
5 changed files with 35 additions and 0 deletions

View File

@ -5,6 +5,7 @@ HEAD
----
* Optimize storage of tiny strings (up to 3 characters)
* Fix support for `const char[]` (issue #2166)
v7.3.1 (2025-02-27)
------

View File

@ -7,6 +7,7 @@ add_executable(MiscTests
conflicts.cpp
issue1967.cpp
issue2129.cpp
issue2166.cpp
JsonString.cpp
NoArduinoHeader.cpp
printable.cpp

View File

@ -128,6 +128,7 @@ TEST_CASE("IsString<T>") {
CHECK(IsString<const __FlashStringHelper*>::value == true);
CHECK(IsString<const char*>::value == true);
CHECK(IsString<const char[8]>::value == true);
CHECK(IsString<const char[]>::value == true);
CHECK(IsString<::String>::value == true);
CHECK(IsString<::StringSumHelper>::value == true);
CHECK(IsString<const EmptyStruct*>::value == false);

View File

@ -0,0 +1,22 @@
// ArduinoJson - https://arduinojson.org
// Copyright © 2014-2025, Benoit BLANCHON
// MIT License
#include <ArduinoJson.h>
#include <catch.hpp>
struct CCLASS {
static const char mszKey[];
};
TEST_CASE("Issue #2166") {
JsonDocument doc;
doc[CCLASS::mszKey] = 12;
REQUIRE(doc.as<std::string>() == "{\"test3\":12}");
JsonObject obj = doc.to<JsonObject>();
obj[CCLASS::mszKey] = 12;
REQUIRE(doc.as<std::string>() == "{\"test3\":12}");
}
const char CCLASS::mszKey[] = "test3";

View File

@ -76,6 +76,16 @@ struct StringAdapter<TChar*, enable_if_t<IsChar<TChar>::value>> {
}
};
template <typename TChar>
struct StringAdapter<TChar[], enable_if_t<IsChar<TChar>::value>> {
using AdaptedString = RamString;
static AdaptedString adapt(const TChar* p) {
auto str = reinterpret_cast<const char*>(p);
return AdaptedString(str, str ? ::strlen(str) : 0);
}
};
template <size_t N>
struct StringAdapter<const char (&)[N]> {
using AdaptedString = RamString;