Skip to content

Commit 296fe79

Browse files
committed
Stop using CollectionIterator in JsonSerializer
This reduces stack consumption and code size. See #2046
1 parent 650d537 commit 296fe79

File tree

3 files changed

+22
-15
lines changed

3 files changed

+22
-15
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ HEAD
55
----
66

77
* Improve error messages when using `char` or `char*` (issue #2043)
8+
* Reduce `serializeJson()`'s size and stack usage (issue #2046)
89

910
v7.0.2 (2024-01-19)
1011
------

src/ArduinoJson/Collection/CollectionData.hpp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -107,6 +107,10 @@ class CollectionData {
107107
return collection->remove(it, resources);
108108
}
109109

110+
SlotId head() const {
111+
return head_;
112+
}
113+
110114
protected:
111115
iterator addSlot(ResourceManager*);
112116

src/ArduinoJson/Json/JsonSerializer.hpp

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

25-
auto it = array.createIterator(resources_);
25+
auto slotId = array.head();
2626

27-
while (!it.done()) {
28-
it->accept(*this);
27+
while (slotId != NULL_SLOT) {
28+
auto slot = resources_->getSlot(slotId);
2929

30-
it.next(resources_);
31-
if (it.done())
32-
break;
30+
slot->data()->accept(*this);
3331

34-
write(',');
32+
slotId = slot->next();
33+
34+
if (slotId != NULL_SLOT)
35+
write(',');
3536
}
3637

3738
write(']');
@@ -41,18 +42,19 @@ class JsonSerializer : public VariantDataVisitor<size_t> {
4142
size_t visit(const ObjectData& object) {
4243
write('{');
4344

44-
auto it = object.createIterator(resources_);
45+
auto slotId = object.head();
46+
47+
while (slotId != NULL_SLOT) {
48+
auto slot = resources_->getSlot(slotId);
4549

46-
while (!it.done()) {
47-
formatter_.writeString(it.key());
50+
formatter_.writeString(slot->key());
4851
write(':');
49-
it->accept(*this);
52+
slot->data()->accept(*this);
5053

51-
it.next(resources_);
52-
if (it.done())
53-
break;
54+
slotId = slot->next();
5455

55-
write(',');
56+
if (slotId != NULL_SLOT)
57+
write(',');
5658
}
5759

5860
write('}');

0 commit comments

Comments
 (0)