Description

JsonDocument::capacity() returns the capacity of the memory pool of the JsonDocument.

Unlike JsonDocument::memoryUsage(), the result of JsonDocument::capacity() doesn’t change during the life of the JsonDocument::memoryUsage().

For StaticJsonDocument, the result is always equal to the capacity set as a template parameter.

For DynamicJsonDocument, the result should be equals to the capacity set in the constructor, but it can be zero if the memory allocation failed.

Use this function to check that the memory allocation succeeded.

Signature

size_t capacity() const;

Example

StaticJsonDocument

StaticJsonDocument<256> doc;
doc.capacity(); // 256

DynamicJsonDocument

DynamicJsonDocument doc(1024);
doc.capacity(); // 1024

Zero when allocation fails

DynamicJsonDocument doc(1000000000);
doc.capacity(); // 0

See also

Global warming stripes by Professor Ed Hawkins (University of Reading)