Skip to content

Commit bec657e

Browse files
committed
Return bool from all built-in toJson converters
1 parent 1232f61 commit bec657e

File tree

8 files changed

+123
-109
lines changed

8 files changed

+123
-109
lines changed

src/ArduinoJson/Document/JsonDocument.hpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -446,8 +446,8 @@ class JsonDocument : public detail::VariantOperators<const JsonDocument&> {
446446
mutable detail::VariantData data_;
447447
};
448448

449-
inline void convertToJson(const JsonDocument& src, JsonVariant dst) {
450-
dst.set(src.as<JsonVariantConst>());
449+
inline bool convertToJson(const JsonDocument& src, JsonVariant dst) {
450+
return dst.set(src.as<JsonVariantConst>());
451451
}
452452

453453
ARDUINOJSON_END_PUBLIC_NAMESPACE

src/ArduinoJson/MsgPack/MsgPackBinary.hpp

Lines changed: 37 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -24,45 +24,48 @@ class MsgPackBinary {
2424

2525
template <>
2626
struct Converter<MsgPackBinary> : private detail::VariantAttorney {
27-
static void toJson(MsgPackBinary src, JsonVariant dst) {
27+
static bool toJson(MsgPackBinary src, JsonVariant dst) {
2828
auto data = VariantAttorney::getData(dst);
2929
if (!data)
30-
return;
30+
return false;
31+
3132
auto resources = getResourceManager(dst);
3233
detail::VariantImpl::clear(data, resources);
33-
if (src.data()) {
34-
size_t headerSize = src.size() >= 0x10000 ? 5
35-
: src.size() >= 0x100 ? 3
36-
: 2;
37-
auto str = resources->createString(src.size() + headerSize);
38-
if (str) {
39-
resources->saveString(str);
40-
auto ptr = reinterpret_cast<uint8_t*>(str->data);
41-
switch (headerSize) {
42-
case 2:
43-
ptr[0] = uint8_t(0xc4);
44-
ptr[1] = uint8_t(src.size() & 0xff);
45-
break;
46-
case 3:
47-
ptr[0] = uint8_t(0xc5);
48-
ptr[1] = uint8_t(src.size() >> 8 & 0xff);
49-
ptr[2] = uint8_t(src.size() & 0xff);
50-
break;
51-
case 5:
52-
ptr[0] = uint8_t(0xc6);
53-
ptr[1] = uint8_t(src.size() >> 24 & 0xff);
54-
ptr[2] = uint8_t(src.size() >> 16 & 0xff);
55-
ptr[3] = uint8_t(src.size() >> 8 & 0xff);
56-
ptr[4] = uint8_t(src.size() & 0xff);
57-
break;
58-
default:
59-
ARDUINOJSON_ASSERT(false);
60-
}
61-
memcpy(ptr + headerSize, src.data(), src.size());
62-
data->setRawString(str);
63-
return;
64-
}
34+
35+
if (!src.data())
36+
return true;
37+
38+
size_t headerSize = src.size() >= 0x10000 ? 5 : src.size() >= 0x100 ? 3 : 2;
39+
40+
auto str = resources->createString(src.size() + headerSize);
41+
if (!str)
42+
return false;
43+
44+
resources->saveString(str);
45+
auto ptr = reinterpret_cast<uint8_t*>(str->data);
46+
switch (headerSize) {
47+
case 2:
48+
ptr[0] = uint8_t(0xc4);
49+
ptr[1] = uint8_t(src.size() & 0xff);
50+
break;
51+
case 3:
52+
ptr[0] = uint8_t(0xc5);
53+
ptr[1] = uint8_t(src.size() >> 8 & 0xff);
54+
ptr[2] = uint8_t(src.size() & 0xff);
55+
break;
56+
case 5:
57+
ptr[0] = uint8_t(0xc6);
58+
ptr[1] = uint8_t(src.size() >> 24 & 0xff);
59+
ptr[2] = uint8_t(src.size() >> 16 & 0xff);
60+
ptr[3] = uint8_t(src.size() >> 8 & 0xff);
61+
ptr[4] = uint8_t(src.size() & 0xff);
62+
break;
63+
default:
64+
ARDUINOJSON_ASSERT(false);
6565
}
66+
memcpy(ptr + headerSize, src.data(), src.size());
67+
data->setRawString(str);
68+
return true;
6669
}
6770

6871
static MsgPackBinary fromJson(JsonVariantConst src) {

src/ArduinoJson/MsgPack/MsgPackExtension.hpp

Lines changed: 46 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -30,53 +30,57 @@ class MsgPackExtension {
3030

3131
template <>
3232
struct Converter<MsgPackExtension> : private detail::VariantAttorney {
33-
static void toJson(MsgPackExtension src, JsonVariant dst) {
33+
static bool toJson(MsgPackExtension src, JsonVariant dst) {
3434
auto data = getData(dst);
3535
if (!data)
36-
return;
36+
return false;
37+
3738
auto resources = getResourceManager(dst);
3839
detail::VariantImpl::clear(data, resources);
39-
if (src.data()) {
40-
uint8_t format, sizeBytes;
41-
if (src.size() >= 0x10000) {
42-
format = 0xc9; // ext 32
43-
sizeBytes = 4;
44-
} else if (src.size() >= 0x100) {
45-
format = 0xc8; // ext 16
46-
sizeBytes = 2;
47-
} else if (src.size() == 16) {
48-
format = 0xd8; // fixext 16
49-
sizeBytes = 0;
50-
} else if (src.size() == 8) {
51-
format = 0xd7; // fixext 8
52-
sizeBytes = 0;
53-
} else if (src.size() == 4) {
54-
format = 0xd6; // fixext 4
55-
sizeBytes = 0;
56-
} else if (src.size() == 2) {
57-
format = 0xd5; // fixext 2
58-
sizeBytes = 0;
59-
} else if (src.size() == 1) {
60-
format = 0xd4; // fixext 1
61-
sizeBytes = 0;
62-
} else {
63-
format = 0xc7; // ext 8
64-
sizeBytes = 1;
65-
}
66-
67-
auto str = resources->createString(src.size() + 2 + sizeBytes);
68-
if (str) {
69-
resources->saveString(str);
70-
auto ptr = reinterpret_cast<uint8_t*>(str->data);
71-
*ptr++ = uint8_t(format);
72-
for (uint8_t i = 0; i < sizeBytes; i++)
73-
*ptr++ = uint8_t(src.size() >> (sizeBytes - i - 1) * 8 & 0xff);
74-
*ptr++ = uint8_t(src.type());
75-
memcpy(ptr, src.data(), src.size());
76-
data->setRawString(str);
77-
return;
78-
}
40+
41+
if (!src.data())
42+
return true;
43+
44+
uint8_t format, sizeBytes;
45+
if (src.size() >= 0x10000) {
46+
format = 0xc9; // ext 32
47+
sizeBytes = 4;
48+
} else if (src.size() >= 0x100) {
49+
format = 0xc8; // ext 16
50+
sizeBytes = 2;
51+
} else if (src.size() == 16) {
52+
format = 0xd8; // fixext 16
53+
sizeBytes = 0;
54+
} else if (src.size() == 8) {
55+
format = 0xd7; // fixext 8
56+
sizeBytes = 0;
57+
} else if (src.size() == 4) {
58+
format = 0xd6; // fixext 4
59+
sizeBytes = 0;
60+
} else if (src.size() == 2) {
61+
format = 0xd5; // fixext 2
62+
sizeBytes = 0;
63+
} else if (src.size() == 1) {
64+
format = 0xd4; // fixext 1
65+
sizeBytes = 0;
66+
} else {
67+
format = 0xc7; // ext 8
68+
sizeBytes = 1;
7969
}
70+
71+
auto str = resources->createString(src.size() + 2 + sizeBytes);
72+
if (!str)
73+
return false;
74+
75+
resources->saveString(str);
76+
auto ptr = reinterpret_cast<uint8_t*>(str->data);
77+
*ptr++ = uint8_t(format);
78+
for (uint8_t i = 0; i < sizeBytes; i++)
79+
*ptr++ = uint8_t(src.size() >> (sizeBytes - i - 1) * 8 & 0xff);
80+
*ptr++ = uint8_t(src.type());
81+
memcpy(ptr, src.data(), src.size());
82+
data->setRawString(str);
83+
return true;
8084
}
8185

8286
static MsgPackExtension fromJson(JsonVariantConst src) {

src/ArduinoJson/Object/ObjectImpl.hpp

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,9 @@ inline VariantData* VariantImpl::addMember(TAdaptedString key,
6161
ARDUINOJSON_ASSERT(data->isObject());
6262
ARDUINOJSON_ASSERT(resources != nullptr);
6363

64+
if (key.isNull())
65+
return nullptr; // Ignore null key
66+
6467
auto keySlot = resources->allocVariant();
6568
if (!keySlot)
6669
return nullptr;

src/ArduinoJson/Variant/ConverterImpl.hpp

Lines changed: 23 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -25,9 +25,10 @@ struct Converter {
2525
"type 'char' is not supported, use 'signed char', 'unsigned "
2626
"char' or another integer type instead");
2727

28-
static void toJson(const T& src, JsonVariant dst) {
28+
static auto toJson(const T& src, JsonVariant dst)
29+
-> decltype(convertToJson(src, dst)) {
2930
// clang-format off
30-
convertToJson(src, dst); // Error here? See https://arduinojson.org/v7/unsupported-set/
31+
return convertToJson(src, dst); // Error here? See https://arduinojson.org/v7/unsupported-set/
3132
// clang-format on
3233
}
3334

@@ -124,8 +125,8 @@ struct Converter<T, detail::enable_if_t<detail::is_floating_point<T>::value>>
124125

125126
template <>
126127
struct Converter<const char*> : private detail::VariantAttorney {
127-
static void toJson(const char* src, JsonVariant dst) {
128-
getVariantImpl(dst).setString(detail::adaptString(src));
128+
static bool toJson(const char* src, JsonVariant dst) {
129+
return getVariantImpl(dst).setString(detail::adaptString(src));
129130
}
130131

131132
static const char* fromJson(JsonVariantConst src) {
@@ -176,8 +177,8 @@ struct Converter<SerializedValue<T>> : private detail::VariantAttorney {
176177

177178
template <>
178179
struct Converter<detail::nullptr_t> : private detail::VariantAttorney {
179-
static void toJson(detail::nullptr_t, JsonVariant dst) {
180-
getVariantImpl(dst).clear();
180+
static bool toJson(detail::nullptr_t, JsonVariant dst) {
181+
return getVariantImpl(dst).clear();
181182
}
182183
static detail::nullptr_t fromJson(JsonVariantConst) {
183184
return nullptr;
@@ -225,17 +226,18 @@ class StringBuilderPrint : public Print {
225226
};
226227
} // namespace detail
227228

228-
inline void convertToJson(const ::Printable& src, JsonVariant dst) {
229+
inline bool convertToJson(const ::Printable& src, JsonVariant dst) {
229230
auto resources = detail::VariantAttorney::getResourceManager(dst);
230231
auto data = detail::VariantAttorney::getData(dst);
231232
if (!resources || !data)
232-
return;
233+
return false;
233234
detail::VariantImpl::clear(data, resources);
234235
detail::StringBuilderPrint print(resources);
235236
src.printTo(print);
236237
if (print.overflowed())
237-
return;
238+
return false;
238239
print.save(data);
240+
return true;
239241
}
240242

241243
#endif
@@ -288,11 +290,11 @@ inline bool canConvertFromJson(JsonVariantConst src, const std::string_view&) {
288290

289291
template <>
290292
struct Converter<JsonArrayConst> : private detail::VariantAttorney {
291-
static void toJson(JsonArrayConst src, JsonVariant dst) {
293+
static bool toJson(JsonArrayConst src, JsonVariant dst) {
292294
if (src.isNull())
293-
dst.set(nullptr);
295+
return dst.set(nullptr);
294296
else
295-
dst.to<JsonArray>().set(src);
297+
return dst.to<JsonArray>().set(src);
296298
}
297299

298300
static JsonArrayConst fromJson(JsonVariantConst src) {
@@ -307,11 +309,11 @@ struct Converter<JsonArrayConst> : private detail::VariantAttorney {
307309

308310
template <>
309311
struct Converter<JsonArray> : private detail::VariantAttorney {
310-
static void toJson(JsonVariantConst src, JsonVariant dst) {
312+
static bool toJson(JsonVariantConst src, JsonVariant dst) {
311313
if (src.isNull())
312-
dst.set(nullptr);
314+
return dst.set(nullptr);
313315
else
314-
dst.to<JsonArray>().set(src);
316+
return dst.to<JsonArray>().set(src);
315317
}
316318

317319
static JsonArray fromJson(JsonVariant src) {
@@ -326,11 +328,11 @@ struct Converter<JsonArray> : private detail::VariantAttorney {
326328

327329
template <>
328330
struct Converter<JsonObjectConst> : private detail::VariantAttorney {
329-
static void toJson(JsonVariantConst src, JsonVariant dst) {
331+
static bool toJson(JsonVariantConst src, JsonVariant dst) {
330332
if (src.isNull())
331-
dst.set(nullptr);
333+
return dst.set(nullptr);
332334
else
333-
dst.to<JsonObject>().set(src);
335+
return dst.to<JsonObject>().set(src);
334336
}
335337

336338
static JsonObjectConst fromJson(JsonVariantConst src) {
@@ -345,11 +347,11 @@ struct Converter<JsonObjectConst> : private detail::VariantAttorney {
345347

346348
template <>
347349
struct Converter<JsonObject> : private detail::VariantAttorney {
348-
static void toJson(JsonVariantConst src, JsonVariant dst) {
350+
static bool toJson(JsonVariantConst src, JsonVariant dst) {
349351
if (src.isNull())
350-
dst.set(nullptr);
352+
return dst.set(nullptr);
351353
else
352-
dst.to<JsonObject>().set(src);
354+
return dst.to<JsonObject>().set(src);
353355
}
354356

355357
static JsonObject fromJson(JsonVariant src) {

src/ArduinoJson/Variant/JsonVariant.hpp

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -47,8 +47,8 @@ bool copyVariant(JsonVariant dst, JsonVariantConst src);
4747

4848
template <>
4949
struct Converter<JsonVariant> : private detail::VariantAttorney {
50-
static void toJson(JsonVariantConst src, JsonVariant dst) {
51-
copyVariant(dst, src);
50+
static bool toJson(JsonVariantConst src, JsonVariant dst) {
51+
return copyVariant(dst, src);
5252
}
5353

5454
static JsonVariant fromJson(JsonVariant src) {
@@ -63,8 +63,8 @@ struct Converter<JsonVariant> : private detail::VariantAttorney {
6363

6464
template <>
6565
struct Converter<JsonVariantConst> : private detail::VariantAttorney {
66-
static void toJson(JsonVariantConst src, JsonVariant dst) {
67-
copyVariant(dst, src);
66+
static bool toJson(JsonVariantConst src, JsonVariant dst) {
67+
return copyVariant(dst, src);
6868
}
6969

7070
static JsonVariantConst fromJson(JsonVariantConst src) {

src/ArduinoJson/Variant/VariantImpl.hpp

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -515,7 +515,7 @@ class VariantImpl {
515515
ARDUINOJSON_ASSERT(resources != nullptr);
516516

517517
if (value.isNull())
518-
return false;
518+
return true; // TODO: should this be moved up to the member function?
519519

520520
if (isTinyString(value, value.size())) {
521521
data->setTinyString(value);
@@ -577,9 +577,11 @@ class VariantImpl {
577577
}
578578

579579
// Release the resources used by this variant and set it to null.
580-
void clear() {
581-
if (data_)
582-
clear(data_, resources_);
580+
bool clear() {
581+
if (!data_)
582+
return false;
583+
clear(data_, resources_);
584+
return true;
583585
}
584586

585587
static void clear(VariantData* data, ResourceManager* resources) {

src/ArduinoJson/Variant/VariantRefBaseImpl.hpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -61,9 +61,9 @@ inline JsonObject VariantRefBase<TDerived>::createNestedObject(
6161
}
6262

6363
template <typename TDerived>
64-
inline void convertToJson(const VariantRefBase<TDerived>& src,
64+
inline bool convertToJson(const VariantRefBase<TDerived>& src,
6565
JsonVariant dst) {
66-
dst.set(src.template as<JsonVariantConst>());
66+
return dst.set(src.template as<JsonVariantConst>());
6767
}
6868

6969
template <typename TDerived>

0 commit comments

Comments
 (0)