Skip to content

Commit e86c6ac

Browse files
committed
Return bool from all built-in toJson converters
1 parent 0021ec8 commit e86c6ac

File tree

8 files changed

+134
-123
lines changed

8 files changed

+134
-123
lines changed

src/ArduinoJson/Document/JsonDocument.hpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -412,8 +412,8 @@ class JsonDocument : public detail::VariantOperators<const JsonDocument&> {
412412
mutable detail::VariantData data_;
413413
};
414414

415-
inline void convertToJson(const JsonDocument& src, JsonVariant dst) {
416-
dst.set(src.as<JsonVariantConst>());
415+
inline bool convertToJson(const JsonDocument& src, JsonVariant dst) {
416+
return dst.set(src.as<JsonVariantConst>());
417417
}
418418

419419
ARDUINOJSON_END_PUBLIC_NAMESPACE

src/ArduinoJson/MsgPack/MsgPackBinary.hpp

Lines changed: 37 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -24,46 +24,46 @@ 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 impl = getImpl(dst);
29-
auto data = impl.data();
30-
if (!data)
31-
return;
29+
if (impl.isUnbound())
30+
return false;
31+
32+
if (!src.data())
33+
return true;
34+
35+
size_t headerSize = src.size() >= 0x10000 ? 5 : src.size() >= 0x100 ? 3 : 2;
36+
3237
auto resources = impl.resources();
33-
impl.clear();
34-
if (src.data()) {
35-
size_t headerSize = src.size() >= 0x10000 ? 5
36-
: src.size() >= 0x100 ? 3
37-
: 2;
38-
auto str = resources->createString(src.size() + headerSize);
39-
if (str) {
40-
resources->saveString(str);
41-
auto ptr = reinterpret_cast<uint8_t*>(str->data);
42-
switch (headerSize) {
43-
case 2:
44-
ptr[0] = uint8_t(0xc4);
45-
ptr[1] = uint8_t(src.size() & 0xff);
46-
break;
47-
case 3:
48-
ptr[0] = uint8_t(0xc5);
49-
ptr[1] = uint8_t(src.size() >> 8 & 0xff);
50-
ptr[2] = uint8_t(src.size() & 0xff);
51-
break;
52-
case 5:
53-
ptr[0] = uint8_t(0xc6);
54-
ptr[1] = uint8_t(src.size() >> 24 & 0xff);
55-
ptr[2] = uint8_t(src.size() >> 16 & 0xff);
56-
ptr[3] = uint8_t(src.size() >> 8 & 0xff);
57-
ptr[4] = uint8_t(src.size() & 0xff);
58-
break;
59-
default:
60-
ARDUINOJSON_ASSERT(false);
61-
}
62-
memcpy(ptr + headerSize, src.data(), src.size());
63-
data->setRawString(str);
64-
return;
65-
}
38+
auto str = resources->createString(src.size() + headerSize);
39+
if (!str)
40+
return false;
41+
resources->saveString(str);
42+
43+
auto ptr = reinterpret_cast<uint8_t*>(str->data);
44+
switch (headerSize) {
45+
case 2:
46+
ptr[0] = uint8_t(0xc4);
47+
ptr[1] = uint8_t(src.size() & 0xff);
48+
break;
49+
case 3:
50+
ptr[0] = uint8_t(0xc5);
51+
ptr[1] = uint8_t(src.size() >> 8 & 0xff);
52+
ptr[2] = uint8_t(src.size() & 0xff);
53+
break;
54+
case 5:
55+
ptr[0] = uint8_t(0xc6);
56+
ptr[1] = uint8_t(src.size() >> 24 & 0xff);
57+
ptr[2] = uint8_t(src.size() >> 16 & 0xff);
58+
ptr[3] = uint8_t(src.size() >> 8 & 0xff);
59+
ptr[4] = uint8_t(src.size() & 0xff);
60+
break;
61+
default:
62+
ARDUINOJSON_ASSERT(false);
6663
}
64+
memcpy(ptr + headerSize, src.data(), src.size());
65+
impl.data()->setRawString(str);
66+
return true;
6767
}
6868

6969
static MsgPackBinary fromJson(JsonVariantConst src) {

src/ArduinoJson/MsgPack/MsgPackExtension.hpp

Lines changed: 48 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -30,54 +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 impl = getImpl(dst);
35-
auto data = impl.data();
36-
auto resources = impl.resources();
37-
if (!data)
38-
return;
35+
if (impl.isUnbound())
36+
return false;
37+
3938
impl.clear();
40-
if (src.data()) {
41-
uint8_t format, sizeBytes;
42-
if (src.size() >= 0x10000) {
43-
format = 0xc9; // ext 32
44-
sizeBytes = 4;
45-
} else if (src.size() >= 0x100) {
46-
format = 0xc8; // ext 16
47-
sizeBytes = 2;
48-
} else if (src.size() == 16) {
49-
format = 0xd8; // fixext 16
50-
sizeBytes = 0;
51-
} else if (src.size() == 8) {
52-
format = 0xd7; // fixext 8
53-
sizeBytes = 0;
54-
} else if (src.size() == 4) {
55-
format = 0xd6; // fixext 4
56-
sizeBytes = 0;
57-
} else if (src.size() == 2) {
58-
format = 0xd5; // fixext 2
59-
sizeBytes = 0;
60-
} else if (src.size() == 1) {
61-
format = 0xd4; // fixext 1
62-
sizeBytes = 0;
63-
} else {
64-
format = 0xc7; // ext 8
65-
sizeBytes = 1;
66-
}
67-
68-
auto str = resources->createString(src.size() + 2 + sizeBytes);
69-
if (str) {
70-
resources->saveString(str);
71-
auto ptr = reinterpret_cast<uint8_t*>(str->data);
72-
*ptr++ = uint8_t(format);
73-
for (uint8_t i = 0; i < sizeBytes; i++)
74-
*ptr++ = uint8_t(src.size() >> (sizeBytes - i - 1) * 8 & 0xff);
75-
*ptr++ = uint8_t(src.type());
76-
memcpy(ptr, src.data(), src.size());
77-
data->setRawString(str);
78-
return;
79-
}
39+
40+
if (!src.data())
41+
return true;
42+
43+
uint8_t format, sizeBytes;
44+
if (src.size() >= 0x10000) {
45+
format = 0xc9; // ext 32
46+
sizeBytes = 4;
47+
} else if (src.size() >= 0x100) {
48+
format = 0xc8; // ext 16
49+
sizeBytes = 2;
50+
} else if (src.size() == 16) {
51+
format = 0xd8; // fixext 16
52+
sizeBytes = 0;
53+
} else if (src.size() == 8) {
54+
format = 0xd7; // fixext 8
55+
sizeBytes = 0;
56+
} else if (src.size() == 4) {
57+
format = 0xd6; // fixext 4
58+
sizeBytes = 0;
59+
} else if (src.size() == 2) {
60+
format = 0xd5; // fixext 2
61+
sizeBytes = 0;
62+
} else if (src.size() == 1) {
63+
format = 0xd4; // fixext 1
64+
sizeBytes = 0;
65+
} else {
66+
format = 0xc7; // ext 8
67+
sizeBytes = 1;
8068
}
69+
70+
auto resources = impl.resources();
71+
auto str = resources->createString(src.size() + 2 + sizeBytes);
72+
if (!str)
73+
return false;
74+
resources->saveString(str);
75+
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+
impl.data()->setRawString(str);
83+
return true;
8184
}
8285

8386
static MsgPackExtension fromJson(JsonVariantConst src) {

src/ArduinoJson/Object/ObjectImpl.hpp

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,9 @@ inline VariantData* VariantImpl::addMember(TAdaptedString key) {
5555
if (!isObject())
5656
return nullptr;
5757

58+
if (key.isNull())
59+
return nullptr; // Ignore null key
60+
5861
auto keySlot = allocVariant();
5962
if (!keySlot)
6063
return nullptr;

src/ArduinoJson/Variant/ConverterImpl.hpp

Lines changed: 37 additions & 32 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

@@ -60,9 +61,9 @@ struct Converter<T, detail::enable_if_t<detail::is_integral<T>::value &&
6061
: private detail::VariantAttorney {
6162
static bool toJson(T src, JsonVariant dst) {
6263
ARDUINOJSON_ASSERT_INTEGER_TYPE_IS_SUPPORTED(T);
63-
auto variant = getImpl(dst);
64-
variant.clear();
65-
return variant.setInteger(src);
64+
auto impl = getImpl(dst);
65+
impl.clear();
66+
return impl.setInteger(src);
6667
}
6768

6869
static T fromJson(JsonVariantConst src) {
@@ -128,10 +129,10 @@ struct Converter<T, detail::enable_if_t<detail::is_floating_point<T>::value>>
128129

129130
template <>
130131
struct Converter<const char*> : private detail::VariantAttorney {
131-
static void toJson(const char* src, JsonVariant dst) {
132+
static bool toJson(const char* src, JsonVariant dst) {
132133
auto impl = getImpl(dst);
133134
impl.clear();
134-
impl.setString(detail::adaptString(src));
135+
return impl.setString(detail::adaptString(src));
135136
}
136137

137138
static const char* fromJson(JsonVariantConst src) {
@@ -145,10 +146,10 @@ struct Converter<const char*> : private detail::VariantAttorney {
145146

146147
template <>
147148
struct Converter<JsonString> : private detail::VariantAttorney {
148-
static void toJson(JsonString src, JsonVariant dst) {
149+
static bool toJson(JsonString src, JsonVariant dst) {
149150
auto impl = getImpl(dst);
150151
impl.clear();
151-
impl.setString(detail::adaptString(src));
152+
return impl.setString(detail::adaptString(src));
152153
}
153154

154155
static JsonString fromJson(JsonVariantConst src) {
@@ -161,11 +162,11 @@ struct Converter<JsonString> : private detail::VariantAttorney {
161162
};
162163

163164
template <typename T>
164-
inline detail::enable_if_t<detail::IsString<T>::value> convertToJson(
165+
inline detail::enable_if_t<detail::IsString<T>::value, bool> convertToJson(
165166
const T& src, JsonVariant dst) {
166-
auto variant = detail::VariantAttorney::getImpl(dst);
167-
variant.clear();
168-
variant.setString(detail::adaptString(src));
167+
auto impl = detail::VariantAttorney::getImpl(dst);
168+
impl.clear();
169+
return impl.setString(detail::adaptString(src));
169170
}
170171

171172
// SerializedValue<std::string>
@@ -174,16 +175,19 @@ inline detail::enable_if_t<detail::IsString<T>::value> convertToJson(
174175
template <typename T>
175176
struct Converter<SerializedValue<T>> : private detail::VariantAttorney {
176177
static bool toJson(SerializedValue<T> src, JsonVariant dst) {
177-
auto variant = getImpl(dst);
178-
variant.clear();
179-
return variant.setRawString(detail::adaptString(src.data(), src.size()));
178+
auto impl = getImpl(dst);
179+
impl.clear();
180+
return impl.setRawString(detail::adaptString(src.data(), src.size()));
180181
}
181182
};
182183

183184
template <>
184185
struct Converter<detail::nullptr_t> : private detail::VariantAttorney {
185-
static void toJson(detail::nullptr_t, JsonVariant dst) {
186+
static bool toJson(detail::nullptr_t, JsonVariant dst) {
187+
if (dst.isUnbound())
188+
return false;
186189
getImpl(dst).clear();
190+
return true;
187191
}
188192
static detail::nullptr_t fromJson(JsonVariantConst) {
189193
return nullptr;
@@ -230,16 +234,17 @@ class StringBuilderPrint : public Print {
230234
};
231235
} // namespace detail
232236

233-
inline void convertToJson(const ::Printable& src, JsonVariant dst) {
237+
inline bool convertToJson(const ::Printable& src, JsonVariant dst) {
234238
auto impl = detail::VariantAttorney::getImpl(dst);
235239
if (impl.isUnbound())
236-
return;
240+
return false;
237241
impl.clear();
238242
detail::StringBuilderPrint print(impl.resources());
239243
src.printTo(print);
240244
if (print.overflowed())
241-
return;
245+
return false;
242246
print.save(impl.data());
247+
return true;
243248
}
244249

245250
#endif
@@ -292,11 +297,11 @@ inline bool canConvertFromJson(JsonVariantConst src, const std::string_view&) {
292297

293298
template <>
294299
struct Converter<JsonArrayConst> : private detail::VariantAttorney {
295-
static void toJson(JsonArrayConst src, JsonVariant dst) {
300+
static bool toJson(JsonArrayConst src, JsonVariant dst) {
296301
if (src.isNull())
297-
dst.set(nullptr);
302+
return dst.set(nullptr);
298303
else
299-
dst.to<JsonArray>().set(src);
304+
return dst.to<JsonArray>().set(src);
300305
}
301306

302307
static JsonArrayConst fromJson(JsonVariantConst src) {
@@ -310,11 +315,11 @@ struct Converter<JsonArrayConst> : private detail::VariantAttorney {
310315

311316
template <>
312317
struct Converter<JsonArray> : private detail::VariantAttorney {
313-
static void toJson(JsonVariantConst src, JsonVariant dst) {
318+
static bool toJson(JsonVariantConst src, JsonVariant dst) {
314319
if (src.isNull())
315-
dst.set(nullptr);
320+
return dst.set(nullptr);
316321
else
317-
dst.to<JsonArray>().set(src);
322+
return dst.to<JsonArray>().set(src);
318323
}
319324

320325
static JsonArray fromJson(JsonVariant src) {
@@ -328,11 +333,11 @@ struct Converter<JsonArray> : private detail::VariantAttorney {
328333

329334
template <>
330335
struct Converter<JsonObjectConst> : private detail::VariantAttorney {
331-
static void toJson(JsonVariantConst src, JsonVariant dst) {
336+
static bool toJson(JsonVariantConst src, JsonVariant dst) {
332337
if (src.isNull())
333-
dst.set(nullptr);
338+
return dst.set(nullptr);
334339
else
335-
dst.to<JsonObject>().set(src);
340+
return dst.to<JsonObject>().set(src);
336341
}
337342

338343
static JsonObjectConst fromJson(JsonVariantConst src) {
@@ -346,11 +351,11 @@ struct Converter<JsonObjectConst> : private detail::VariantAttorney {
346351

347352
template <>
348353
struct Converter<JsonObject> : private detail::VariantAttorney {
349-
static void toJson(JsonVariantConst src, JsonVariant dst) {
354+
static bool toJson(JsonVariantConst src, JsonVariant dst) {
350355
if (src.isNull())
351-
dst.set(nullptr);
356+
return dst.set(nullptr);
352357
else
353-
dst.to<JsonObject>().set(src);
358+
return dst.to<JsonObject>().set(src);
354359
}
355360

356361
static JsonObject fromJson(JsonVariant src) {

0 commit comments

Comments
 (0)