From 0429016ff11053dcf866ddb81c362fa0ba808fdd Mon Sep 17 00:00:00 2001 From: Benoit Blanchon Date: Fri, 12 Nov 2021 15:28:33 +0100 Subject: [PATCH] Remove `IsWriteableString` --- extras/tests/Helpers/api/String.h | 5 ++ extras/tests/Misc/StringWriter.cpp | 14 ----- src/ArduinoJson/Strings/IsWriteableString.hpp | 37 ------------ src/ArduinoJson/Variant/ConverterImpl.hpp | 56 ++++++++++++------- 4 files changed, 42 insertions(+), 70 deletions(-) delete mode 100644 src/ArduinoJson/Strings/IsWriteableString.hpp diff --git a/extras/tests/Helpers/api/String.h b/extras/tests/Helpers/api/String.h index 31609de5..96b05e30 100644 --- a/extras/tests/Helpers/api/String.h +++ b/extras/tests/Helpers/api/String.h @@ -32,6 +32,11 @@ class String { return _str == s; } + String& operator=(const char* s) { + _str.assign(s); + return *this; + } + friend std::ostream& operator<<(std::ostream& lhs, const ::String& rhs) { lhs << rhs._str; return lhs; diff --git a/extras/tests/Misc/StringWriter.cpp b/extras/tests/Misc/StringWriter.cpp index 0df5a5b7..1928f69e 100644 --- a/extras/tests/Misc/StringWriter.cpp +++ b/extras/tests/Misc/StringWriter.cpp @@ -135,20 +135,6 @@ TEST_CASE("Writer") { REQUIRE("ABCD" == output); } -TEST_CASE("IsWriteableString") { - SECTION("std::string") { - REQUIRE(IsWriteableString::value == true); - } - - SECTION("custom_string") { - REQUIRE(IsWriteableString::value == true); - } - - SECTION("basic_string") { - REQUIRE(IsWriteableString >::value == false); - } -} - TEST_CASE("serializeJson(doc, String)") { StaticJsonDocument<1024> doc; doc["hello"] = "world"; diff --git a/src/ArduinoJson/Strings/IsWriteableString.hpp b/src/ArduinoJson/Strings/IsWriteableString.hpp deleted file mode 100644 index 556c4765..00000000 --- a/src/ArduinoJson/Strings/IsWriteableString.hpp +++ /dev/null @@ -1,37 +0,0 @@ -// ArduinoJson - https://arduinojson.org -// Copyright Benoit Blanchon 2014-2021 -// MIT License - -#pragma once - -#include -#include - -#if ARDUINOJSON_ENABLE_ARDUINO_STRING -# include -#endif - -#if ARDUINOJSON_ENABLE_STD_STRING -# include -#endif - -namespace ARDUINOJSON_NAMESPACE { - -template -struct IsWriteableString : false_type {}; - -#if ARDUINOJSON_ENABLE_ARDUINO_STRING - -template <> -struct IsWriteableString< ::String> : true_type {}; - -#endif - -#if ARDUINOJSON_ENABLE_STD_STRING - -template -struct IsWriteableString > - : true_type {}; - -#endif -} // namespace ARDUINOJSON_NAMESPACE diff --git a/src/ArduinoJson/Variant/ConverterImpl.hpp b/src/ArduinoJson/Variant/ConverterImpl.hpp index 236ed612..d282d8e2 100644 --- a/src/ArduinoJson/Variant/ConverterImpl.hpp +++ b/src/ArduinoJson/Variant/ConverterImpl.hpp @@ -4,7 +4,7 @@ #pragma once -#include +#include #include #include @@ -154,24 +154,6 @@ inline typename enable_if::value, bool>::type convertToJson( return variantSetString(data, adaptString(src), pool); } -template -inline typename enable_if::value>::type convertFromJson( - VariantConstRef src, T& dst) { - const VariantData* data = getData(src); - String str = data != 0 ? data->asString() : 0; - if (str) - dst = str.c_str(); - else - serializeJson(src, dst); -} - -template -inline typename enable_if::value, bool>::type -canConvertFromJson(VariantConstRef src, const T&) { - const VariantData* data = getData(src); - return data && data->isString(); -} - template <> struct Converter > { static void toJson(SerializedValue src, VariantRef dst) { @@ -273,6 +255,42 @@ inline void convertToJson(const ::Printable& src, VariantRef dst) { #endif +#if ARDUINOJSON_ENABLE_ARDUINO_STRING + +inline void convertFromJson(VariantConstRef src, ::String& dst) { + const VariantData* data = getData(src); + String str = data != 0 ? data->asString() : String(); + if (str) + dst = str.c_str(); + else + serializeJson(src, dst); +} + +inline bool canConvertFromJson(VariantConstRef src, const ::String&) { + const VariantData* data = getData(src); + return data && data->isString(); +} + +#endif + +#if ARDUINOJSON_ENABLE_STD_STRING + +inline void convertFromJson(VariantConstRef src, std::string& dst) { + const VariantData* data = getData(src); + String str = data != 0 ? data->asString() : String(); + if (str) + dst.assign(str.c_str()); + else + serializeJson(src, dst); +} + +inline bool canConvertFromJson(VariantConstRef src, const std::string&) { + const VariantData* data = getData(src); + return data && data->isString(); +} + +#endif + #if ARDUINOJSON_ENABLE_STRING_VIEW inline void convertFromJson(VariantConstRef src, std::string_view& dst) {