ソースを参照

Remove `JsonVariant::set(char)`

Benoit Blanchon 5 年 前
コミット
54d4b308f4

+ 13 - 2
CHANGELOG.md

@@ -4,9 +4,9 @@ ArduinoJson: change log
 HEAD
 ----
 
-* Removed `JsonVariant::as<char>()` (issue #1498)
+* Removed support for `char` values, see below (issue #1498)
 
-> ### BREAKING CHANGE
+> ### BREAKING CHANGES
 >
 > We cannot cast a `JsonVariant` to a `char` anymore, so the following will break:
 > ```c++
@@ -16,6 +16,17 @@ HEAD
 > ```c++
 > int8_t age = doc["age"];  // OK
 > ```
+>
+> Similarly, we cannot assign from a `char` anymore, so the following will break:
+> ```c++
+> char age;
+> doc["age"] = age;  // error: no matching function for call to 'VariantRef::set(const char&)'
+> ```
+> Instead, you must use another integral type, such as `int8_t`:
+> ```c++
+> int8_t age;
+> doc["age"] = age;  // OK
+> ```
 
 v6.17.3 (2021-02-15)
 -------

+ 2 - 0
extras/tests/FailingBuilds/CMakeLists.txt

@@ -43,3 +43,5 @@ build_should_fail(delete_jsondocument)
 add_executable(variant_as_char variant_as_char.cpp)
 build_should_fail(variant_as_char)
 
+add_executable(assign_char assign_char.cpp)
+build_should_fail(assign_char)

+ 12 - 0
extras/tests/FailingBuilds/assign_char.cpp

@@ -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"] = 'A';
+}

+ 14 - 0
src/ArduinoJson/Object/MemberProxy.hpp

@@ -46,6 +46,20 @@ class MemberProxy : public VariantOperators<MemberProxy<TObject, TStringRef> >,
   template <typename TValue>
   FORCE_INLINE typename enable_if<!is_array<TValue>::value, this_type &>::type
   operator=(const TValue &src) {
+    /********************************************************************
+     **                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 assign the wrong value type.   **
+     **                                                                **
+     **  For example:                                                  **
+     **    char age = 42                                               **
+     **    doc["age"] = age;                                           **
+     **  Instead, use:                                                 **
+     **    int8_t age = 42;                                            **
+     **    doc["age"] = age;                                           **
+     ********************************************************************/
     getOrAddUpstreamMember().set(src);
     return *this;
   }

+ 3 - 2
src/ArduinoJson/Variant/VariantRef.hpp

@@ -186,8 +186,9 @@ class VariantRef : public VariantRefBase<VariantData>,
   // set(unsigned long)
   template <typename T>
   FORCE_INLINE bool set(
-      T value, typename enable_if<is_integral<T>::value &&
-                                  !is_same<bool, T>::value>::type * = 0) const {
+      T value,
+      typename enable_if<is_integral<T>::value && !is_same<bool, T>::value &&
+                         !is_same<char, T>::value>::type * = 0) const {
     return variantSetInteger<T>(_data, value);
   }