mirror of
				https://github.com/eledio-devices/thirdparty-ArduinoJson.git
				synced 2025-10-31 08:42:39 +01:00 
			
		
		
		
	Added a deprecation warning for as<char*>()
This commit is contained in:
		| @@ -61,6 +61,7 @@ HEAD | |||||||
| > Serial.println(doc["sensor"].as<const char*>());  // OK | > Serial.println(doc["sensor"].as<const char*>());  // OK | ||||||
| > ``` | > ``` | ||||||
| > | > | ||||||
|  | > A deprecation warning with the message "Replace `as<char*>()` with `as<const char*>()`" was added to allow a smooth transition. | ||||||
| > | > | ||||||
| > #### `DeserializationError::NotSupported` removed | > #### `DeserializationError::NotSupported` removed | ||||||
| > | > | ||||||
|   | |||||||
| @@ -5,6 +5,7 @@ | |||||||
| add_executable(MiscTests | add_executable(MiscTests | ||||||
| 	arithmeticCompare.cpp | 	arithmeticCompare.cpp | ||||||
| 	conflicts.cpp | 	conflicts.cpp | ||||||
|  | 	deprecated.cpp | ||||||
| 	FloatParts.cpp | 	FloatParts.cpp | ||||||
| 	JsonString.cpp | 	JsonString.cpp | ||||||
| 	printable.cpp | 	printable.cpp | ||||||
|   | |||||||
							
								
								
									
										39
									
								
								extras/tests/Misc/deprecated.cpp
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										39
									
								
								extras/tests/Misc/deprecated.cpp
									
									
									
									
									
										Normal file
									
								
							| @@ -0,0 +1,39 @@ | |||||||
|  | // ArduinoJson - https://arduinojson.org | ||||||
|  | // Copyright Benoit Blanchon 2014-2021 | ||||||
|  | // MIT License | ||||||
|  |  | ||||||
|  | #include <ArduinoJson.h> | ||||||
|  | #include <catch.hpp> | ||||||
|  |  | ||||||
|  | #if defined(__GNUC__) | ||||||
|  | #pragma GCC diagnostic ignored "-Wdeprecated-declarations" | ||||||
|  | #endif | ||||||
|  |  | ||||||
|  | #ifdef _MSC_VER | ||||||
|  | #pragma warning(disable : 4996)  // deprecation warning | ||||||
|  | #endif | ||||||
|  |  | ||||||
|  | TEST_CASE("Deprecated features") { | ||||||
|  |   StaticJsonDocument<256> doc; | ||||||
|  |   const char* s = "hello"; | ||||||
|  |   doc["s"] = s; | ||||||
|  |   doc["a"].add(s); | ||||||
|  |  | ||||||
|  |   SECTION("JsonVariant::as<char*>()") { | ||||||
|  |     JsonVariant v = doc["s"]; | ||||||
|  |     REQUIRE(v.as<char*>() == s); | ||||||
|  |   } | ||||||
|  |  | ||||||
|  |   SECTION("JsonVariantConst::as<char*>()") { | ||||||
|  |     JsonVariantConst v = doc["s"]; | ||||||
|  |     REQUIRE(v.as<char*>() == s); | ||||||
|  |   } | ||||||
|  |  | ||||||
|  |   SECTION("MemberProxy::as<char*>()") { | ||||||
|  |     REQUIRE(doc["s"].as<char*>() == s); | ||||||
|  |   } | ||||||
|  |  | ||||||
|  |   SECTION("ElementProxy::as<char*>()") { | ||||||
|  |     REQUIRE(doc["a"][0].as<char*>() == s); | ||||||
|  |   } | ||||||
|  | } | ||||||
| @@ -65,10 +65,17 @@ class ElementProxy : public VariantOperators<ElementProxy<TArray> >, | |||||||
|   } |   } | ||||||
|  |  | ||||||
|   template <typename T> |   template <typename T> | ||||||
|   FORCE_INLINE T as() const { |   FORCE_INLINE typename enable_if<!is_same<T, char*>::value, T>::type as() | ||||||
|  |       const { | ||||||
|     return getUpstreamElement().template as<T>(); |     return getUpstreamElement().template as<T>(); | ||||||
|   } |   } | ||||||
|  |  | ||||||
|  |   template <typename T> | ||||||
|  |   FORCE_INLINE typename enable_if<is_same<T, char*>::value, const char*>::type | ||||||
|  |   DEPRECATED("Replace as<char*>() with as<const char*>()") as() const { | ||||||
|  |     return as<const char*>(); | ||||||
|  |   } | ||||||
|  |  | ||||||
|   template <typename T> |   template <typename T> | ||||||
|   FORCE_INLINE operator T() const { |   FORCE_INLINE operator T() const { | ||||||
|     return getUpstreamElement(); |     return getUpstreamElement(); | ||||||
|   | |||||||
| @@ -46,20 +46,6 @@ class MemberProxy : public VariantOperators<MemberProxy<TObject, TStringRef> >, | |||||||
|   template <typename TValue> |   template <typename TValue> | ||||||
|   FORCE_INLINE typename enable_if<!is_array<TValue>::value, this_type &>::type |   FORCE_INLINE typename enable_if<!is_array<TValue>::value, this_type &>::type | ||||||
|   operator=(const TValue &src) { |   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); |     getOrAddUpstreamMember().set(src); | ||||||
|     return *this; |     return *this; | ||||||
|   } |   } | ||||||
| @@ -81,9 +67,16 @@ class MemberProxy : public VariantOperators<MemberProxy<TObject, TStringRef> >, | |||||||
|     return getUpstreamMember().isNull(); |     return getUpstreamMember().isNull(); | ||||||
|   } |   } | ||||||
|  |  | ||||||
|   template <typename TValue> |   template <typename T> | ||||||
|   FORCE_INLINE TValue as() const { |   FORCE_INLINE typename enable_if<!is_same<T, char *>::value, T>::type as() | ||||||
|     return getUpstreamMember().template as<TValue>(); |       const { | ||||||
|  |     return getUpstreamMember().template as<T>(); | ||||||
|  |   } | ||||||
|  |  | ||||||
|  |   template <typename T> | ||||||
|  |   FORCE_INLINE typename enable_if<is_same<T, char *>::value, const char *>::type | ||||||
|  |   DEPRECATED("Replace as<char*>() with as<const char*>()") as() const { | ||||||
|  |     return as<const char *>(); | ||||||
|   } |   } | ||||||
|  |  | ||||||
|   template <typename T> |   template <typename T> | ||||||
|   | |||||||
| @@ -94,10 +94,17 @@ class VariantRef : public VariantRefBase<VariantData>, | |||||||
|   } |   } | ||||||
|  |  | ||||||
|   template <typename T> |   template <typename T> | ||||||
|   FORCE_INLINE T as() const { |   FORCE_INLINE typename enable_if<!is_same<T, char *>::value, T>::type as() | ||||||
|  |       const { | ||||||
|     return Converter<T>::fromJson(*this); |     return Converter<T>::fromJson(*this); | ||||||
|   } |   } | ||||||
|  |  | ||||||
|  |   template <typename T> | ||||||
|  |   FORCE_INLINE typename enable_if<is_same<T, char *>::value, const char *>::type | ||||||
|  |   DEPRECATED("Replace as<char*>() with as<const char*>()") as() const { | ||||||
|  |     return as<const char *>(); | ||||||
|  |   } | ||||||
|  |  | ||||||
|   template <typename T> |   template <typename T> | ||||||
|   FORCE_INLINE bool is() const { |   FORCE_INLINE bool is() const { | ||||||
|     return Converter<T>::checkJson(*this); |     return Converter<T>::checkJson(*this); | ||||||
| @@ -204,10 +211,17 @@ class VariantConstRef : public VariantRefBase<const VariantData>, | |||||||
|   } |   } | ||||||
|  |  | ||||||
|   template <typename T> |   template <typename T> | ||||||
|   FORCE_INLINE T as() const { |   FORCE_INLINE typename enable_if<!is_same<T, char *>::value, T>::type as() | ||||||
|  |       const { | ||||||
|     return Converter<T>::fromJson(*this); |     return Converter<T>::fromJson(*this); | ||||||
|   } |   } | ||||||
|  |  | ||||||
|  |   template <typename T> | ||||||
|  |   FORCE_INLINE typename enable_if<is_same<T, char *>::value, const char *>::type | ||||||
|  |   DEPRECATED("Replace as<char*>() with as<const char*>()") as() const { | ||||||
|  |     return as<const char *>(); | ||||||
|  |   } | ||||||
|  |  | ||||||
|   template <typename T> |   template <typename T> | ||||||
|   FORCE_INLINE bool is() const { |   FORCE_INLINE bool is() const { | ||||||
|     return Converter<T>::checkJson(*this); |     return Converter<T>::checkJson(*this); | ||||||
|   | |||||||
		Reference in New Issue
	
	Block a user