Updated FAQ (markdown)

Benoît Blanchon
2016-10-09 09:30:38 +02:00
parent 8d52b43204
commit 4cba328185

40
FAQ.md

@@ -14,6 +14,7 @@
- [How to assign a `JsonArray` or `JsonObject`?](#how-to-assign-a-jsonarray-or-jsonobject)
- [Part 2 - Serialization questions](#part-2---serialization-questions)
- [Why some parts are missing?](#why-some-parts-are-missing)
- [Why does the generated JSON contains garbage?](#why-does-the-generated-json-contains-garbage)
- [How to compute the JSON length?](#how-to-compute-the-json-length)
- [How to create complex nested objects?](#how-to-create-complex-nested-objects)
- [The first serialization succeeds, why do the next ones fail?](#the-first-serialization-succeeds-why-do-the-next-ones-fail)
@@ -353,6 +354,45 @@ See also:
* Issue [#360](https://github.com/bblanchon/ArduinoJson/issues/360)
### Why does the generated JSON contains garbage?
There are two key concepts you need to understand to use ArduinoJson:
1. `JsonObject`s and `JsonArray`s are stored in a `JsonBuffer`
2. `char*` are not copied
Similarly, there are two reasons to get garbage in the output:
1. `JsonBuffer` is not in memory anymore
2. The string pointed by the `char*` is not in memory anymore.
##### Example 1 of what you should not do:
```c++
JsonObject& myFunction() {
StaticJsonBuffer<200> jsonBuffer;
return jsonBuffer.createObject();
}
```
This is wrong because it returns a reference (a pointer if you prefer) to a `JsonObject` that is not in memory anymore.
##### Example 2 of what you should not do:
```c++
void myFunction(JsonObject& obj) {
char myValue[] = "Hello World!";
obj["value"] = myValue;
}
```
This is wrong because the `JsonObject` now contains a pointer to a string that is not in memory anymore.
See also:
* [Avoiding pitfalls: Do not assume that strings are copied](https://github.com/bblanchon/ArduinoJson/wiki/Avoiding-pitfalls#6-do-not-assume-that-strings-are-copied)
* Issues [#364](https://github.com/bblanchon/ArduinoJson/issues/364), [#366](https://github.com/bblanchon/ArduinoJson/issues/366)
### How to compute the JSON length?
Use `measureLength()` to compute the number of characters that will be printed by `printTo()`.