浏览代码

Remove `IsWriteableString`

Benoit Blanchon 4 年之前
父节点
当前提交
0429016ff1

+ 5 - 0
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;

+ 0 - 14
extras/tests/Misc/StringWriter.cpp

@@ -135,20 +135,6 @@ TEST_CASE("Writer<custom_string>") {
   REQUIRE("ABCD" == output);
 }
 
-TEST_CASE("IsWriteableString") {
-  SECTION("std::string") {
-    REQUIRE(IsWriteableString<std::string>::value == true);
-  }
-
-  SECTION("custom_string") {
-    REQUIRE(IsWriteableString<custom_string>::value == true);
-  }
-
-  SECTION("basic_string<wchar_t>") {
-    REQUIRE(IsWriteableString<std::basic_string<wchar_t> >::value == false);
-  }
-}
-
 TEST_CASE("serializeJson(doc, String)") {
   StaticJsonDocument<1024> doc;
   doc["hello"] = "world";

+ 0 - 37
src/ArduinoJson/Strings/IsWriteableString.hpp

@@ -1,37 +0,0 @@
-// ArduinoJson - https://arduinojson.org
-// Copyright Benoit Blanchon 2014-2021
-// MIT License
-
-#pragma once
-
-#include <ArduinoJson/Configuration.hpp>
-#include <ArduinoJson/Polyfills/type_traits.hpp>
-
-#if ARDUINOJSON_ENABLE_ARDUINO_STRING
-#  include <Arduino.h>
-#endif
-
-#if ARDUINOJSON_ENABLE_STD_STRING
-#  include <string>
-#endif
-
-namespace ARDUINOJSON_NAMESPACE {
-
-template <typename>
-struct IsWriteableString : false_type {};
-
-#if ARDUINOJSON_ENABLE_ARDUINO_STRING
-
-template <>
-struct IsWriteableString< ::String> : true_type {};
-
-#endif
-
-#if ARDUINOJSON_ENABLE_STD_STRING
-
-template <typename TCharTraits, typename TAllocator>
-struct IsWriteableString<std::basic_string<char, TCharTraits, TAllocator> >
-    : true_type {};
-
-#endif
-}  // namespace ARDUINOJSON_NAMESPACE

+ 37 - 19
src/ArduinoJson/Variant/ConverterImpl.hpp

@@ -4,7 +4,7 @@
 
 #pragma once
 
-#include <ArduinoJson/Strings/IsWriteableString.hpp>
+#include <ArduinoJson/Json/JsonSerializer.hpp>
 #include <ArduinoJson/Variant/VariantFunctions.hpp>
 #include <ArduinoJson/Variant/VariantRef.hpp>
 
@@ -154,24 +154,6 @@ inline typename enable_if<IsString<T>::value, bool>::type convertToJson(
   return variantSetString(data, adaptString(src), pool);
 }
 
-template <typename T>
-inline typename enable_if<IsWriteableString<T>::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 <typename T>
-inline typename enable_if<IsWriteableString<T>::value, bool>::type
-canConvertFromJson(VariantConstRef src, const T&) {
-  const VariantData* data = getData(src);
-  return data && data->isString();
-}
-
 template <>
 struct Converter<SerializedValue<const char*> > {
   static void toJson(SerializedValue<const char*> 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) {