From 3530aa88d625262bf0e131d272858641ae4f658a Mon Sep 17 00:00:00 2001 From: Benoit Blanchon Date: Tue, 26 Feb 2019 10:02:52 +0100 Subject: [PATCH] Updated the examples on wandbox.org --- scripts/wandbox/JsonGeneratorExample.cpp | 37 +++++++++++++----------- scripts/wandbox/JsonParserExample.cpp | 30 +++++++++---------- scripts/wandbox/MsgPackParserExample.cpp | 11 +++---- 3 files changed, 39 insertions(+), 39 deletions(-) diff --git a/scripts/wandbox/JsonGeneratorExample.cpp b/scripts/wandbox/JsonGeneratorExample.cpp index b83e5c9c..9220eb83 100644 --- a/scripts/wandbox/JsonGeneratorExample.cpp +++ b/scripts/wandbox/JsonGeneratorExample.cpp @@ -8,11 +8,11 @@ #include "ArduinoJson.h" int main() { - // The JSON document + // Allocate the JSON document // // Inside the brackets, 200 is the RAM allocated to this document. // Don't forget to change this value to match your requirement. - // Use arduinojson.org/assistant to compute the capacity. + // Use arduinojson.org/v6/assistant to compute the capacity. StaticJsonDocument<200> doc; // StaticJsonObject allocates memory on the stack, it can be @@ -20,30 +20,35 @@ int main() { // // DynamicJsonDocument doc(200); - // Make our document be an object - JsonObject root = doc.to(); - - // Add values in the object + // StaticJsonObject allocates memory on the stack, it can be + // replaced by DynamicJsonDocument which allocates in the heap. // - // Most of the time, you can rely on the implicit casts. - // In other case, you can do root.set("time", 1351824120); - root["sensor"] = "gps"; - root["time"] = 1351824120; + // DynamicJsonDocument doc(200); + + // Add values in the document + // + doc["sensor"] = "gps"; + doc["time"] = 1351824120; // Add an array. // - JsonArray data = root.createNestedArray("data"); + JsonArray data = doc.createNestedArray("data"); data.add(48.756080); data.add(2.302038); - serializeJson(root, std::cout); - // This prints: + // Generate the minified JSON and send it to STDOUT + // + serializeJson(doc, std::cout); + // The above line prints: // {"sensor":"gps","time":1351824120,"data":[48.756080,2.302038]} + // Start a new line std::cout << std::endl; - serializeJsonPretty(root, std::cout); - // This prints: + // Generate the prettified JSON and send it to STDOUT + // + serializeJsonPretty(doc, std::cout); + // The above line prints: // { // "sensor": "gps", // "time": 1351824120, @@ -52,6 +57,4 @@ int main() { // 2.302038 // ] // } - - return 0; } diff --git a/scripts/wandbox/JsonParserExample.cpp b/scripts/wandbox/JsonParserExample.cpp index d5581a4a..f5177176 100644 --- a/scripts/wandbox/JsonParserExample.cpp +++ b/scripts/wandbox/JsonParserExample.cpp @@ -8,23 +8,26 @@ #include "ArduinoJson.h" int main() { - // Root JSON object + // Allocate the JSON document // - // Inside the brackets, 300 is the size of the memory pool in bytes. + // Inside the brackets, 200 is the capacity 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. + // Use arduinojson.org/v6/assistant to compute the capacity. StaticJsonDocument<300> doc; // StaticJsonDocument allocates memory on the stack, it can be - // replaced by DynamicJsonObject which allocates in the heap. + // replaced by DynamicJsonDocument which allocates in the heap. // - // DynamicJsonObject doc(200); + // DynamicJsonDocument doc(200); // JSON input string. // - // It's better to use a char[] as shown here. - // If you use a const char* or a String, ArduinoJson will - // have to make a copy of the input in the JsonBuffer. + // Using a char[], as shown here, enables the "zero-copy" mode. This mode uses + // the minimal amount of memory because the JsonDocument stores pointers to + // the input buffer. + // If you use another type of input, ArduinoJson must copy the strings from + // the input to the JsonDocument, so you need to increase the capacity of the + // JsonDocument. char json[] = "{\"sensor\":\"gps\",\"time\":1351824120,\"data\":[48.756080,2.302038]}"; @@ -37,17 +40,14 @@ int main() { return 1; } - // Get the root object in the document - JsonObject root = doc.as(); - // Fetch values. // // Most of the time, you can rely on the implicit casts. // In other case, you can do doc["time"].as(); - const char* sensor = root["sensor"]; - long time = root["time"]; - double latitude = root["data"][0]; - double longitude = root["data"][1]; + const char* sensor = doc["sensor"]; + long time = doc["time"]; + double latitude = doc["data"][0]; + double longitude = doc["data"][1]; // Print values. std::cout << sensor << std::endl; diff --git a/scripts/wandbox/MsgPackParserExample.cpp b/scripts/wandbox/MsgPackParserExample.cpp index fc5a5539..778011db 100644 --- a/scripts/wandbox/MsgPackParserExample.cpp +++ b/scripts/wandbox/MsgPackParserExample.cpp @@ -49,17 +49,14 @@ int main() { return 1; } - // Get the root object in the document - JsonObject root = doc.as(); - // Fetch values. // // Most of the time, you can rely on the implicit casts. // In other case, you can do doc["time"].as(); - const char* sensor = root["sensor"]; - long time = root["time"]; - double latitude = root["data"][0]; - double longitude = root["data"][1]; + const char* sensor = doc["sensor"]; + long time = doc["time"]; + double latitude = doc["data"][0]; + double longitude = doc["data"][1]; // Print values. std::cout << sensor << std::endl;