mirror of
				https://github.com/eledio-devices/thirdparty-ArduinoJson.git
				synced 2025-10-31 16:14:11 +01:00 
			
		
		
		
	Added support for enums in as<T>() and is<T>()  (fixes #1256)
				
					
				
			This commit is contained in:
		| @@ -8,6 +8,7 @@ HEAD | ||||
| * CMake: made project arch-independent | ||||
| * Visual Studio: fixed error C2766 with flag `/Zc:__cplusplus` (issue #1250) | ||||
| * Added support for `JsonDocument` to `copyArray()` (issue #1255) | ||||
| * Added support for `enum`s in `as<T>()` and `is<T>()`  (issue #1256) | ||||
|  | ||||
| v6.15.1 (2020-04-08) | ||||
| ------- | ||||
|   | ||||
| @@ -10,6 +10,8 @@ namespace my { | ||||
| using ARDUINOJSON_NAMESPACE::isinf; | ||||
| }  // namespace my | ||||
|  | ||||
| enum MY_ENUM { ONE = 1, TWO = 2 }; | ||||
|  | ||||
| TEST_CASE("JsonVariant::as()") { | ||||
|   static const char* null = 0; | ||||
|  | ||||
| @@ -212,4 +214,10 @@ TEST_CASE("JsonVariant::as()") { | ||||
|     REQUIRE(cvar.as<char*>() == std::string("hello")); | ||||
|     // REQUIRE(cvar.as<std::string>() == std::string("hello")); | ||||
|   } | ||||
|  | ||||
|   SECTION("as<enum>()") { | ||||
|     variant.set(1); | ||||
|  | ||||
|     REQUIRE(variant.as<MY_ENUM>() == ONE); | ||||
|   } | ||||
| } | ||||
|   | ||||
| @@ -5,6 +5,8 @@ | ||||
| #include <ArduinoJson.h> | ||||
| #include <catch.hpp> | ||||
|  | ||||
| enum MYENUM2 { ONE = 1, TWO = 2 }; | ||||
|  | ||||
| template <typename TVariant> | ||||
| void checkIsArray(TVariant var) { | ||||
|   REQUIRE(var.template is<JsonArray>()); | ||||
| @@ -80,6 +82,7 @@ void checkIsInteger(TVariant var) { | ||||
|   REQUIRE(var.template is<int>()); | ||||
|   REQUIRE(var.template is<float>()); | ||||
|   REQUIRE(var.template is<double>()); | ||||
|   REQUIRE(var.template is<MYENUM2>()); | ||||
|  | ||||
|   REQUIRE_FALSE(var.template is<bool>()); | ||||
|   REQUIRE_FALSE(var.template is<const char *>()); | ||||
| @@ -107,6 +110,7 @@ void checkIsString(TVariant var) { | ||||
|   REQUIRE_FALSE(var.template is<double>()); | ||||
|   REQUIRE_FALSE(var.template is<float>()); | ||||
|   REQUIRE_FALSE(var.template is<long>()); | ||||
|   REQUIRE_FALSE(var.template is<MYENUM2>()); | ||||
|   REQUIRE_FALSE(var.template is<JsonArray>()); | ||||
|   REQUIRE_FALSE(var.template is<JsonObject>()); | ||||
| } | ||||
|   | ||||
| @@ -57,6 +57,12 @@ inline typename enable_if<is_integral<T>::value, T>::type variantAs( | ||||
|   return data != 0 ? data->asIntegral<T>() : T(0); | ||||
| } | ||||
|  | ||||
| template <typename T> | ||||
| inline typename enable_if<is_enum<T>::value, T>::type variantAs( | ||||
|     const VariantData* data) { | ||||
|   return data != 0 ? static_cast<T>(data->asIntegral<int>()) : T(); | ||||
| } | ||||
|  | ||||
| template <typename T> | ||||
| inline typename enable_if<is_same<T, bool>::value, T>::type variantAs( | ||||
|     const VariantData* data) { | ||||
|   | ||||
| @@ -101,6 +101,11 @@ class VariantRefBase { | ||||
|     return variantIsNull(_data); | ||||
|   } | ||||
| #endif | ||||
|   // bool is<enum>() const; | ||||
|   template <typename T> | ||||
|   FORCE_INLINE typename enable_if<is_enum<T>::value, bool>::type is() const { | ||||
|     return variantIsInteger<int>(_data); | ||||
|   } | ||||
|  | ||||
|   FORCE_INLINE bool isNull() const { | ||||
|     return variantIsNull(_data); | ||||
|   | ||||
		Reference in New Issue
	
	Block a user