Updated the examples on wandbox.org

This commit is contained in:
Benoit Blanchon
2019-02-26 10:02:52 +01:00
parent 21998890d4
commit 3530aa88d6
3 changed files with 39 additions and 39 deletions

View File

@ -8,11 +8,11 @@
#include "ArduinoJson.h" #include "ArduinoJson.h"
int main() { int main() {
// The JSON document // Allocate the JSON document
// //
// Inside the brackets, 200 is the RAM allocated to this document. // Inside the brackets, 200 is the RAM allocated to this document.
// Don't forget to change this value to match your requirement. // 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; StaticJsonDocument<200> doc;
// StaticJsonObject allocates memory on the stack, it can be // StaticJsonObject allocates memory on the stack, it can be
@ -20,30 +20,35 @@ int main() {
// //
// DynamicJsonDocument doc(200); // DynamicJsonDocument doc(200);
// Make our document be an object // StaticJsonObject allocates memory on the stack, it can be
JsonObject root = doc.to<JsonObject>(); // replaced by DynamicJsonDocument which allocates in the heap.
// Add values in the object
// //
// Most of the time, you can rely on the implicit casts. // DynamicJsonDocument doc(200);
// In other case, you can do root.set<long>("time", 1351824120);
root["sensor"] = "gps"; // Add values in the document
root["time"] = 1351824120; //
doc["sensor"] = "gps";
doc["time"] = 1351824120;
// Add an array. // Add an array.
// //
JsonArray data = root.createNestedArray("data"); JsonArray data = doc.createNestedArray("data");
data.add(48.756080); data.add(48.756080);
data.add(2.302038); data.add(2.302038);
serializeJson(root, std::cout); // Generate the minified JSON and send it to STDOUT
// This prints: //
serializeJson(doc, std::cout);
// The above line prints:
// {"sensor":"gps","time":1351824120,"data":[48.756080,2.302038]} // {"sensor":"gps","time":1351824120,"data":[48.756080,2.302038]}
// Start a new line
std::cout << std::endl; std::cout << std::endl;
serializeJsonPretty(root, std::cout); // Generate the prettified JSON and send it to STDOUT
// This prints: //
serializeJsonPretty(doc, std::cout);
// The above line prints:
// { // {
// "sensor": "gps", // "sensor": "gps",
// "time": 1351824120, // "time": 1351824120,
@ -52,6 +57,4 @@ int main() {
// 2.302038 // 2.302038
// ] // ]
// } // }
return 0;
} }

View File

@ -8,23 +8,26 @@
#include "ArduinoJson.h" #include "ArduinoJson.h"
int main() { 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. // 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<300> doc;
// StaticJsonDocument<N> allocates memory on the stack, it can be // StaticJsonDocument<N> 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. // JSON input string.
// //
// It's better to use a char[] as shown here. // Using a char[], as shown here, enables the "zero-copy" mode. This mode uses
// If you use a const char* or a String, ArduinoJson will // the minimal amount of memory because the JsonDocument stores pointers to
// have to make a copy of the input in the JsonBuffer. // 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[] = char json[] =
"{\"sensor\":\"gps\",\"time\":1351824120,\"data\":[48.756080,2.302038]}"; "{\"sensor\":\"gps\",\"time\":1351824120,\"data\":[48.756080,2.302038]}";
@ -37,17 +40,14 @@ int main() {
return 1; return 1;
} }
// Get the root object in the document
JsonObject root = doc.as<JsonObject>();
// Fetch values. // Fetch values.
// //
// Most of the time, you can rely on the implicit casts. // Most of the time, you can rely on the implicit casts.
// In other case, you can do doc["time"].as<long>(); // In other case, you can do doc["time"].as<long>();
const char* sensor = root["sensor"]; const char* sensor = doc["sensor"];
long time = root["time"]; long time = doc["time"];
double latitude = root["data"][0]; double latitude = doc["data"][0];
double longitude = root["data"][1]; double longitude = doc["data"][1];
// Print values. // Print values.
std::cout << sensor << std::endl; std::cout << sensor << std::endl;

View File

@ -49,17 +49,14 @@ int main() {
return 1; return 1;
} }
// Get the root object in the document
JsonObject root = doc.as<JsonObject>();
// Fetch values. // Fetch values.
// //
// Most of the time, you can rely on the implicit casts. // Most of the time, you can rely on the implicit casts.
// In other case, you can do doc["time"].as<long>(); // In other case, you can do doc["time"].as<long>();
const char* sensor = root["sensor"]; const char* sensor = doc["sensor"];
long time = root["time"]; long time = doc["time"];
double latitude = root["data"][0]; double latitude = doc["data"][0];
double longitude = root["data"][1]; double longitude = doc["data"][1];
// Print values. // Print values.
std::cout << sensor << std::endl; std::cout << sensor << std::endl;