Pārlūkot izejas kodu

Add a stub for `StaticJsonDocument`

Benoit Blanchon 2 gadi atpakaļ
vecāks
revīzija
ea5b019552

+ 1 - 0
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)

+ 26 - 0
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"
+)

+ 32 - 0
extras/tests/Deprecated/StaticJsonDocument.cpp

@@ -0,0 +1,32 @@
+// ArduinoJson - https://arduinojson.org
+// Copyright © 2014-2023, Benoit BLANCHON
+// MIT License
+
+#include <ArduinoJson.h>
+#include <catch.hpp>
+
+using ArduinoJson::detail::is_base_of;
+
+TEST_CASE("StaticJsonDocument") {
+  SECTION("is a JsonDocument") {
+    REQUIRE(is_base_of<JsonDocument, StaticJsonDocument<256>>::value == true);
+  }
+
+  SECTION("deserialize / serialize") {
+    StaticJsonDocument<256> doc;
+    deserializeJson(doc, "{\"hello\":\"world\"}");
+    REQUIRE(doc.as<std::string>() == "{\"hello\":\"world\"}");
+  }
+
+  SECTION("copy") {
+    StaticJsonDocument<256> doc;
+    doc["hello"] = "world";
+    auto copy = doc;
+    REQUIRE(copy.as<std::string>() == "{\"hello\":\"world\"}");
+  }
+
+  SECTION("capacity") {
+    StaticJsonDocument<256> doc;
+    REQUIRE(doc.capacity() == 256);
+  }
+}

+ 16 - 0
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)

+ 20 - 0
src/ArduinoJson/compatibility.hpp

@@ -4,6 +4,8 @@
 //
 // clang-format off
 
+#include <ArduinoJson/Namespace.hpp>
+
 #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 <size_t N>
+class ARDUINOJSON_DEPRECATED("use JsonDocument instead") StaticJsonDocument
+    : public JsonDocument {
+ public:
+  using JsonDocument::JsonDocument;
+
+  size_t capacity() const {
+    return N;
+  }
+};
+
+ARDUINOJSON_END_PUBLIC_NAMESPACE