Removed JsonVariant::as<char>() (fixes #1498)

This commit is contained in:
Benoit Blanchon
2021-02-18 08:48:10 +01:00
parent e22d4bf31f
commit 5234c8124b
6 changed files with 48 additions and 4 deletions

View File

@ -1,6 +1,22 @@
ArduinoJson: change log ArduinoJson: change log
======================= =======================
HEAD
----
* Removed `JsonVariant::as<char>()` (issue #1498)
> ### BREAKING CHANGE
>
> We cannot cast a `JsonVariant` to a `char` anymore, so the following will break:
> ```c++
> char age = doc["age"]; // error: no matching function for call to 'variantAs(VariantData*&)'
> ```
> Instead, you must use another integral type, such as `int8_t`:
> ```c++
> int8_t age = doc["age"]; // OK
> ```
v6.17.3 (2021-02-15) v6.17.3 (2021-02-15)
------- -------

View File

@ -39,3 +39,7 @@ build_should_fail(write_long_long)
add_executable(delete_jsondocument delete_jsondocument.cpp) add_executable(delete_jsondocument delete_jsondocument.cpp)
build_should_fail(delete_jsondocument) build_should_fail(delete_jsondocument)
add_executable(variant_as_char variant_as_char.cpp)
build_should_fail(variant_as_char)

View File

@ -0,0 +1,12 @@
// ArduinoJson - arduinojson.org
// Copyright Benoit Blanchon 2014-2021
// MIT License
#include <ArduinoJson.h>
// See issue #1498
int main() {
DynamicJsonDocument doc(1024);
doc["dummy"].as<char>();
}

View File

@ -68,9 +68,6 @@ TEST_CASE("JsonVariant set()/get()") {
SECTION("Float") { SECTION("Float") {
checkNumericType<float>(); checkNumericType<float>();
} }
SECTION("Char") {
checkNumericType<char>();
}
SECTION("SChar") { SECTION("SChar") {
checkNumericType<signed char>(); checkNumericType<signed char>();
} }

View File

@ -53,7 +53,8 @@ struct VariantConstAs<ArrayRef> {
// --- // ---
template <typename T> template <typename T>
inline typename enable_if<is_integral<T>::value && !is_same<bool, T>::value, inline typename enable_if<is_integral<T>::value && !is_same<bool, T>::value &&
!is_same<char, T>::value,
T>::type T>::type
variantAs(const VariantData* data) { variantAs(const VariantData* data) {
ARDUINOJSON_ASSERT_INTEGER_TYPE_IS_SUPPORTED(T); ARDUINOJSON_ASSERT_INTEGER_TYPE_IS_SUPPORTED(T);

View File

@ -251,6 +251,20 @@ class VariantRef : public VariantRefBase<VariantData>,
template <typename T> template <typename T>
FORCE_INLINE typename VariantAs<T>::type as() const { FORCE_INLINE typename VariantAs<T>::type as() const {
/********************************************************************
** THIS IS NOT A BUG IN THE LIBRARY **
** -------------------------------- **
** Get a compilation error pointing here? **
** It doesn't mean the error *is* here. **
** Often, it's because you try to extract the wrong value type. **
** **
** For example: **
** char* name = doc["name"]; **
** char age = doc["age"]; **
** Instead, use: **
** const char* name = doc["name"]; **
** int8_t age = doc["age"]; **
********************************************************************/
return variantAs<typename VariantAs<T>::type>(_data, _pool); return variantAs<typename VariantAs<T>::type>(_data, _pool);
} }