浏览代码

Fix `volatile bool` serialized as `1` or `0`

Fixes #2029
Benoit Blanchon 2 年之前
父节点
当前提交
5d1d2721d1
共有 3 个文件被更改,包括 10 次插入1 次删除
  1. 1 0
      CHANGELOG.md
  2. 7 0
      extras/tests/JsonVariant/types.cpp
  3. 2 1
      src/ArduinoJson/Variant/VariantImpl.hpp

+ 1 - 0
CHANGELOG.md

@@ -5,6 +5,7 @@ HEAD
 ----
 
 * Fix warning `function returns incomplete class type` on IAR (issue #2001)
+* Fix `volatile bool` serialized as `1` or `0` instead of `true` or `false` (issue #2029)
 
 v6.21.4 (2023-12-07)
 -------

+ 7 - 0
extras/tests/JsonVariant/types.cpp

@@ -140,6 +140,13 @@ TEST_CASE("volatile") {
   DynamicJsonDocument doc(4096);
   JsonVariant variant = doc.to<JsonVariant>();
 
+  SECTION("volatile bool") {  // issue #2029
+    volatile bool f = true;
+    variant.set(f);
+    CHECK(variant.is<bool>() == true);
+    CHECK(variant.as<bool>() == true);
+  }
+
   SECTION("volatile int") {
     volatile int f = 42;
     variant.set(f);

+ 2 - 1
src/ArduinoJson/Variant/VariantImpl.hpp

@@ -137,7 +137,8 @@ VariantRefBase<TDerived>::is() const {
 template <typename TDerived>
 template <typename T>
 inline bool VariantRefBase<TDerived>::set(const T& value) const {
-  Converter<T>::toJson(value, getOrCreateVariant());
+  Converter<typename detail::remove_cv<T>::type>::toJson(value,
+                                                         getOrCreateVariant());
   MemoryPool* pool = getPool();
   return pool && !pool->overflowed();
 }