mirror of
https://github.com/bblanchon/ArduinoJson.git
synced 2025-09-26 06:50:56 +02:00
Updated FAQ (markdown)
27
FAQ.md
27
FAQ.md
@@ -345,6 +345,33 @@ Reusable | no<sup>(3)</sup> | no<sup>(3)</sup>
|
|||||||
|
|
||||||
As a general rule, if your `StaticJsonBuffer` is bigger than 2K, then it may be a good time to switch to a `DynamicJsonBuffer`.
|
As a general rule, if your `StaticJsonBuffer` is bigger than 2K, then it may be a good time to switch to a `DynamicJsonBuffer`.
|
||||||
|
|
||||||
|
### How to determine the buffer size?
|
||||||
|
|
||||||
|
There are basically tree approaches here:
|
||||||
|
|
||||||
|
1. either you can predict the content of the object tree,
|
||||||
|
2. you know how much memory is available.
|
||||||
|
3. you try and look at current size
|
||||||
|
|
||||||
|
In the first case, you know some constraints on the object tree. For instance, let's say that you know in advance (and by that I mean "at compilation time") that you want to generate an object with 3 values, one of them being an array with 2 values, like the following:
|
||||||
|
|
||||||
|
{"sensor":"gps","time":1351824120,"data":[48.756080,2.302038]}
|
||||||
|
|
||||||
|
To determine the memory usage of this object tree, you use the two macros `JSON_ARRAY_SIZE(n)` and `JSON_OBJECT_SIZE(n)`, both take the number of elements as an argument.
|
||||||
|
For the example above, it would be:
|
||||||
|
|
||||||
|
const int BUFFER_SIZE = JSON_OBJECT_SIZE(3) + JSON_ARRAY_SIZE(2);
|
||||||
|
StaticJsonBuffer<BUFFER_SIZE> jsonBuffer;
|
||||||
|
|
||||||
|
In the second case, let's say you dynamically generate a JSON object tree of a random complexity so you can't put a limit based on that. But on the other hand, you don't want your program to crash because the object tree doesn't fit in memory.
|
||||||
|
The solution here is to determine how much memory is available, or in other words how much memory you can afford for the JSON object tree.
|
||||||
|
|
||||||
|
The third solution is to run your program an print `jsonBuffer.size()` to get the current size of the buffer.
|
||||||
|
|
||||||
|
**WARNING**: if you use `String` to create your JSON keys or values, there content will automatically be duplicated in the `JsonBuffer`, so you need to add the total length of all strings in the size of the `JsonBuffer`.
|
||||||
|
|
||||||
|
See issue [#243](https://github.com/bblanchon/ArduinoJson/issues/243#issuecomment-196553398)
|
||||||
|
|
||||||
### I found a memory leak in the library!
|
### I found a memory leak in the library!
|
||||||
|
|
||||||
This is very unlikely. You're probably using the library incorrectly.
|
This is very unlikely. You're probably using the library incorrectly.
|
||||||
|
Reference in New Issue
Block a user