mirror of
				https://github.com/eledio-devices/thirdparty-ArduinoJson.git
				synced 2025-10-31 16:14:11 +01:00 
			
		
		
		
	Fixed enums serialized as booleans (fixes #1197)
This commit is contained in:
		| @@ -9,6 +9,7 @@ HEAD | |||||||
| * Changed the array subscript operator to automatically add missing elements | * Changed the array subscript operator to automatically add missing elements | ||||||
| * Fixed "deprecated-copy" warning on GCC 9 (fixes #1184) | * Fixed "deprecated-copy" warning on GCC 9 (fixes #1184) | ||||||
| * Fixed `MemberProxy::set(char[])` not duplicating the string (issue #1191) | * Fixed `MemberProxy::set(char[])` not duplicating the string (issue #1191) | ||||||
|  | * Fixed enums serialized as booleans (issue #1197) | ||||||
|  |  | ||||||
| v6.14.1 (2020-01-27) | v6.14.1 (2020-01-27) | ||||||
| ------- | ------- | ||||||
|   | |||||||
| @@ -5,6 +5,8 @@ | |||||||
| #include <ArduinoJson.h> | #include <ArduinoJson.h> | ||||||
| #include <catch.hpp> | #include <catch.hpp> | ||||||
|  |  | ||||||
|  | enum ErrorCode { ERROR_01 = 1, ERROR_10 = 10 }; | ||||||
|  |  | ||||||
| TEST_CASE("JsonVariant and strings") { | TEST_CASE("JsonVariant and strings") { | ||||||
|   DynamicJsonDocument doc(4096); |   DynamicJsonDocument doc(4096); | ||||||
|   JsonVariant variant = doc.to<JsonVariant>(); |   JsonVariant variant = doc.to<JsonVariant>(); | ||||||
| @@ -91,6 +93,15 @@ TEST_CASE("JsonVariant and strings") { | |||||||
|  |  | ||||||
|     REQUIRE(variant == "hello"); |     REQUIRE(variant == "hello"); | ||||||
|   } |   } | ||||||
|  |  | ||||||
|  |   SECTION("stores an enum as an integer") { | ||||||
|  |     ErrorCode code = ERROR_10; | ||||||
|  |  | ||||||
|  |     variant.set(code); | ||||||
|  |  | ||||||
|  |     REQUIRE(variant.is<int>() == true); | ||||||
|  |     REQUIRE(variant.as<int>() == 10); | ||||||
|  |   } | ||||||
| } | } | ||||||
|  |  | ||||||
| TEST_CASE("JsonVariant with not enough memory") { | TEST_CASE("JsonVariant with not enough memory") { | ||||||
|   | |||||||
| @@ -7,6 +7,9 @@ | |||||||
|  |  | ||||||
| using namespace ARDUINOJSON_NAMESPACE; | using namespace ARDUINOJSON_NAMESPACE; | ||||||
|  |  | ||||||
|  | class EmptyClass {}; | ||||||
|  | enum EmptyEnum {}; | ||||||
|  |  | ||||||
| TEST_CASE("Polyfills/type_traits") { | TEST_CASE("Polyfills/type_traits") { | ||||||
|   SECTION("is_base_of") { |   SECTION("is_base_of") { | ||||||
|     REQUIRE_FALSE( |     REQUIRE_FALSE( | ||||||
| @@ -48,6 +51,30 @@ TEST_CASE("Polyfills/type_traits") { | |||||||
|     CHECK(is_unsigned<double>::value == false); |     CHECK(is_unsigned<double>::value == false); | ||||||
|   } |   } | ||||||
|  |  | ||||||
|  |   SECTION("is_convertible") { | ||||||
|  |     CHECK((is_convertible<short, int>::value == true)); | ||||||
|  |     CHECK((is_convertible<int, int>::value == true)); | ||||||
|  |     CHECK((is_convertible<EmptyEnum, int>::value == true)); | ||||||
|  |     CHECK((is_convertible<int*, int>::value == false)); | ||||||
|  |     CHECK((is_convertible<EmptyClass, int>::value == false)); | ||||||
|  |   } | ||||||
|  |  | ||||||
|  |   SECTION("is_class") { | ||||||
|  |     CHECK((is_class<int>::value == false)); | ||||||
|  |     CHECK((is_class<EmptyEnum>::value == false)); | ||||||
|  |     CHECK((is_class<int*>::value == false)); | ||||||
|  |     CHECK((is_class<EmptyClass>::value == true)); | ||||||
|  |   } | ||||||
|  |  | ||||||
|  |   SECTION("is_enum") { | ||||||
|  |     CHECK(is_enum<int>::value == false); | ||||||
|  |     CHECK(is_enum<EmptyEnum>::value == true); | ||||||
|  |     CHECK(is_enum<int*>::value == false); | ||||||
|  |     CHECK(is_enum<EmptyClass>::value == false); | ||||||
|  |     CHECK(is_enum<bool>::value == false); | ||||||
|  |     CHECK(is_enum<double>::value == false); | ||||||
|  |   } | ||||||
|  |  | ||||||
|   SECTION("IsVisitable") { |   SECTION("IsVisitable") { | ||||||
|     CHECK(IsVisitable<DeserializationError>::value == false); |     CHECK(IsVisitable<DeserializationError>::value == false); | ||||||
|     CHECK(IsVisitable<JsonPair>::value == false); |     CHECK(IsVisitable<JsonPair>::value == false); | ||||||
|   | |||||||
| @@ -9,7 +9,10 @@ | |||||||
| #include "type_traits/integral_constant.hpp" | #include "type_traits/integral_constant.hpp" | ||||||
| #include "type_traits/is_array.hpp" | #include "type_traits/is_array.hpp" | ||||||
| #include "type_traits/is_base_of.hpp" | #include "type_traits/is_base_of.hpp" | ||||||
|  | #include "type_traits/is_class.hpp" | ||||||
| #include "type_traits/is_const.hpp" | #include "type_traits/is_const.hpp" | ||||||
|  | #include "type_traits/is_convertible.hpp" | ||||||
|  | #include "type_traits/is_enum.hpp" | ||||||
| #include "type_traits/is_floating_point.hpp" | #include "type_traits/is_floating_point.hpp" | ||||||
| #include "type_traits/is_integral.hpp" | #include "type_traits/is_integral.hpp" | ||||||
| #include "type_traits/is_same.hpp" | #include "type_traits/is_same.hpp" | ||||||
|   | |||||||
							
								
								
									
										14
									
								
								src/ArduinoJson/Polyfills/type_traits/declval.hpp
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										14
									
								
								src/ArduinoJson/Polyfills/type_traits/declval.hpp
									
									
									
									
									
										Normal file
									
								
							| @@ -0,0 +1,14 @@ | |||||||
|  | // ArduinoJson - arduinojson.org | ||||||
|  | // Copyright Benoit Blanchon 2014-2020 | ||||||
|  | // MIT License | ||||||
|  |  | ||||||
|  | #pragma once | ||||||
|  |  | ||||||
|  | #include <ArduinoJson/Namespace.hpp> | ||||||
|  |  | ||||||
|  | namespace ARDUINOJSON_NAMESPACE { | ||||||
|  |  | ||||||
|  | template <typename T> | ||||||
|  | T declval(); | ||||||
|  |  | ||||||
|  | }  // namespace ARDUINOJSON_NAMESPACE | ||||||
							
								
								
									
										26
									
								
								src/ArduinoJson/Polyfills/type_traits/is_class.hpp
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										26
									
								
								src/ArduinoJson/Polyfills/type_traits/is_class.hpp
									
									
									
									
									
										Normal file
									
								
							| @@ -0,0 +1,26 @@ | |||||||
|  | // ArduinoJson - arduinojson.org | ||||||
|  | // Copyright Benoit Blanchon 2014-2020 | ||||||
|  | // MIT License | ||||||
|  |  | ||||||
|  | #pragma once | ||||||
|  |  | ||||||
|  | #include "declval.hpp" | ||||||
|  |  | ||||||
|  | namespace ARDUINOJSON_NAMESPACE { | ||||||
|  |  | ||||||
|  | template <typename T> | ||||||
|  | struct is_class { | ||||||
|  |  protected:  // <- to avoid GCC's "all member functions in class are private" | ||||||
|  |   typedef char Yes[1]; | ||||||
|  |   typedef char No[2]; | ||||||
|  |  | ||||||
|  |   template <typename U> | ||||||
|  |   static Yes &probe(void (U::*)(void)); | ||||||
|  |   template <typename> | ||||||
|  |   static No &probe(...); | ||||||
|  |  | ||||||
|  |  public: | ||||||
|  |   static const bool value = sizeof(probe<T>(0)) == sizeof(Yes); | ||||||
|  | }; | ||||||
|  |  | ||||||
|  | }  // namespace ARDUINOJSON_NAMESPACE | ||||||
							
								
								
									
										34
									
								
								src/ArduinoJson/Polyfills/type_traits/is_convertible.hpp
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										34
									
								
								src/ArduinoJson/Polyfills/type_traits/is_convertible.hpp
									
									
									
									
									
										Normal file
									
								
							| @@ -0,0 +1,34 @@ | |||||||
|  | // ArduinoJson - arduinojson.org | ||||||
|  | // Copyright Benoit Blanchon 2014-2020 | ||||||
|  | // MIT License | ||||||
|  |  | ||||||
|  | #pragma once | ||||||
|  |  | ||||||
|  | #include "declval.hpp" | ||||||
|  |  | ||||||
|  | #ifdef _MSC_VER | ||||||
|  | #pragma warning(push) | ||||||
|  | // conversion from 'T' to 'To', possible loss of data | ||||||
|  | #pragma warning(disable : 4244) | ||||||
|  | #endif | ||||||
|  |  | ||||||
|  | namespace ARDUINOJSON_NAMESPACE { | ||||||
|  |  | ||||||
|  | template <typename From, typename To> | ||||||
|  | struct is_convertible { | ||||||
|  |  protected:  // <- to avoid GCC's "all member functions in class are private" | ||||||
|  |   typedef char Yes[1]; | ||||||
|  |   typedef char No[2]; | ||||||
|  |  | ||||||
|  |   static Yes &probe(To); | ||||||
|  |   static No &probe(...); | ||||||
|  |  | ||||||
|  |  public: | ||||||
|  |   static const bool value = sizeof(probe(declval<From>())) == sizeof(Yes); | ||||||
|  | }; | ||||||
|  |  | ||||||
|  | }  // namespace ARDUINOJSON_NAMESPACE | ||||||
|  |  | ||||||
|  | #ifdef _MSC_VER | ||||||
|  | #pragma warning(pop) | ||||||
|  | #endif | ||||||
							
								
								
									
										23
									
								
								src/ArduinoJson/Polyfills/type_traits/is_enum.hpp
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										23
									
								
								src/ArduinoJson/Polyfills/type_traits/is_enum.hpp
									
									
									
									
									
										Normal file
									
								
							| @@ -0,0 +1,23 @@ | |||||||
|  | // ArduinoJson - arduinojson.org | ||||||
|  | // Copyright Benoit Blanchon 2014-2020 | ||||||
|  | // MIT License | ||||||
|  |  | ||||||
|  | #pragma once | ||||||
|  |  | ||||||
|  | #include "is_class.hpp" | ||||||
|  | #include "is_convertible.hpp" | ||||||
|  | #include "is_floating_point.hpp" | ||||||
|  | #include "is_integral.hpp" | ||||||
|  | #include "is_same.hpp" | ||||||
|  |  | ||||||
|  | namespace ARDUINOJSON_NAMESPACE { | ||||||
|  |  | ||||||
|  | template <typename T> | ||||||
|  | struct is_enum { | ||||||
|  |   static const bool value = is_convertible<T, int>::value && | ||||||
|  |                             !is_class<T>::value && !is_integral<T>::value && | ||||||
|  |                             !is_floating_point<T>::value && | ||||||
|  |                             !is_same<T, bool>::value; | ||||||
|  | }; | ||||||
|  |  | ||||||
|  | }  // namespace ARDUINOJSON_NAMESPACE | ||||||
| @@ -153,7 +153,9 @@ class VariantRef : public VariantRefBase<VariantData>, | |||||||
|   } |   } | ||||||
|  |  | ||||||
|   // set(bool value) |   // set(bool value) | ||||||
|   FORCE_INLINE bool set(bool value) const { |   template <typename T> | ||||||
|  |   FORCE_INLINE bool set( | ||||||
|  |       T value, typename enable_if<is_same<T, bool>::value>::type * = 0) const { | ||||||
|     return variantSetBoolean(_data, value); |     return variantSetBoolean(_data, value); | ||||||
|   } |   } | ||||||
|  |  | ||||||
| @@ -237,6 +239,13 @@ class VariantRef : public VariantRefBase<VariantData>, | |||||||
|   typename enable_if<IsVisitable<TVariant>::value, bool>::type set( |   typename enable_if<IsVisitable<TVariant>::value, bool>::type set( | ||||||
|       const TVariant &value) const; |       const TVariant &value) const; | ||||||
|  |  | ||||||
|  |   // set(enum value) | ||||||
|  |   template <typename T> | ||||||
|  |   FORCE_INLINE bool set( | ||||||
|  |       T value, typename enable_if<is_enum<T>::value>::type * = 0) const { | ||||||
|  |     return variantSetSignedInteger(_data, static_cast<Integer>(value)); | ||||||
|  |   } | ||||||
|  |  | ||||||
|   // Get the variant as the specified type. |   // Get the variant as the specified type. | ||||||
|   // |   // | ||||||
|   // std::string as<std::string>() const; |   // std::string as<std::string>() const; | ||||||
|   | |||||||
		Reference in New Issue
	
	Block a user