StaticJsonDocument
Description
StaticJsonDocument
is a JsonDocument
that allocates its memory pool in-place, so it doesn’t rely on dynamic memory allocation.
Because it doesn’t call malloc()
and free()
, StaticJsonDocument
is slightly faster than DynamicJsonDocument
.
If you declare a local variable of type StaticJsonDocument
, it allocates the memory pool in the stack memory. Beware not to allocate a memory pool too large in the stack because it would cause a stack overflow. Use StaticJsonDocument
for small documents (below 1KB) and switch to a DynamicJsonDocument
if it’s too large to fit in the stack memory.
Member functions
as<T>()
casts the root to the specified type (e.g.JsonArray
orJsonObject
)add()
adds elements to the root arraycapacity()
returns the capacity of the memory poolclear()
empties the document and resets the memory poolcontainsKey()
tests if the root object contains the specified keycreateNestedArray()
creates a nested array attached to the rootcreateNestedObject()
create a nested object attached to the rootgarbageCollect()
reclaims leaked memory blocksoperator[]
gets or sets values in the documentoverflowed()
tells if the memory pool was large enoughis<T>()
tests the type of the rootisNull()
tells if the document is null or emptymemoryUsage()
tells how many bytes are used in the memory poolnesting()
returns the number of nesting layers in the documentremove()
removes an element (or member) at the specified index (or key)set()
replaces the root with the specified valuesize()
returns the number of elements (or members) that the root array (or object) containsto<T>()
clears the document and converts it to the specified type (e.g.JsonArray
orJsonObject
)
Example
Here is a program that deserializes a JSON document using a StaticJsonDocument
StaticJsonDocument<200> doc; // <- a little more than 200 bytes in the stack
char json[] = "{\"hello\":\"world\"}";
deserializeJson(doc, json);
const char* world = doc["hello"];