Skip to content

Commit 8ff4dd9

Browse files
committed
JsonSerializer: use iterators to serialize array and objects
1 parent 75d6af2 commit 8ff4dd9

File tree

1 file changed

+16
-20
lines changed

1 file changed

+16
-20
lines changed

src/ArduinoJson/Json/JsonSerializer.hpp

Lines changed: 16 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -22,17 +22,14 @@ class JsonSerializer : public VariantDataVisitor<size_t> {
2222
size_t visitArray(const VariantImpl& array) {
2323
write('[');
2424

25-
auto slotId = array.head();
25+
bool first = true;
2626

27-
while (slotId != NULL_SLOT) {
28-
auto slot = resources_->getVariant(slotId);
29-
30-
VariantImpl(slot, resources_).accept(*this);
31-
32-
slotId = slot->next;
33-
34-
if (slotId != NULL_SLOT)
27+
for (auto it = array.createIterator(); !it.done(); it.move()) {
28+
if (!first)
3529
write(',');
30+
31+
it->accept(*this);
32+
first = false;
3633
}
3734

3835
write(']');
@@ -42,20 +39,19 @@ class JsonSerializer : public VariantDataVisitor<size_t> {
4239
size_t visitObject(const VariantImpl& object) {
4340
write('{');
4441

45-
auto slotId = object.head();
42+
bool first = true;
43+
bool isValue = false;
4644

47-
bool isKey = true;
48-
49-
while (slotId != NULL_SLOT) {
50-
auto slot = resources_->getVariant(slotId);
51-
VariantImpl(slot, resources_).accept(*this);
52-
53-
slotId = slot->next;
45+
for (auto it = object.createIterator(); !it.done(); it.move()) {
46+
if (isValue)
47+
write(':');
48+
else if (!first)
49+
write(',');
5450

55-
if (slotId != NULL_SLOT)
56-
write(isKey ? ':' : ',');
51+
it->accept(*this);
5752

58-
isKey = !isKey;
53+
first = false;
54+
isValue = !isValue;
5955
}
6056

6157
write('}');

0 commit comments

Comments
 (0)