From 93cb3d2fdc49519a4b37c17602e9c433a047c9c0 Mon Sep 17 00:00:00 2001 From: Benoit Blanchon Date: Wed, 9 Aug 2023 14:48:58 +0200 Subject: [PATCH] Add a stub for `BasicJsonDocument` --- extras/tests/Deprecated/BasicJsonDocument.cpp | 69 +++++++++++++++++++ extras/tests/Deprecated/CMakeLists.txt | 1 + src/ArduinoJson/compatibility.hpp | 51 ++++++++++++++ 3 files changed, 121 insertions(+) create mode 100644 extras/tests/Deprecated/BasicJsonDocument.cpp diff --git a/extras/tests/Deprecated/BasicJsonDocument.cpp b/extras/tests/Deprecated/BasicJsonDocument.cpp new file mode 100644 index 00000000..3a412772 --- /dev/null +++ b/extras/tests/Deprecated/BasicJsonDocument.cpp @@ -0,0 +1,69 @@ +// ArduinoJson - https://arduinojson.org +// Copyright © 2014-2023, Benoit BLANCHON +// MIT License + +#include +#include + +#include + +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>::value == + true); + } + + SECTION("deserialize / serialize") { + BasicJsonDocument doc(256); + deserializeJson(doc, "{\"hello\":\"world\"}"); + REQUIRE(doc.as() == "{\"hello\":\"world\"}"); + doc.clear(); + REQUIRE(allocatorLog == "ARAARDDD"); + } + + SECTION("copy") { + BasicJsonDocument doc(256); + doc["hello"] = "world"; + auto copy = doc; + REQUIRE(copy.as() == "{\"hello\":\"world\"}"); + REQUIRE(allocatorLog == "AA"); + } + + SECTION("capacity") { + BasicJsonDocument doc(256); + REQUIRE(doc.capacity() == 256); + } + + SECTION("garbageCollect()") { + BasicJsonDocument doc(256); + doc.garbageCollect(); + } +} diff --git a/extras/tests/Deprecated/CMakeLists.txt b/extras/tests/Deprecated/CMakeLists.txt index 264cdced..89a696a2 100644 --- a/extras/tests/Deprecated/CMakeLists.txt +++ b/extras/tests/Deprecated/CMakeLists.txt @@ -15,6 +15,7 @@ if(MSVC) endif() add_executable(DeprecatedTests + BasicJsonDocument.cpp DynamicJsonDocument.cpp StaticJsonDocument.cpp ) diff --git a/src/ArduinoJson/compatibility.hpp b/src/ArduinoJson/compatibility.hpp index f3e8b2fc..9e9610b3 100644 --- a/src/ArduinoJson/compatibility.hpp +++ b/src/ArduinoJson/compatibility.hpp @@ -42,6 +42,57 @@ class ARDUINOJSON_DEPRECATED("use JsonDocument instead") StaticJsonDocument } }; +namespace detail { +template +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 +class ARDUINOJSON_DEPRECATED("use JsonDocument instead") BasicJsonDocument + : public JsonDocument { + public: + BasicJsonDocument(size_t capacity) + : JsonDocument(detail::AllocatorAdapter::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 {