Changed return type of convertToJson() and Converter::toJson() to void

This commit is contained in:
Benoit Blanchon
2021-07-23 13:23:48 +02:00
parent f5c7a6478e
commit 4073b52c00
9 changed files with 54 additions and 58 deletions

View File

@ -1,6 +1,11 @@
ArduinoJson: change log ArduinoJson: change log
======================= =======================
HEAD
----
* Changed return type of `convertToJson()` and `Converter<T>::toJson()` to `void`
v6.18.2 (2021-07-19) v6.18.2 (2021-07-19)
------- -------

View File

@ -13,11 +13,10 @@ struct Date {
int year; int year;
}; };
bool convertToJson(const Date& src, JsonVariant dst) { void convertToJson(const Date& src, JsonVariant dst) {
dst["day"] = src.day; dst["day"] = src.day;
dst["month"] = src.month; dst["month"] = src.month;
dst["year"] = src.year; dst["year"] = src.year;
return true;
} }
void convertFromJson(JsonVariantConst src, Date& dst) { void convertFromJson(JsonVariantConst src, Date& dst) {
@ -92,10 +91,9 @@ class Complex {
namespace ARDUINOJSON_NAMESPACE { namespace ARDUINOJSON_NAMESPACE {
template <> template <>
struct Converter<Complex> { struct Converter<Complex> {
static bool toJson(const Complex& src, VariantRef dst) { static void toJson(const Complex& src, VariantRef dst) {
dst["real"] = src.real(); dst["real"] = src.real();
dst["imag"] = src.imag(); dst["imag"] = src.imag();
return true;
} }
static Complex fromJson(VariantConstRef src) { static Complex fromJson(VariantConstRef src) {

View File

@ -173,8 +173,8 @@ class ArrayRef : public ArrayRefBase<CollectionData>,
template <> template <>
struct Converter<ArrayConstRef> { struct Converter<ArrayConstRef> {
static bool toJson(VariantConstRef src, VariantRef dst) { static void toJson(VariantConstRef src, VariantRef dst) {
return variantCopyFrom(getData(dst), getData(src), getPool(dst)); variantCopyFrom(getData(dst), getData(src), getPool(dst));
} }
static ArrayConstRef fromJson(VariantConstRef src) { static ArrayConstRef fromJson(VariantConstRef src) {
@ -189,8 +189,8 @@ struct Converter<ArrayConstRef> {
template <> template <>
struct Converter<ArrayRef> { struct Converter<ArrayRef> {
static bool toJson(VariantConstRef src, VariantRef dst) { static void toJson(VariantConstRef src, VariantRef dst) {
return variantCopyFrom(getData(dst), getData(src), getPool(dst)); variantCopyFrom(getData(dst), getData(src), getPool(dst));
} }
static ArrayRef fromJson(VariantRef src) { static ArrayRef fromJson(VariantRef src) {

View File

@ -178,8 +178,8 @@ class ElementProxy : public VariantOperators<ElementProxy<TArray> >,
return _array.getOrAddElement(_index); return _array.getOrAddElement(_index);
} }
friend bool convertToJson(const this_type& src, VariantRef dst) { friend void convertToJson(const this_type& src, VariantRef dst) {
return dst.set(src.getUpstreamElement()); dst.set(src.getUpstreamElement());
} }
TArray _array; TArray _array;

View File

@ -337,8 +337,8 @@ class JsonDocument : public Visitable {
JsonDocument& operator=(const JsonDocument&); JsonDocument& operator=(const JsonDocument&);
}; };
inline bool convertToJson(const JsonDocument& src, VariantRef dst) { inline void convertToJson(const JsonDocument& src, VariantRef dst) {
return dst.set(src.as<VariantConstRef>()); dst.set(src.as<VariantConstRef>());
} }
} // namespace ARDUINOJSON_NAMESPACE } // namespace ARDUINOJSON_NAMESPACE

View File

@ -187,8 +187,8 @@ class MemberProxy : public VariantOperators<MemberProxy<TObject, TStringRef> >,
return _object.getOrAddMember(_key); return _object.getOrAddMember(_key);
} }
friend bool convertToJson(const this_type &src, VariantRef dst) { friend void convertToJson(const this_type &src, VariantRef dst) {
return dst.set(src.getUpstreamMember()); dst.set(src.getUpstreamMember());
} }
TObject _object; TObject _object;

View File

@ -239,8 +239,8 @@ class ObjectRef : public ObjectRefBase<CollectionData>,
template <> template <>
struct Converter<ObjectConstRef> { struct Converter<ObjectConstRef> {
static bool toJson(VariantConstRef src, VariantRef dst) { static void toJson(VariantConstRef src, VariantRef dst) {
return variantCopyFrom(getData(dst), getData(src), getPool(dst)); variantCopyFrom(getData(dst), getData(src), getPool(dst));
} }
static ObjectConstRef fromJson(VariantConstRef src) { static ObjectConstRef fromJson(VariantConstRef src) {
@ -255,8 +255,8 @@ struct Converter<ObjectConstRef> {
template <> template <>
struct Converter<ObjectRef> { struct Converter<ObjectRef> {
static bool toJson(VariantConstRef src, VariantRef dst) { static void toJson(VariantConstRef src, VariantRef dst) {
return variantCopyFrom(getData(dst), getData(src), getPool(dst)); variantCopyFrom(getData(dst), getData(src), getPool(dst));
} }
static ObjectRef fromJson(VariantRef src) { static ObjectRef fromJson(VariantRef src) {

View File

@ -12,9 +12,9 @@ namespace ARDUINOJSON_NAMESPACE {
template <typename T, typename Enable> template <typename T, typename Enable>
struct Converter { struct Converter {
static bool toJson(const T& src, VariantRef dst) { static void toJson(const T& src, VariantRef dst) {
// clang-format off // 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 // clang-format on
} }
@ -38,13 +38,11 @@ template <typename T>
struct Converter< struct Converter<
T, typename enable_if<is_integral<T>::value && !is_same<bool, T>::value && T, typename enable_if<is_integral<T>::value && !is_same<bool, T>::value &&
!is_same<char, T>::value>::type> { !is_same<char, T>::value>::type> {
static bool toJson(T src, VariantRef dst) { static void toJson(T src, VariantRef dst) {
VariantData* data = getData(dst); VariantData* data = getData(dst);
ARDUINOJSON_ASSERT_INTEGER_TYPE_IS_SUPPORTED(T); ARDUINOJSON_ASSERT_INTEGER_TYPE_IS_SUPPORTED(T);
if (!data) if (data)
return false; data->setInteger(src);
data->setInteger(src);
return true;
} }
static T fromJson(VariantConstRef src) { static T fromJson(VariantConstRef src) {
@ -61,8 +59,8 @@ struct Converter<
template <typename T> template <typename T>
struct Converter<T, typename enable_if<is_enum<T>::value>::type> { struct Converter<T, typename enable_if<is_enum<T>::value>::type> {
static bool toJson(T src, VariantRef dst) { static void toJson(T src, VariantRef dst) {
return dst.set(static_cast<Integer>(src)); dst.set(static_cast<Integer>(src));
} }
static T fromJson(VariantConstRef src) { static T fromJson(VariantConstRef src) {
@ -78,12 +76,10 @@ struct Converter<T, typename enable_if<is_enum<T>::value>::type> {
template <> template <>
struct Converter<bool> { struct Converter<bool> {
static bool toJson(bool src, VariantRef dst) { static void toJson(bool src, VariantRef dst) {
VariantData* data = getData(dst); VariantData* data = getData(dst);
if (!data) if (data)
return false; data->setBoolean(src);
data->setBoolean(src);
return true;
} }
static bool fromJson(VariantConstRef src) { static bool fromJson(VariantConstRef src) {
@ -99,12 +95,10 @@ struct Converter<bool> {
template <typename T> template <typename T>
struct Converter<T, typename enable_if<is_floating_point<T>::value>::type> { struct Converter<T, typename enable_if<is_floating_point<T>::value>::type> {
static bool toJson(T src, VariantRef dst) { static void toJson(T src, VariantRef dst) {
VariantData* data = getData(dst); VariantData* data = getData(dst);
if (!data) if (data)
return false; data->setFloat(static_cast<Float>(src));
data->setFloat(static_cast<Float>(src));
return true;
} }
static T fromJson(VariantConstRef src) { static T fromJson(VariantConstRef src) {
@ -120,8 +114,8 @@ struct Converter<T, typename enable_if<is_floating_point<T>::value>::type> {
template <> template <>
struct Converter<const char*> { struct Converter<const char*> {
static bool toJson(const char* src, VariantRef dst) { static void toJson(const char* src, VariantRef dst) {
return variantSetString(getData(dst), adaptString(src), getPool(dst)); variantSetString(getData(dst), adaptString(src), getPool(dst));
} }
static const char* fromJson(VariantConstRef src) { static const char* fromJson(VariantConstRef src) {
@ -163,12 +157,10 @@ canConvertFromJson(VariantConstRef src, const T&) {
template <> template <>
struct Converter<SerializedValue<const char*> > { struct Converter<SerializedValue<const char*> > {
static bool toJson(SerializedValue<const char*> src, VariantRef dst) { static void toJson(SerializedValue<const char*> src, VariantRef dst) {
VariantData* data = getData(dst); VariantData* data = getData(dst);
if (!data) if (data)
return false; data->setLinkedRaw(src);
data->setLinkedRaw(src);
return true;
} }
}; };
@ -178,10 +170,11 @@ struct Converter<SerializedValue<const char*> > {
template <typename T> template <typename T>
struct Converter<SerializedValue<T>, struct Converter<SerializedValue<T>,
typename enable_if<!is_same<const char*, T>::value>::type> { typename enable_if<!is_same<const char*, T>::value>::type> {
static bool toJson(SerializedValue<T> src, VariantRef dst) { static void toJson(SerializedValue<T> src, VariantRef dst) {
VariantData* data = getData(dst); VariantData* data = getData(dst);
MemoryPool* pool = getPool(dst); MemoryPool* pool = getPool(dst);
return data != 0 && data->setOwnedRaw(src, pool); if (data)
data->setOwnedRaw(src, pool);
} }
}; };
@ -189,9 +182,8 @@ struct Converter<SerializedValue<T>,
template <> template <>
struct Converter<decltype(nullptr)> { struct Converter<decltype(nullptr)> {
static bool toJson(decltype(nullptr), VariantRef dst) { static void toJson(decltype(nullptr), VariantRef dst) {
variantSetNull(getData(dst)); variantSetNull(getData(dst));
return true;
} }
static decltype(nullptr) fromJson(VariantConstRef) { static decltype(nullptr) fromJson(VariantConstRef) {
return nullptr; return nullptr;
@ -247,20 +239,19 @@ class MemoryPoolPrint : public Print {
size_t _capacity; size_t _capacity;
}; };
inline bool convertToJson(const ::Printable& src, VariantRef dst) { inline void convertToJson(const ::Printable& src, VariantRef dst) {
MemoryPool* pool = getPool(dst); MemoryPool* pool = getPool(dst);
VariantData* data = getData(dst); VariantData* data = getData(dst);
if (!pool || !data) if (!pool || !data)
return false; return;
MemoryPoolPrint print(pool); MemoryPoolPrint print(pool);
src.printTo(print); src.printTo(print);
if (print.overflowed()) { if (print.overflowed()) {
pool->markAsOverflowed(); pool->markAsOverflowed();
data->setNull(); data->setNull();
return false; return;
} }
data->setStringPointer(print.c_str(), storage_policies::store_by_copy()); data->setStringPointer(print.c_str(), storage_policies::store_by_copy());
return true;
} }
#endif #endif

View File

@ -85,7 +85,8 @@ class VariantRef : public VariantRefBase<VariantData>,
template <typename T> template <typename T>
FORCE_INLINE bool set(const T &value) const { FORCE_INLINE bool set(const T &value) const {
return Converter<T>::toJson(value, *this); Converter<T>::toJson(value, *this);
return _pool && !_pool->overflowed();
} }
bool ARDUINOJSON_DEPRECATED( bool ARDUINOJSON_DEPRECATED(
@ -94,7 +95,8 @@ class VariantRef : public VariantRefBase<VariantData>,
template <typename T> template <typename T>
FORCE_INLINE bool set(T *value) const { FORCE_INLINE bool set(T *value) const {
return Converter<T *>::toJson(value, *this); Converter<T *>::toJson(value, *this);
return _pool && !_pool->overflowed();
} }
template <typename T> template <typename T>
@ -339,8 +341,8 @@ class VariantConstRef : public VariantRefBase<const VariantData>,
template <> template <>
struct Converter<VariantRef> { struct Converter<VariantRef> {
static bool toJson(VariantRef src, VariantRef dst) { static void toJson(VariantRef src, VariantRef dst) {
return variantCopyFrom(getData(dst), getData(src), getPool(dst)); variantCopyFrom(getData(dst), getData(src), getPool(dst));
} }
static VariantRef fromJson(VariantRef src) { static VariantRef fromJson(VariantRef src) {
@ -362,8 +364,8 @@ struct Converter<VariantRef> {
template <> template <>
struct Converter<VariantConstRef> { struct Converter<VariantConstRef> {
static bool toJson(VariantConstRef src, VariantRef dst) { static void toJson(VariantConstRef src, VariantRef dst) {
return variantCopyFrom(getData(dst), getData(src), getPool(dst)); variantCopyFrom(getData(dst), getData(src), getPool(dst));
} }
static VariantConstRef fromJson(VariantConstRef src) { static VariantConstRef fromJson(VariantConstRef src) {