Don't use JsonBuffer to create or parse objects and arrays.

* Added DynamicJsonArray and StaticJsonArray
* Added DynamicJsonObject and StaticJsonObject
* Added DynamicJsonVariant and StaticJsonVariant
* Added deserializeJson()
* Removed JsonBuffer::parseArray(), parseObject() and parse()
* Removed JsonBuffer::createArray() and createObject()
This commit is contained in:
Benoit Blanchon
2018-02-26 16:05:16 +01:00
parent baf5adcf33
commit 7a2a64803a
89 changed files with 1612 additions and 1691 deletions

View File

@ -32,12 +32,12 @@ void loadConfiguration(const char *filename, Config &config) {
// Allocate the memory pool on the stack.
// Don't forget to change the capacity to match your JSON document.
// Use arduinojson.org/assistant to compute the capacity.
StaticJsonBuffer<512> jsonBuffer;
StaticJsonObject<512> root;
// Parse the root object
JsonObject &root = jsonBuffer.parseObject(file);
bool success = deserializeJson(root, file);
if (!root.success())
if (!success)
Serial.println(F("Failed to read file, using default configuration"));
// Copy values from the JsonObject to the Config
@ -65,10 +65,7 @@ void saveConfiguration(const char *filename, const Config &config) {
// Allocate the memory pool on the stack
// Don't forget to change the capacity to match your JSON document.
// Use https://arduinojson.org/assistant/ to compute the capacity.
StaticJsonBuffer<256> jsonBuffer;
// Parse the root object
JsonObject &root = jsonBuffer.createObject();
StaticJsonObject<256> root;
// Set the values
root["hostname"] = config.hostname;
@ -141,4 +138,4 @@ void loop() {
// The book "Mastering ArduinoJson" contains a case study of a project that has
// a complex configuration with nested members.
// Contrary to this example, the project in the book uses the SPIFFS filesystem.
// Please check it out at: https://arduinojson.org/book/
// Please check it out at: https://arduinojson.org/book/

View File

@ -11,24 +11,17 @@ void setup() {
Serial.begin(9600);
while (!Serial) continue;
// Memory pool for JSON object tree.
// Root JSON object
//
// Inside the brackets, 200 is the size of the pool in bytes.
// Inside the brackets, 200 is the size of the memory pool in bytes.
// Don't forget to change this value to match your JSON document.
// Use arduinojson.org/assistant to compute the capacity.
StaticJsonBuffer<200> jsonBuffer;
StaticJsonObject<200> root;
// StaticJsonBuffer allocates memory on the stack, it can be
// replaced by DynamicJsonBuffer which allocates in the heap.
// StaticJsonObject allocates memory on the stack, it can be
// replaced by DynamicJsonObject which allocates in the heap.
//
// DynamicJsonBuffer jsonBuffer(200);
// Create the root of the object tree.
//
// It's a reference to the JsonObject, the actual bytes are inside the
// JsonBuffer with all the other nodes of the object tree.
// Memory is freed when jsonBuffer goes out of scope.
JsonObject& root = jsonBuffer.createObject();
// DynamicJsonObject root(200);
// Add values in the object
//
@ -78,4 +71,4 @@ void loop() {
// The book "Mastering ArduinoJson" contains a tutorial on serialization.
// It begins with a simple example, like the one above, and then adds more
// features like serializing directly to a file or an HTTP request.
// Please check it out at: https://arduinojson.org/book/
// Please check it out at: https://arduinojson.org/book/

View File

@ -73,10 +73,10 @@ void setup() {
// Allocate JsonBuffer
// Use arduinojson.org/assistant to compute the capacity.
const size_t capacity = JSON_OBJECT_SIZE(3) + JSON_ARRAY_SIZE(2) + 60;
DynamicJsonBuffer jsonBuffer(capacity);
DynamicJsonObject root(capacity);
// Parse JSON object
JsonObject& root = jsonBuffer.parseObject(client);
bool success = deserializeJson(root, client);
if (!root.success()) {
Serial.println(F("Parsing failed!"));
return;
@ -109,4 +109,4 @@ void loop() {
// showing how to parse the response from Yahoo Weather. In the last chapter,
// it shows how to parse the huge documents from OpenWeatherMap
// and Weather Underground.
// Please check it out at: https://arduinojson.org/book/
// Please check it out at: https://arduinojson.org/book/

View File

@ -11,17 +11,17 @@ void setup() {
Serial.begin(9600);
while (!Serial) continue;
// Memory pool for JSON object tree.
// Root JSON object
//
// Inside the brackets, 200 is the size of the pool in bytes.
// Inside the brackets, 200 is the size of the memory pool in bytes.
// Don't forget to change this value to match your JSON document.
// Use arduinojson.org/assistant to compute the capacity.
StaticJsonBuffer<200> jsonBuffer;
StaticJsonObject<200> root;
// StaticJsonBuffer allocates memory on the stack, it can be
// replaced by DynamicJsonBuffer which allocates in the heap.
// StaticJsonObject allocates memory on the stack, it can be
// replaced by DynamicJsonObject which allocates in the heap.
//
// DynamicJsonBuffer jsonBuffer(200);
// DynamicJsonObject root(200);
// JSON input string.
//
@ -36,10 +36,10 @@ void setup() {
// It's a reference to the JsonObject, the actual bytes are inside the
// JsonBuffer with all the other nodes of the object tree.
// Memory is freed when jsonBuffer goes out of scope.
JsonObject& root = jsonBuffer.parseObject(json);
bool success = deserializeJson(root, json);
// Test if parsing succeeds.
if (!root.success()) {
if (!success) {
Serial.println("parseObject() failed");
return;
}
@ -75,4 +75,4 @@ void loop() {
// The book "Mastering ArduinoJson" contains a tutorial on deserialization.
// It begins with a simple example, like the one above, and then adds more
// features like deserializing directly from a file or an HTTP request.
// Please check it out at: https://arduinojson.org/book/
// Please check it out at: https://arduinojson.org/book/

View File

@ -51,12 +51,9 @@ void loop() {
// Read the request (we ignore the content in this example)
while (client.available()) client.read();
// Allocate JsonBuffer
// Allocate the root JsonObject
// Use arduinojson.org/assistant to compute the capacity.
StaticJsonBuffer<500> jsonBuffer;
// Create the root object
JsonObject& root = jsonBuffer.createObject();
StaticJsonObject<500> root;
// Create the "analog" array
JsonArray& analogValues = root.createNestedArray("analog");
@ -106,4 +103,4 @@ void loop() {
// The book "Mastering ArduinoJson" contains a tutorial on serialization.
// It begins with a simple example, then adds more features like serializing
// directly to a file or an HTTP client.
// Please check it out at: https://arduinojson.org/book/
// Please check it out at: https://arduinojson.org/book/

View File

@ -43,12 +43,9 @@ void setup() {
}
void loop() {
// Allocate JsonBuffer
// Allocate the root JsonObject
// Use arduinojson.org/assistant to compute the capacity.
StaticJsonBuffer<500> jsonBuffer;
// Create the root object
JsonObject& root = jsonBuffer.createObject();
StaticJsonObject<500> root;
// Create the "analog" array
JsonArray& analogValues = root.createNestedArray("analog");
@ -98,4 +95,4 @@ void loop() {
// The book "Mastering ArduinoJson" contains a tutorial on serialization.
// It begins with a simple example, then adds more features like serializing
// directly to a file or any stream.
// Please check it out at: https://arduinojson.org/book/
// Please check it out at: https://arduinojson.org/book/

View File

@ -14,14 +14,13 @@
void setup() {
#ifdef PROGMEM // <- check that Flash strings are supported
DynamicJsonBuffer jsonBuffer;
DynamicJsonObject root;
// You can use a Flash String as your JSON input.
// WARNING: the content of the Flash String will be duplicated in the
// JsonBuffer.
JsonObject& root =
jsonBuffer.parseObject(F("{\"sensor\":\"gps\",\"time\":1351824120,"
"\"data\":[48.756080,2.302038]}"));
deserializeJson(root, F("{\"sensor\":\"gps\",\"time\":1351824120,"
"\"data\":[48.756080,2.302038]}"));
// You can use a Flash String to get an element of a JsonObject
// No duplication is done.
@ -67,4 +66,4 @@ void loop() {
// The book "Mastering ArduinoJson" contains a quick C++ course that explains
// how your microcontroller stores strings in memory. It also tells why you
// should not abuse Flash strings with ArduinoJson.
// Please check it out at: https://arduinojson.org/book/
// Please check it out at: https://arduinojson.org/book/

View File

@ -11,13 +11,13 @@
#include <ArduinoJson.h>
void setup() {
DynamicJsonBuffer jsonBuffer;
DynamicJsonObject root;
// You can use a String as your JSON input.
// WARNING: the content of the String will be duplicated in the JsonBuffer.
String input =
"{\"sensor\":\"gps\",\"time\":1351824120,\"data\":[48.756080,2.302038]}";
JsonObject& root = jsonBuffer.parseObject(input);
deserializeJson(root, input);
// You can use a String to get an element of a JsonObject
// No duplication is done.
@ -71,4 +71,4 @@ void loop() {
// The book "Mastering ArduinoJson" contains a quick C++ course that explains
// how your microcontroller stores strings in memory. On several occasions, it
// shows how you can avoid String in your program.
// Please check it out at: https://arduinojson.org/book/
// Please check it out at: https://arduinojson.org/book/