Skip to content

Commit 650d537

Browse files
committed
Improve error messages when using char or char*
See #2043
1 parent 0435945 commit 650d537

File tree

3 files changed

+18
-5
lines changed

3 files changed

+18
-5
lines changed

CHANGELOG.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,11 @@
11
ArduinoJson: change log
22
=======================
33

4+
HEAD
5+
----
6+
7+
* Improve error messages when using `char` or `char*` (issue #2043)
8+
49
v7.0.2 (2024-01-19)
510
------
611

src/ArduinoJson/Variant/ConverterImpl.hpp

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,13 +21,20 @@ ARDUINOJSON_BEGIN_PUBLIC_NAMESPACE
2121

2222
template <typename T, typename Enable>
2323
struct Converter {
24+
static_assert(!detail::is_same<T, char>::value,
25+
"type 'char' is not supported, use 'signed char', 'unsigned "
26+
"char' or another integer type instead");
27+
2428
static void toJson(const T& src, JsonVariant dst) {
2529
// clang-format off
2630
convertToJson(src, dst); // Error here? See https://arduinojson.org/v7/unsupported-set/
2731
// clang-format on
2832
}
2933

3034
static T fromJson(JsonVariantConst src) {
35+
static_assert(!detail::is_same<T, char*>::value,
36+
"type 'char*' is not supported, use 'const char*' instead");
37+
3138
// clang-format off
3239
T result; // Error here? See https://arduinojson.org/v7/non-default-constructible/
3340
convertFromJson(src, result); // Error here? See https://arduinojson.org/v7/unsupported-as/
@@ -36,6 +43,9 @@ struct Converter {
3643
}
3744

3845
static bool checkJson(JsonVariantConst src) {
46+
static_assert(!detail::is_same<T, char*>::value,
47+
"type 'char*' is not supported, use 'const char*' instead");
48+
3949
T dummy = T();
4050
// clang-format off
4151
return canConvertFromJson(src, dummy); // Error here? See https://arduinojson.org/v7/unsupported-is/

src/ArduinoJson/Variant/VariantRefBase.hpp

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -91,11 +91,9 @@ class VariantRefBase : public VariantTag {
9191
// Returns true if the value is of the specified type.
9292
// https://arduinojson.org/v7/api/jsonvariant/is/
9393
template <typename T>
94-
FORCE_INLINE typename enable_if<!ConverterNeedsWriteableRef<T>::value &&
95-
!is_same<T, char*>::value &&
96-
!is_same<T, char>::value,
97-
bool>::type
98-
is() const {
94+
FORCE_INLINE
95+
typename enable_if<!ConverterNeedsWriteableRef<T>::value, bool>::type
96+
is() const {
9997
return Converter<T>::checkJson(getVariantConst());
10098
}
10199

0 commit comments

Comments
 (0)