How to compute the JSON length?
Use measureLength()
to compute the number of characters that will be printed by printTo()
.
Use measurePrettyLength()
to compute the number of characters that will be printed by prettyPrintTo()
.
None of these methods includes the zero-terminator; so, if you need to allocate a buffer, don’t forget to add 1 to the size.
StaticJsonBuffer<200> jsonBuffer;
JsonObject& root = jsonBuffer.createObject();
root["hello"] = world;
size_t len = root.measureLength(); // returns 17
size_t size = len+1;
char json[size];
root.printTo(json, size); // writes {"hello":"world"}