From 4073b52c00eca4a0ae9a0fed6e29b371fee6df7f Mon Sep 17 00:00:00 2001 From: Benoit Blanchon Date: Fri, 23 Jul 2021 13:23:48 +0200 Subject: [PATCH] Changed return type of convertToJson() and Converter::toJson() to void --- CHANGELOG.md | 5 ++ extras/tests/JsonVariant/converters.cpp | 6 +-- src/ArduinoJson/Array/ArrayRef.hpp | 8 +-- src/ArduinoJson/Array/ElementProxy.hpp | 4 +- src/ArduinoJson/Document/JsonDocument.hpp | 4 +- src/ArduinoJson/Object/MemberProxy.hpp | 4 +- src/ArduinoJson/Object/ObjectRef.hpp | 8 +-- src/ArduinoJson/Variant/ConverterImpl.hpp | 59 ++++++++++------------- src/ArduinoJson/Variant/VariantRef.hpp | 14 +++--- 9 files changed, 54 insertions(+), 58 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 6e1997a4..e62fb2fc 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,6 +1,11 @@ ArduinoJson: change log ======================= +HEAD +---- + +* Changed return type of `convertToJson()` and `Converter::toJson()` to `void` + v6.18.2 (2021-07-19) ------- diff --git a/extras/tests/JsonVariant/converters.cpp b/extras/tests/JsonVariant/converters.cpp index 01e407b8..51aedc1a 100644 --- a/extras/tests/JsonVariant/converters.cpp +++ b/extras/tests/JsonVariant/converters.cpp @@ -13,11 +13,10 @@ struct Date { int year; }; -bool convertToJson(const Date& src, JsonVariant dst) { +void convertToJson(const Date& src, JsonVariant dst) { dst["day"] = src.day; dst["month"] = src.month; dst["year"] = src.year; - return true; } void convertFromJson(JsonVariantConst src, Date& dst) { @@ -92,10 +91,9 @@ class Complex { namespace ARDUINOJSON_NAMESPACE { template <> struct Converter { - static bool toJson(const Complex& src, VariantRef dst) { + static void toJson(const Complex& src, VariantRef dst) { dst["real"] = src.real(); dst["imag"] = src.imag(); - return true; } static Complex fromJson(VariantConstRef src) { diff --git a/src/ArduinoJson/Array/ArrayRef.hpp b/src/ArduinoJson/Array/ArrayRef.hpp index 219a6b84..4f8d0c63 100644 --- a/src/ArduinoJson/Array/ArrayRef.hpp +++ b/src/ArduinoJson/Array/ArrayRef.hpp @@ -173,8 +173,8 @@ class ArrayRef : public ArrayRefBase, template <> struct Converter { - static bool toJson(VariantConstRef src, VariantRef dst) { - return variantCopyFrom(getData(dst), getData(src), getPool(dst)); + static void toJson(VariantConstRef src, VariantRef dst) { + variantCopyFrom(getData(dst), getData(src), getPool(dst)); } static ArrayConstRef fromJson(VariantConstRef src) { @@ -189,8 +189,8 @@ struct Converter { template <> struct Converter { - static bool toJson(VariantConstRef src, VariantRef dst) { - return variantCopyFrom(getData(dst), getData(src), getPool(dst)); + static void toJson(VariantConstRef src, VariantRef dst) { + variantCopyFrom(getData(dst), getData(src), getPool(dst)); } static ArrayRef fromJson(VariantRef src) { diff --git a/src/ArduinoJson/Array/ElementProxy.hpp b/src/ArduinoJson/Array/ElementProxy.hpp index 7ee7f03b..c1016eb0 100644 --- a/src/ArduinoJson/Array/ElementProxy.hpp +++ b/src/ArduinoJson/Array/ElementProxy.hpp @@ -178,8 +178,8 @@ class ElementProxy : public VariantOperators >, return _array.getOrAddElement(_index); } - friend bool convertToJson(const this_type& src, VariantRef dst) { - return dst.set(src.getUpstreamElement()); + friend void convertToJson(const this_type& src, VariantRef dst) { + dst.set(src.getUpstreamElement()); } TArray _array; diff --git a/src/ArduinoJson/Document/JsonDocument.hpp b/src/ArduinoJson/Document/JsonDocument.hpp index 64c76b8f..d81853c3 100644 --- a/src/ArduinoJson/Document/JsonDocument.hpp +++ b/src/ArduinoJson/Document/JsonDocument.hpp @@ -337,8 +337,8 @@ class JsonDocument : public Visitable { JsonDocument& operator=(const JsonDocument&); }; -inline bool convertToJson(const JsonDocument& src, VariantRef dst) { - return dst.set(src.as()); +inline void convertToJson(const JsonDocument& src, VariantRef dst) { + dst.set(src.as()); } } // namespace ARDUINOJSON_NAMESPACE diff --git a/src/ArduinoJson/Object/MemberProxy.hpp b/src/ArduinoJson/Object/MemberProxy.hpp index 882b1fd2..f1463a3d 100644 --- a/src/ArduinoJson/Object/MemberProxy.hpp +++ b/src/ArduinoJson/Object/MemberProxy.hpp @@ -187,8 +187,8 @@ class MemberProxy : public VariantOperators >, return _object.getOrAddMember(_key); } - friend bool convertToJson(const this_type &src, VariantRef dst) { - return dst.set(src.getUpstreamMember()); + friend void convertToJson(const this_type &src, VariantRef dst) { + dst.set(src.getUpstreamMember()); } TObject _object; diff --git a/src/ArduinoJson/Object/ObjectRef.hpp b/src/ArduinoJson/Object/ObjectRef.hpp index cb541bdb..047d9b1b 100644 --- a/src/ArduinoJson/Object/ObjectRef.hpp +++ b/src/ArduinoJson/Object/ObjectRef.hpp @@ -239,8 +239,8 @@ class ObjectRef : public ObjectRefBase, template <> struct Converter { - static bool toJson(VariantConstRef src, VariantRef dst) { - return variantCopyFrom(getData(dst), getData(src), getPool(dst)); + static void toJson(VariantConstRef src, VariantRef dst) { + variantCopyFrom(getData(dst), getData(src), getPool(dst)); } static ObjectConstRef fromJson(VariantConstRef src) { @@ -255,8 +255,8 @@ struct Converter { template <> struct Converter { - static bool toJson(VariantConstRef src, VariantRef dst) { - return variantCopyFrom(getData(dst), getData(src), getPool(dst)); + static void toJson(VariantConstRef src, VariantRef dst) { + variantCopyFrom(getData(dst), getData(src), getPool(dst)); } static ObjectRef fromJson(VariantRef src) { diff --git a/src/ArduinoJson/Variant/ConverterImpl.hpp b/src/ArduinoJson/Variant/ConverterImpl.hpp index 34e12bbb..fb9876ed 100644 --- a/src/ArduinoJson/Variant/ConverterImpl.hpp +++ b/src/ArduinoJson/Variant/ConverterImpl.hpp @@ -12,9 +12,9 @@ namespace ARDUINOJSON_NAMESPACE { template struct Converter { - static bool toJson(const T& src, VariantRef dst) { + static void toJson(const T& src, VariantRef dst) { // clang-format off - return convertToJson(src, dst); // Error here? See https://arduinojson.org/v6/unsupported-set/ + convertToJson(src, dst); // Error here? See https://arduinojson.org/v6/unsupported-set/ // clang-format on } @@ -38,13 +38,11 @@ template struct Converter< T, typename enable_if::value && !is_same::value && !is_same::value>::type> { - static bool toJson(T src, VariantRef dst) { + static void toJson(T src, VariantRef dst) { VariantData* data = getData(dst); ARDUINOJSON_ASSERT_INTEGER_TYPE_IS_SUPPORTED(T); - if (!data) - return false; - data->setInteger(src); - return true; + if (data) + data->setInteger(src); } static T fromJson(VariantConstRef src) { @@ -61,8 +59,8 @@ struct Converter< template struct Converter::value>::type> { - static bool toJson(T src, VariantRef dst) { - return dst.set(static_cast(src)); + static void toJson(T src, VariantRef dst) { + dst.set(static_cast(src)); } static T fromJson(VariantConstRef src) { @@ -78,12 +76,10 @@ struct Converter::value>::type> { template <> struct Converter { - static bool toJson(bool src, VariantRef dst) { + static void toJson(bool src, VariantRef dst) { VariantData* data = getData(dst); - if (!data) - return false; - data->setBoolean(src); - return true; + if (data) + data->setBoolean(src); } static bool fromJson(VariantConstRef src) { @@ -99,12 +95,10 @@ struct Converter { template struct Converter::value>::type> { - static bool toJson(T src, VariantRef dst) { + static void toJson(T src, VariantRef dst) { VariantData* data = getData(dst); - if (!data) - return false; - data->setFloat(static_cast(src)); - return true; + if (data) + data->setFloat(static_cast(src)); } static T fromJson(VariantConstRef src) { @@ -120,8 +114,8 @@ struct Converter::value>::type> { template <> struct Converter { - static bool toJson(const char* src, VariantRef dst) { - return variantSetString(getData(dst), adaptString(src), getPool(dst)); + static void toJson(const char* src, VariantRef dst) { + variantSetString(getData(dst), adaptString(src), getPool(dst)); } static const char* fromJson(VariantConstRef src) { @@ -163,12 +157,10 @@ canConvertFromJson(VariantConstRef src, const T&) { template <> struct Converter > { - static bool toJson(SerializedValue src, VariantRef dst) { + static void toJson(SerializedValue src, VariantRef dst) { VariantData* data = getData(dst); - if (!data) - return false; - data->setLinkedRaw(src); - return true; + if (data) + data->setLinkedRaw(src); } }; @@ -178,10 +170,11 @@ struct Converter > { template struct Converter, typename enable_if::value>::type> { - static bool toJson(SerializedValue src, VariantRef dst) { + static void toJson(SerializedValue src, VariantRef dst) { VariantData* data = getData(dst); MemoryPool* pool = getPool(dst); - return data != 0 && data->setOwnedRaw(src, pool); + if (data) + data->setOwnedRaw(src, pool); } }; @@ -189,9 +182,8 @@ struct Converter, template <> struct Converter { - static bool toJson(decltype(nullptr), VariantRef dst) { + static void toJson(decltype(nullptr), VariantRef dst) { variantSetNull(getData(dst)); - return true; } static decltype(nullptr) fromJson(VariantConstRef) { return nullptr; @@ -247,20 +239,19 @@ class MemoryPoolPrint : public Print { size_t _capacity; }; -inline bool convertToJson(const ::Printable& src, VariantRef dst) { +inline void convertToJson(const ::Printable& src, VariantRef dst) { MemoryPool* pool = getPool(dst); VariantData* data = getData(dst); if (!pool || !data) - return false; + return; MemoryPoolPrint print(pool); src.printTo(print); if (print.overflowed()) { pool->markAsOverflowed(); data->setNull(); - return false; + return; } data->setStringPointer(print.c_str(), storage_policies::store_by_copy()); - return true; } #endif diff --git a/src/ArduinoJson/Variant/VariantRef.hpp b/src/ArduinoJson/Variant/VariantRef.hpp index 06615920..b05ed90c 100644 --- a/src/ArduinoJson/Variant/VariantRef.hpp +++ b/src/ArduinoJson/Variant/VariantRef.hpp @@ -85,7 +85,8 @@ class VariantRef : public VariantRefBase, template FORCE_INLINE bool set(const T &value) const { - return Converter::toJson(value, *this); + Converter::toJson(value, *this); + return _pool && !_pool->overflowed(); } bool ARDUINOJSON_DEPRECATED( @@ -94,7 +95,8 @@ class VariantRef : public VariantRefBase, template FORCE_INLINE bool set(T *value) const { - return Converter::toJson(value, *this); + Converter::toJson(value, *this); + return _pool && !_pool->overflowed(); } template @@ -339,8 +341,8 @@ class VariantConstRef : public VariantRefBase, template <> struct Converter { - static bool toJson(VariantRef src, VariantRef dst) { - return variantCopyFrom(getData(dst), getData(src), getPool(dst)); + static void toJson(VariantRef src, VariantRef dst) { + variantCopyFrom(getData(dst), getData(src), getPool(dst)); } static VariantRef fromJson(VariantRef src) { @@ -362,8 +364,8 @@ struct Converter { template <> struct Converter { - static bool toJson(VariantConstRef src, VariantRef dst) { - return variantCopyFrom(getData(dst), getData(src), getPool(dst)); + static void toJson(VariantConstRef src, VariantRef dst) { + variantCopyFrom(getData(dst), getData(src), getPool(dst)); } static VariantConstRef fromJson(VariantConstRef src) {