8000 Added a note to avoid issue #10 · MacroBull/ArduinoJson@7246db7 · GitHub
[go: up one dir, main page]

Skip to content

Commit 7246db7

Browse files
committed
Added a note to avoid issue bblanchon#10
1 parent 4bdbc6c commit 7246db7

File tree

1 file changed

+27
-0
lines changed

1 file changed

+27
-0
lines changed

JsonGenerator/README.md

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -101,6 +101,33 @@ or
101101
JsonObject<8> nestedObject;
102102
array.add(nestedObject);
103103

104+
> ##### CAUTION! Nested objects must be in memory
105+
> Calling `add()` makes the `JsonArray` store a pointer to the nested object.
106+
> This is designed to avoid memory duplication.
107+
> But it can only work if the object is in memory when `printTo()` is executed.
108+
> For instance, don't do this:
109+
>
110+
> void addNestedObject()
111+
> {
112+
> JsonObject<2> nestedObject;
113+
> // ...
114+
> array.add(nestedObject); // <- DON'T !!
115+
>
116+
> // array now contains a pointer to a local variable that will be
117+
> // discarded as soon as the function exits
118+
> }
119+
>
120+
> For the same reason, don't do this either:
121+
>
122+
> for( int i=0; i<100; i++)
123+
> {
124+
> JsonObject<2> nestedObject;
125+
> // ...
126+
> array.add(nestedObject); // <- DON'T !!
127+
> }
128+
> // array now contains 100 pointers to the same a local variable
129+
> // that is out of the scope anyway
130+
104131
#### JSON Object
105132

106133
You create a JSON object (ie hash-table/dictionary) with the following line:

0 commit comments

Comments
 (0)
0