diff --git a/extras/tests/CMakeLists.txt b/extras/tests/CMakeLists.txt index fdab9b61..278bb963 100644 --- a/extras/tests/CMakeLists.txt +++ b/extras/tests/CMakeLists.txt @@ -12,6 +12,7 @@ link_libraries(ArduinoJson catch) include_directories(Helpers) add_subdirectory(Cpp17) add_subdirectory(Cpp20) +add_subdirectory(Deprecated) add_subdirectory(FailingBuilds) add_subdirectory(IntegrationTests) add_subdirectory(JsonArray) diff --git a/extras/tests/Deprecated/CMakeLists.txt b/extras/tests/Deprecated/CMakeLists.txt new file mode 100644 index 00000000..2eaeefb7 --- /dev/null +++ b/extras/tests/Deprecated/CMakeLists.txt @@ -0,0 +1,26 @@ +# ArduinoJson - https://arduinojson.org +# Copyright © 2014-2023, Benoit BLANCHON +# MIT License + +if(CMAKE_CXX_COMPILER_ID MATCHES "(GNU|Clang)") + add_compile_options( + -Wno-deprecated-declarations + ) +endif() + +if(MSVC) + add_compile_options( + /wd4996 + ) +endif() + +add_executable(DeprecatedTests + StaticJsonDocument.cpp +) + +add_test(Deprecated DeprecatedTests) + +set_tests_properties(Deprecated + PROPERTIES + LABELS "Catch" +) diff --git a/extras/tests/Deprecated/StaticJsonDocument.cpp b/extras/tests/Deprecated/StaticJsonDocument.cpp new file mode 100644 index 00000000..f3b8926c --- /dev/null +++ b/extras/tests/Deprecated/StaticJsonDocument.cpp @@ -0,0 +1,32 @@ +// ArduinoJson - https://arduinojson.org +// Copyright © 2014-2023, Benoit BLANCHON +// MIT License + +#include +#include + +using ArduinoJson::detail::is_base_of; + +TEST_CASE("StaticJsonDocument") { + SECTION("is a JsonDocument") { + REQUIRE(is_base_of>::value == true); + } + + SECTION("deserialize / serialize") { + StaticJsonDocument<256> doc; + deserializeJson(doc, "{\"hello\":\"world\"}"); + REQUIRE(doc.as() == "{\"hello\":\"world\"}"); + } + + SECTION("copy") { + StaticJsonDocument<256> doc; + doc["hello"] = "world"; + auto copy = doc; + REQUIRE(copy.as() == "{\"hello\":\"world\"}"); + } + + SECTION("capacity") { + StaticJsonDocument<256> doc; + REQUIRE(doc.capacity() == 256); + } +} diff --git a/src/ArduinoJson/Polyfills/attributes.hpp b/src/ArduinoJson/Polyfills/attributes.hpp index c4196fbb..29155a10 100644 --- a/src/ArduinoJson/Polyfills/attributes.hpp +++ b/src/ArduinoJson/Polyfills/attributes.hpp @@ -9,16 +9,32 @@ # define FORCE_INLINE // __forceinline causes C4714 when returning std::string # define NO_INLINE __declspec(noinline) +# ifndef ARDUINOJSON_DEPRECATED +# define ARDUINOJSON_DEPRECATED(msg) __declspec(deprecated(msg)) +# endif + #elif defined(__GNUC__) // GCC or Clang # define FORCE_INLINE __attribute__((always_inline)) # define NO_INLINE __attribute__((noinline)) +# ifndef ARDUINOJSON_DEPRECATED +# if __GNUC__ > 4 || (__GNUC__ == 4 && __GNUC_MINOR__ >= 5) +# define ARDUINOJSON_DEPRECATED(msg) __attribute__((deprecated(msg))) +# else +# define ARDUINOJSON_DEPRECATED(msg) __attribute__((deprecated)) +# endif +# endif + #else // Other compilers # define FORCE_INLINE # define NO_INLINE +# ifndef ARDUINOJSON_DEPRECATED +# define ARDUINOJSON_DEPRECATED(msg) +# endif + #endif #if defined(__has_attribute) diff --git a/src/ArduinoJson/compatibility.hpp b/src/ArduinoJson/compatibility.hpp index 85769487..a477fc92 100644 --- a/src/ArduinoJson/compatibility.hpp +++ b/src/ArduinoJson/compatibility.hpp @@ -4,6 +4,8 @@ // // clang-format off +#include + #ifdef __GNUC__ #define ARDUINOJSON_PRAGMA(x) _Pragma(#x) @@ -23,3 +25,21 @@ #define ARDUINOJSON_NAMESPACE _Pragma ("GCC warning \"ARDUINOJSON_NAMESPACE is deprecated, use ArduinoJson instead\"") ArduinoJson #endif + +// clang-format on + +ARDUINOJSON_BEGIN_PUBLIC_NAMESPACE + +// DEPRECATED: use JsonDocument instead +template +class ARDUINOJSON_DEPRECATED("use JsonDocument instead") StaticJsonDocument + : public JsonDocument { + public: + using JsonDocument::JsonDocument; + + size_t capacity() const { + return N; + } +}; + +ARDUINOJSON_END_PUBLIC_NAMESPACE