Explorar o código

Add a stub for `BasicJsonDocument`

Benoit Blanchon %!s(int64=2) %!d(string=hai) anos
pai
achega
93cb3d2fdc

+ 69 - 0
extras/tests/Deprecated/BasicJsonDocument.cpp

@@ -0,0 +1,69 @@
+// ArduinoJson - https://arduinojson.org
+// Copyright © 2014-2023, Benoit BLANCHON
+// MIT License
+
+#include <ArduinoJson.h>
+#include <catch.hpp>
+
+#include <string>
+
+using ArduinoJson::detail::is_base_of;
+
+static std::string allocatorLog;
+
+struct CustomAllocator {
+  CustomAllocator() {
+    allocatorLog = "";
+  }
+
+  void* allocate(size_t n) {
+    allocatorLog += "A";
+    return malloc(n);
+  }
+
+  void deallocate(void* p) {
+    free(p);
+    allocatorLog += "D";
+  }
+
+  void* reallocate(void* p, size_t n) {
+    allocatorLog += "R";
+    return realloc(p, n);
+  }
+};
+
+TEST_CASE("BasicJsonDocument") {
+  allocatorLog.clear();
+
+  SECTION("is a JsonDocument") {
+    REQUIRE(
+        is_base_of<JsonDocument, BasicJsonDocument<CustomAllocator>>::value ==
+        true);
+  }
+
+  SECTION("deserialize / serialize") {
+    BasicJsonDocument<CustomAllocator> doc(256);
+    deserializeJson(doc, "{\"hello\":\"world\"}");
+    REQUIRE(doc.as<std::string>() == "{\"hello\":\"world\"}");
+    doc.clear();
+    REQUIRE(allocatorLog == "ARAARDDD");
+  }
+
+  SECTION("copy") {
+    BasicJsonDocument<CustomAllocator> doc(256);
+    doc["hello"] = "world";
+    auto copy = doc;
+    REQUIRE(copy.as<std::string>() == "{\"hello\":\"world\"}");
+    REQUIRE(allocatorLog == "AA");
+  }
+
+  SECTION("capacity") {
+    BasicJsonDocument<CustomAllocator> doc(256);
+    REQUIRE(doc.capacity() == 256);
+  }
+
+  SECTION("garbageCollect()") {
+    BasicJsonDocument<CustomAllocator> doc(256);
+    doc.garbageCollect();
+  }
+}

+ 1 - 0
extras/tests/Deprecated/CMakeLists.txt

@@ -15,6 +15,7 @@ if(MSVC)
 endif()
 
 add_executable(DeprecatedTests
+	BasicJsonDocument.cpp
 	DynamicJsonDocument.cpp
 	StaticJsonDocument.cpp
 )

+ 51 - 0
src/ArduinoJson/compatibility.hpp

@@ -42,6 +42,57 @@ class ARDUINOJSON_DEPRECATED("use JsonDocument instead") StaticJsonDocument
   }
 };
 
+namespace detail {
+template <typename TAllocator>
+class AllocatorAdapter : public Allocator {
+ public:
+  AllocatorAdapter(const AllocatorAdapter&) = delete;
+  AllocatorAdapter& operator=(const AllocatorAdapter&) = delete;
+
+  void* allocate(size_t size) override {
+    return _allocator.allocate(size);
+  }
+
+  void deallocate(void* ptr) override {
+    _allocator.deallocate(ptr);
+  }
+
+  void* reallocate(void* ptr, size_t new_size) override {
+    return _allocator.reallocate(ptr, new_size);
+  }
+
+  static Allocator* instance() {
+    static AllocatorAdapter instance;
+    return &instance;
+  }
+
+ private:
+  AllocatorAdapter() = default;
+  ~AllocatorAdapter() = default;
+
+  TAllocator _allocator;
+};
+}  // namespace detail
+
+// DEPRECATED: use JsonDocument instead
+template <typename TAllocator>
+class ARDUINOJSON_DEPRECATED("use JsonDocument instead") BasicJsonDocument
+    : public JsonDocument {
+ public:
+  BasicJsonDocument(size_t capacity)
+      : JsonDocument(detail::AllocatorAdapter<TAllocator>::instance()),
+        _capacity(capacity) {}
+
+  size_t capacity() const {
+    return _capacity;
+  }
+
+  void garbageCollect() {}
+
+ private:
+  size_t _capacity;
+};
+
 // DEPRECATED: use JsonDocument instead
 class ARDUINOJSON_DEPRECATED("use JsonDocument instead") DynamicJsonDocument
     : public JsonDocument {