From be5d5300a23473645d7e68315024d459887287de Mon Sep 17 00:00:00 2001 From: Benoit Blanchon Date: Wed, 9 Aug 2023 18:02:31 +0200 Subject: [PATCH] Add a stub for `DynamicJsonDocument` --- extras/tests/Deprecated/CMakeLists.txt | 1 + .../tests/Deprecated/DynamicJsonDocument.cpp | 37 +++++++++++++++++++ src/ArduinoJson/compatibility.hpp | 16 ++++++++ 3 files changed, 54 insertions(+) create mode 100644 extras/tests/Deprecated/DynamicJsonDocument.cpp diff --git a/extras/tests/Deprecated/CMakeLists.txt b/extras/tests/Deprecated/CMakeLists.txt index 2eaeefb7..264cdced 100644 --- a/extras/tests/Deprecated/CMakeLists.txt +++ b/extras/tests/Deprecated/CMakeLists.txt @@ -15,6 +15,7 @@ if(MSVC) endif() add_executable(DeprecatedTests + DynamicJsonDocument.cpp StaticJsonDocument.cpp ) diff --git a/extras/tests/Deprecated/DynamicJsonDocument.cpp b/extras/tests/Deprecated/DynamicJsonDocument.cpp new file mode 100644 index 00000000..93b2354a --- /dev/null +++ b/extras/tests/Deprecated/DynamicJsonDocument.cpp @@ -0,0 +1,37 @@ +// ArduinoJson - https://arduinojson.org +// Copyright © 2014-2023, Benoit BLANCHON +// MIT License + +#include +#include + +using ArduinoJson::detail::is_base_of; + +TEST_CASE("DynamicJsonDocument") { + SECTION("is a JsonDocument") { + REQUIRE(is_base_of::value == true); + } + + SECTION("deserialize / serialize") { + DynamicJsonDocument doc(256); + deserializeJson(doc, "{\"hello\":\"world\"}"); + REQUIRE(doc.as() == "{\"hello\":\"world\"}"); + } + + SECTION("copy") { + DynamicJsonDocument doc(256); + doc["hello"] = "world"; + auto copy = doc; + REQUIRE(copy.as() == "{\"hello\":\"world\"}"); + } + + SECTION("capacity") { + DynamicJsonDocument doc(256); + REQUIRE(doc.capacity() == 256); + } + + SECTION("garbageCollect()") { + DynamicJsonDocument doc(256); + doc.garbageCollect(); + } +} diff --git a/src/ArduinoJson/compatibility.hpp b/src/ArduinoJson/compatibility.hpp index a477fc92..f3e8b2fc 100644 --- a/src/ArduinoJson/compatibility.hpp +++ b/src/ArduinoJson/compatibility.hpp @@ -42,4 +42,20 @@ class ARDUINOJSON_DEPRECATED("use JsonDocument instead") StaticJsonDocument } }; +// DEPRECATED: use JsonDocument instead +class ARDUINOJSON_DEPRECATED("use JsonDocument instead") DynamicJsonDocument + : public JsonDocument { + public: + DynamicJsonDocument(size_t capacity) : _capacity(capacity) {} + + size_t capacity() const { + return _capacity; + } + + void garbageCollect() {} + + private: + size_t _capacity; +}; + ARDUINOJSON_END_PUBLIC_NAMESPACE