mirror of
https://github.com/bblanchon/ArduinoJson.git
synced 2025-09-26 06:50:56 +02:00
Updated FAQ (markdown)
28
FAQ.md
28
FAQ.md
@@ -13,6 +13,7 @@
|
|||||||
- [How to write a function that works with both `JsonArray` and `JsonObject`?](#how-to-write-a-function-that-works-with-both-jsonarray-and-jsonobject)
|
- [How to write a function that works with both `JsonArray` and `JsonObject`?](#how-to-write-a-function-that-works-with-both-jsonarray-and-jsonobject)
|
||||||
- [How to assign a `JsonArray` or `JsonObject`?](#how-to-assign-a-jsonarray-or-jsonobject)
|
- [How to assign a `JsonArray` or `JsonObject`?](#how-to-assign-a-jsonarray-or-jsonobject)
|
||||||
- [Part 2 - Serialization questions](#part-2---serialization-questions)
|
- [Part 2 - Serialization questions](#part-2---serialization-questions)
|
||||||
|
- [Why some parts are missing?](#why-some-parts-are-missing)
|
||||||
- [How to compute the JSON length?](#how-to-compute-the-json-length)
|
- [How to compute the JSON length?](#how-to-compute-the-json-length)
|
||||||
- [How to create complex nested objects?](#how-to-create-complex-nested-objects)
|
- [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)
|
- [The first serialization succeeds, why do the next ones fail?](#the-first-serialization-succeeds-why-do-the-next-ones-fail)
|
||||||
@@ -182,7 +183,7 @@ See issues [#160](https://github.com/bblanchon/ArduinoJson/issues/160), [#203](h
|
|||||||
|
|
||||||
### How to reuse a `JsonBuffer`?
|
### How to reuse a `JsonBuffer`?
|
||||||
|
|
||||||
`StaticJsonBuffer` and `DynamicJsonBuffer` are designed to be throwaway memory pools, they are not intended to be reused.
|
`StaticJsonBuffer` and `DynamicJsonBuffer` are designed to be throwaway memory pools, they are not intended to be reused.
|
||||||
As a consequence, using a global `JsonBuffer` is almost always a bad idea.
|
As a consequence, using a global `JsonBuffer` is almost always a bad idea.
|
||||||
|
|
||||||
If you believe you need to reuse a `JsonBuffer`, it's because [you're not using the library correctly](https://github.com/bblanchon/ArduinoJson/wiki/FAQ#whats-the-best-way-to-use-the-library).
|
If you believe you need to reuse a `JsonBuffer`, it's because [you're not using the library correctly](https://github.com/bblanchon/ArduinoJson/wiki/FAQ#whats-the-best-way-to-use-the-library).
|
||||||
@@ -193,7 +194,7 @@ Imagine a `clear()` function is available, someone could write:
|
|||||||
|
|
||||||
```c++
|
```c++
|
||||||
JsonObject& myObject = jsonBuffer.createObject();
|
JsonObject& myObject = jsonBuffer.createObject();
|
||||||
jsonBuffer.clear();
|
jsonBuffer.clear();
|
||||||
JsonArray& myArray = jsonBuffer.createArray();
|
JsonArray& myArray = jsonBuffer.createArray();
|
||||||
```
|
```
|
||||||
|
|
||||||
@@ -292,7 +293,7 @@ void myFunction(JsonVariant variant)
|
|||||||
JsonArray& array = variant;
|
JsonArray& array = variant;
|
||||||
// ...
|
// ...
|
||||||
}
|
}
|
||||||
else if (variant.is<JsonObject&>())
|
else if (variant.is<JsonObject&>())
|
||||||
{
|
{
|
||||||
JsonObject& object = variant;
|
JsonObject& object = variant;
|
||||||
// ...
|
// ...
|
||||||
@@ -336,6 +337,21 @@ See issues [#341](https://github.com/bblanchon/ArduinoJson/issues/341) and [#343
|
|||||||
## Part 2 - Serialization questions
|
## Part 2 - Serialization questions
|
||||||
|
|
||||||
|
|
||||||
|
### Why some parts are missing?
|
||||||
|
|
||||||
|
Example: you want to write `{"p1":[0, 1]}`, but instead you get `{"p1":[]}`.
|
||||||
|
|
||||||
|
This is because the `StaticJsonBuffer` too small.
|
||||||
|
|
||||||
|
Solution: Increase the size of the `StaticJsonBuffer` or switch to a `DynamicJsonBuffer`.
|
||||||
|
|
||||||
|
See also:
|
||||||
|
|
||||||
|
* [What are the differences between StaticJsonBuffer and DynamicJsonBuffer?](#what-are-the-differences-between-staticjsonbuffer-and-dynamicjsonbuffer)
|
||||||
|
* [How to determine the buffer size?](#how-to-determine-the-buffer-size)
|
||||||
|
* [JsonBuffer size calculator](https://rawgit.com/bblanchon/ArduinoJson/master/scripts/buffer-size-calculator.html)
|
||||||
|
* Issue [#360](https://github.com/bblanchon/ArduinoJson/issues/360)
|
||||||
|
|
||||||
|
|
||||||
### How to compute the JSON length?
|
### How to compute the JSON length?
|
||||||
|
|
||||||
@@ -731,11 +747,11 @@ But you can enumerate all the key-value pairs in the object, by using iterators:
|
|||||||
char json[] = "{\"key\":\"value\"}";
|
char json[] = "{\"key\":\"value\"}";
|
||||||
JsonObject& object = jsonBuffer.parseObject(json);
|
JsonObject& object = jsonBuffer.parseObject(json);
|
||||||
|
|
||||||
for(JsonObject::iterator it=object.begin(); it!=object.end(); ++it)
|
for(JsonObject::iterator it=object.begin(); it!=object.end(); ++it)
|
||||||
{
|
{
|
||||||
const char* key = it->key;
|
const char* key = it->key;
|
||||||
|
|
||||||
if (it->value.is<char*>()) {
|
if (it->value.is<char*>()) {
|
||||||
const char* value = it->value;
|
const char* value = it->value;
|
||||||
// ...
|
// ...
|
||||||
}
|
}
|
||||||
@@ -776,4 +792,4 @@ ssid = network["ssid"].as<const char*>();
|
|||||||
ssid = network["ssid"].as<String>();
|
ssid = network["ssid"].as<String>();
|
||||||
```
|
```
|
||||||
|
|
||||||
See issue [#118](https://github.com/bblanchon/ArduinoJson/issues/118), [#146](https://github.com/bblanchon/ArduinoJson/issues/146) and [#197](https://github.com/bblanchon/ArduinoJson/issues/197).
|
See issue [#118](https://github.com/bblanchon/ArduinoJson/issues/118), [#146](https://github.com/bblanchon/ArduinoJson/issues/146) and [#197](https://github.com/bblanchon/ArduinoJson/issues/197).
|
||||||
|
Reference in New Issue
Block a user