Improve error messages when using char or char*

See #2043
This commit is contained in:
Benoit Blanchon
2024-01-26 10:34:49 +01:00
parent 0435945a62
commit 650d537b5d
3 changed files with 18 additions and 5 deletions

View File

@@ -1,6 +1,11 @@
ArduinoJson: change log ArduinoJson: change log
======================= =======================
HEAD
----
* Improve error messages when using `char` or `char*` (issue #2043)
v7.0.2 (2024-01-19) v7.0.2 (2024-01-19)
------ ------

View File

@@ -21,6 +21,10 @@ ARDUINOJSON_BEGIN_PUBLIC_NAMESPACE
template <typename T, typename Enable> template <typename T, typename Enable>
struct Converter { struct Converter {
static_assert(!detail::is_same<T, char>::value,
"type 'char' is not supported, use 'signed char', 'unsigned "
"char' or another integer type instead");
static void toJson(const T& src, JsonVariant dst) { static void toJson(const T& src, JsonVariant dst) {
// clang-format off // clang-format off
convertToJson(src, dst); // Error here? See https://arduinojson.org/v7/unsupported-set/ convertToJson(src, dst); // Error here? See https://arduinojson.org/v7/unsupported-set/
@@ -28,6 +32,9 @@ struct Converter {
} }
static T fromJson(JsonVariantConst src) { static T fromJson(JsonVariantConst src) {
static_assert(!detail::is_same<T, char*>::value,
"type 'char*' is not supported, use 'const char*' instead");
// clang-format off // clang-format off
T result; // Error here? See https://arduinojson.org/v7/non-default-constructible/ T result; // Error here? See https://arduinojson.org/v7/non-default-constructible/
convertFromJson(src, result); // Error here? See https://arduinojson.org/v7/unsupported-as/ convertFromJson(src, result); // Error here? See https://arduinojson.org/v7/unsupported-as/
@@ -36,6 +43,9 @@ struct Converter {
} }
static bool checkJson(JsonVariantConst src) { static bool checkJson(JsonVariantConst src) {
static_assert(!detail::is_same<T, char*>::value,
"type 'char*' is not supported, use 'const char*' instead");
T dummy = T(); T dummy = T();
// clang-format off // clang-format off
return canConvertFromJson(src, dummy); // Error here? See https://arduinojson.org/v7/unsupported-is/ return canConvertFromJson(src, dummy); // Error here? See https://arduinojson.org/v7/unsupported-is/

View File

@@ -91,11 +91,9 @@ class VariantRefBase : public VariantTag {
// Returns true if the value is of the specified type. // Returns true if the value is of the specified type.
// https://arduinojson.org/v7/api/jsonvariant/is/ // https://arduinojson.org/v7/api/jsonvariant/is/
template <typename T> template <typename T>
FORCE_INLINE typename enable_if<!ConverterNeedsWriteableRef<T>::value && FORCE_INLINE
!is_same<T, char*>::value && typename enable_if<!ConverterNeedsWriteableRef<T>::value, bool>::type
!is_same<T, char>::value, is() const {
bool>::type
is() const {
return Converter<T>::checkJson(getVariantConst()); return Converter<T>::checkJson(getVariantConst());
} }