From 84b98c362a04bfc4c776a42147d75662a14601c1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Beno=C3=AEt=20Blanchon?= Date: Thu, 28 Apr 2016 08:26:31 +0200 Subject: [PATCH] Updated FAQ (markdown) --- FAQ.md | 29 +++++++++++++++++++++++++++-- 1 file changed, 27 insertions(+), 2 deletions(-) diff --git a/FAQ.md b/FAQ.md index 05339f2..00b659d 100644 --- a/FAQ.md +++ b/FAQ.md @@ -10,7 +10,8 @@ * [What's the best way to use the library?](#whats-the-best-way-to-use-the-library) * [How to write a function that works with both `JsonArray` and `JsonObject`?](#how-to-write-a-function-that-works-with-both-jsonarray-and-jsonobject) * [Part 2 - Serialization questions](#part-2---serialization-questions) - * [How do I create complex nested objects?](#how-do-i-create-complex-nested-objects) + * [How to compute the JSON length?](#how-to-compute-the-JSON-length) + * [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) * [Part 3 - Deserialization questions](#part-3---deserializationparsing-questions) * [Can I parse data from a stream?](#can-i-parse-data-from-a-stream) @@ -282,7 +283,31 @@ See issue [#195](https://github.com/bblanchon/ArduinoJson/issues/195) and [#196] -### How do I create complex nested objects? +### 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 include the zero terminator. +So if you need to allocate a buffer, don't forget to add 1 to the size. + +```c++ +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"} +``` + +See issues [#75](https://github.com/bblanchon/ArduinoJson/issues/75), [#94](https://github.com/bblanchon/ArduinoJson/issues/94), [#166](https://github.com/bblanchon/ArduinoJson/issues/166), [#178](https://github.com/bblanchon/ArduinoJson/issues/178) and [#268](https://github.com/bblanchon/ArduinoJson/issues/268). + + +### How to create complex nested objects? To create a nested object, call `createNestedObject()`. To create a nested array, call `createNestedArray()`.