From ed5f890d2800152f930f2b586e5546d0cc26480c Mon Sep 17 00:00:00 2001 From: Benoit Blanchon Date: Tue, 26 Nov 2024 14:32:29 +0100 Subject: [PATCH] Replace `JsonString::Ownership` with `bool` --- CHANGELOG.md | 1 + extras/tests/JsonObject/subscript.cpp | 4 ++-- extras/tests/JsonVariant/set.cpp | 4 ++-- extras/tests/Misc/StringAdapters.cpp | 4 ++-- .../tests/MsgPackSerializer/serializeVariant.cpp | 3 +-- src/ArduinoJson/Memory/StringBuffer.hpp | 2 +- src/ArduinoJson/Memory/StringBuilder.hpp | 2 +- src/ArduinoJson/Strings/JsonString.hpp | 14 ++++++++------ src/ArduinoJson/Variant/VariantData.hpp | 12 +++++------- 9 files changed, 23 insertions(+), 23 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 105c2a61..d50b7a4d 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -8,6 +8,7 @@ HEAD * Make `ElementProxy` and `MemberProxy` non-copyable * Change string copy policy: only string literal are stored by pointer * `JsonString` is now stored by copy, unless specified otherwise +* Replace undocumented `JsonString::Ownership` with `bool` > ### BREAKING CHANGES > diff --git a/extras/tests/JsonObject/subscript.cpp b/extras/tests/JsonObject/subscript.cpp index 23512a02..0acc01e5 100644 --- a/extras/tests/JsonObject/subscript.cpp +++ b/extras/tests/JsonObject/subscript.cpp @@ -158,7 +158,7 @@ TEST_CASE("JsonObject::operator[]") { } SECTION("should duplicate a non-static JsonString key") { - obj[JsonString("hello", JsonString::Copied)] = "world"; + obj[JsonString("hello", false)] = "world"; REQUIRE(spy.log() == AllocatorLog{ Allocate(sizeofPool()), Allocate(sizeofString("hello")), @@ -166,7 +166,7 @@ TEST_CASE("JsonObject::operator[]") { } SECTION("should not duplicate a static JsonString key") { - obj[JsonString("hello", JsonString::Linked)] = "world"; + obj[JsonString("hello", true)] = "world"; REQUIRE(spy.log() == AllocatorLog{ Allocate(sizeofPool()), }); diff --git a/extras/tests/JsonVariant/set.cpp b/extras/tests/JsonVariant/set.cpp index 8e5c1a29..b71c0016 100644 --- a/extras/tests/JsonVariant/set.cpp +++ b/extras/tests/JsonVariant/set.cpp @@ -132,7 +132,7 @@ TEST_CASE("JsonVariant::set() when there is enough memory") { char str[16]; strcpy(str, "hello"); - bool result = variant.set(JsonString(str, JsonString::Linked)); + bool result = variant.set(JsonString(str, true)); strcpy(str, "world"); REQUIRE(result == true); @@ -144,7 +144,7 @@ TEST_CASE("JsonVariant::set() when there is enough memory") { char str[16]; strcpy(str, "hello"); - bool result = variant.set(JsonString(str, JsonString::Copied)); + bool result = variant.set(JsonString(str)); strcpy(str, "world"); REQUIRE(result == true); diff --git a/extras/tests/Misc/StringAdapters.cpp b/extras/tests/Misc/StringAdapters.cpp index d8d21e46..5e344a9a 100644 --- a/extras/tests/Misc/StringAdapters.cpp +++ b/extras/tests/Misc/StringAdapters.cpp @@ -101,7 +101,7 @@ TEST_CASE("adaptString()") { } SECTION("JsonString linked") { - JsonString orig("hello", JsonString::Ownership::Linked); + JsonString orig("hello", true); auto s = adaptString(orig); CHECK(s.isNull() == false); @@ -110,7 +110,7 @@ TEST_CASE("adaptString()") { } SECTION("JsonString copied") { - JsonString orig("hello", JsonString::Ownership::Copied); + JsonString orig("hello", false); auto s = adaptString(orig); CHECK(s.isNull() == false); diff --git a/extras/tests/MsgPackSerializer/serializeVariant.cpp b/extras/tests/MsgPackSerializer/serializeVariant.cpp index fdbf2a68..505b56ce 100644 --- a/extras/tests/MsgPackSerializer/serializeVariant.cpp +++ b/extras/tests/MsgPackSerializer/serializeVariant.cpp @@ -139,8 +139,7 @@ TEST_CASE("serialize MsgPack value") { SECTION("str 32") { std::string shortest(65536, '?'); - checkVariant(JsonString(shortest.c_str(), - JsonString::Linked), // force store by pointer + checkVariant(JsonString(shortest.c_str(), true), // force store by pointer "\xDB\x00\x01\x00\x00"_s + shortest); } diff --git a/src/ArduinoJson/Memory/StringBuffer.hpp b/src/ArduinoJson/Memory/StringBuffer.hpp index d5412393..e0259662 100644 --- a/src/ArduinoJson/Memory/StringBuffer.hpp +++ b/src/ArduinoJson/Memory/StringBuffer.hpp @@ -55,7 +55,7 @@ class StringBuffer { JsonString str() const { ARDUINOJSON_ASSERT(node_ != nullptr); - return JsonString(node_->data, node_->length, JsonString::Copied); + return JsonString(node_->data, node_->length); } private: diff --git a/src/ArduinoJson/Memory/StringBuilder.hpp b/src/ArduinoJson/Memory/StringBuilder.hpp index f3a842ac..e789952d 100644 --- a/src/ArduinoJson/Memory/StringBuilder.hpp +++ b/src/ArduinoJson/Memory/StringBuilder.hpp @@ -68,7 +68,7 @@ class StringBuilder { JsonString str() const { ARDUINOJSON_ASSERT(node_ != nullptr); node_->data[size_] = 0; - return JsonString(node_->data, size_, JsonString::Copied); + return JsonString(node_->data, size_); } private: diff --git a/src/ArduinoJson/Strings/JsonString.hpp b/src/ArduinoJson/Strings/JsonString.hpp index 006d8f25..1f1f26aa 100644 --- a/src/ArduinoJson/Strings/JsonString.hpp +++ b/src/ArduinoJson/Strings/JsonString.hpp @@ -4,6 +4,7 @@ #pragma once +#include #include #if ARDUINOJSON_ENABLE_STD_STREAM @@ -18,15 +19,16 @@ class JsonString { friend struct detail::StringAdapter; public: - enum Ownership { Copied, Linked }; - JsonString() : str_(nullptr, 0, true) {} - JsonString(const char* data, Ownership ownership = Copied) - : str_(data, data ? ::strlen(data) : 0, ownership == Linked) {} + JsonString(const char* data, bool isStatic = false) + : str_(data, data ? ::strlen(data) : 0, isStatic) {} - JsonString(const char* data, size_t sz, Ownership ownership = Copied) - : str_(data, sz, ownership == Linked) {} + template ::value && + !detail::is_same::value>> + JsonString(const char* data, TSize sz, bool isStatic = false) + : str_(data, size_t(sz), isStatic) {} // Returns a pointer to the characters. const char* c_str() const { diff --git a/src/ArduinoJson/Variant/VariantData.hpp b/src/ArduinoJson/Variant/VariantData.hpp index 3a4253fb..ba1bef8a 100644 --- a/src/ArduinoJson/Variant/VariantData.hpp +++ b/src/ArduinoJson/Variant/VariantData.hpp @@ -64,13 +64,11 @@ class VariantData { return visit.visit(content_.asObject); case VariantType::LinkedString: - return visit.visit( - JsonString(content_.asLinkedString, JsonString::Linked)); + return visit.visit(JsonString(content_.asLinkedString, true)); case VariantType::OwnedString: return visit.visit(JsonString(content_.asOwnedString->data, - content_.asOwnedString->length, - JsonString::Copied)); + content_.asOwnedString->length)); case VariantType::RawString: return visit.visit(RawString(content_.asOwnedString->data, @@ -262,7 +260,7 @@ class VariantData { switch (type_) { case VariantType::RawString: return JsonString(content_.asOwnedString->data, - content_.asOwnedString->length, JsonString::Copied); + content_.asOwnedString->length); default: return JsonString(); } @@ -271,10 +269,10 @@ class VariantData { JsonString asString() const { switch (type_) { case VariantType::LinkedString: - return JsonString(content_.asLinkedString, JsonString::Linked); + return JsonString(content_.asLinkedString, true); case VariantType::OwnedString: return JsonString(content_.asOwnedString->data, - content_.asOwnedString->length, JsonString::Copied); + content_.asOwnedString->length); default: return JsonString(); }