diff --git a/CHANGELOG.md b/CHANGELOG.md index eef086b6..c787e9cf 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -61,6 +61,7 @@ HEAD > Serial.println(doc["sensor"].as()); // OK > ``` > +> A deprecation warning with the message "Replace `as()` with `as()`" was added to allow a smooth transition. > > #### `DeserializationError::NotSupported` removed > diff --git a/extras/tests/Misc/CMakeLists.txt b/extras/tests/Misc/CMakeLists.txt index d15e55c7..7aca704e 100644 --- a/extras/tests/Misc/CMakeLists.txt +++ b/extras/tests/Misc/CMakeLists.txt @@ -5,6 +5,7 @@ add_executable(MiscTests arithmeticCompare.cpp conflicts.cpp + deprecated.cpp FloatParts.cpp JsonString.cpp printable.cpp diff --git a/extras/tests/Misc/deprecated.cpp b/extras/tests/Misc/deprecated.cpp new file mode 100644 index 00000000..a8a92dec --- /dev/null +++ b/extras/tests/Misc/deprecated.cpp @@ -0,0 +1,39 @@ +// ArduinoJson - https://arduinojson.org +// Copyright Benoit Blanchon 2014-2021 +// MIT License + +#include +#include + +#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()") { + JsonVariant v = doc["s"]; + REQUIRE(v.as() == s); + } + + SECTION("JsonVariantConst::as()") { + JsonVariantConst v = doc["s"]; + REQUIRE(v.as() == s); + } + + SECTION("MemberProxy::as()") { + REQUIRE(doc["s"].as() == s); + } + + SECTION("ElementProxy::as()") { + REQUIRE(doc["a"][0].as() == s); + } +} diff --git a/src/ArduinoJson/Array/ElementProxy.hpp b/src/ArduinoJson/Array/ElementProxy.hpp index 140f8dff..9b115f49 100644 --- a/src/ArduinoJson/Array/ElementProxy.hpp +++ b/src/ArduinoJson/Array/ElementProxy.hpp @@ -65,10 +65,17 @@ class ElementProxy : public VariantOperators >, } template - FORCE_INLINE T as() const { + FORCE_INLINE typename enable_if::value, T>::type as() + const { return getUpstreamElement().template as(); } + template + FORCE_INLINE typename enable_if::value, const char*>::type + DEPRECATED("Replace as() with as()") as() const { + return as(); + } + template FORCE_INLINE operator T() const { return getUpstreamElement(); diff --git a/src/ArduinoJson/Object/MemberProxy.hpp b/src/ArduinoJson/Object/MemberProxy.hpp index 48c47600..1e765a42 100644 --- a/src/ArduinoJson/Object/MemberProxy.hpp +++ b/src/ArduinoJson/Object/MemberProxy.hpp @@ -46,20 +46,6 @@ class MemberProxy : public VariantOperators >, template FORCE_INLINE typename enable_if::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; } @@ -81,9 +67,16 @@ class MemberProxy : public VariantOperators >, return getUpstreamMember().isNull(); } - template - FORCE_INLINE TValue as() const { - return getUpstreamMember().template as(); + template + FORCE_INLINE typename enable_if::value, T>::type as() + const { + return getUpstreamMember().template as(); + } + + template + FORCE_INLINE typename enable_if::value, const char *>::type + DEPRECATED("Replace as() with as()") as() const { + return as(); } template diff --git a/src/ArduinoJson/Variant/VariantRef.hpp b/src/ArduinoJson/Variant/VariantRef.hpp index fbee399b..e29fa522 100644 --- a/src/ArduinoJson/Variant/VariantRef.hpp +++ b/src/ArduinoJson/Variant/VariantRef.hpp @@ -94,10 +94,17 @@ class VariantRef : public VariantRefBase, } template - FORCE_INLINE T as() const { + FORCE_INLINE typename enable_if::value, T>::type as() + const { return Converter::fromJson(*this); } + template + FORCE_INLINE typename enable_if::value, const char *>::type + DEPRECATED("Replace as() with as()") as() const { + return as(); + } + template FORCE_INLINE bool is() const { return Converter::checkJson(*this); @@ -204,10 +211,17 @@ class VariantConstRef : public VariantRefBase, } template - FORCE_INLINE T as() const { + FORCE_INLINE typename enable_if::value, T>::type as() + const { return Converter::fromJson(*this); } + template + FORCE_INLINE typename enable_if::value, const char *>::type + DEPRECATED("Replace as() with as()") as() const { + return as(); + } + template FORCE_INLINE bool is() const { return Converter::checkJson(*this);