diff --git a/examples/JsonGeneratorExample/JsonGeneratorExample.ino b/examples/JsonGeneratorExample/JsonGeneratorExample.ino index abf0ea95..717a2276 100644 --- a/examples/JsonGeneratorExample/JsonGeneratorExample.ino +++ b/examples/JsonGeneratorExample/JsonGeneratorExample.ino @@ -13,12 +13,36 @@ void setup() { // wait serial port initialization } + // Memory pool for JSON object tree. + // + // Inside the brackets, 200 is the size of the pool in bytes. + // If the JSON object is more complex, you need to increase that value. StaticJsonBuffer<200> jsonBuffer; + // StaticJsonBuffer allocates memory on the stack, it can be + // replaced by DynamicJsonBuffer which allocates in the heap. + // It's simpler but less efficient. + // + // DynamicJsonBuffer jsonBuffer; + + // 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(); + + // Add values in the object + // + // 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; + // Add a nested array. + // + // It's also possible to create the array separately and add it to the + // JsonObject but it's less efficient. JsonArray& data = root.createNestedArray("data"); data.add(double_with_n_digits(48.756080, 6)); data.add(double_with_n_digits(2.302038, 6)); diff --git a/examples/JsonParserExample/JsonParserExample.ino b/examples/JsonParserExample/JsonParserExample.ino index fad4a5ad..13536019 100644 --- a/examples/JsonParserExample/JsonParserExample.ino +++ b/examples/JsonParserExample/JsonParserExample.ino @@ -13,23 +13,49 @@ void setup() { // wait serial port initialization } + // Memory pool for JSON object tree. + // + // Inside the brackets, 200 is the size of the pool in bytes, + // If the JSON object is more complex, you need to increase that value. StaticJsonBuffer<200> jsonBuffer; + // StaticJsonBuffer allocates memory on the stack, it can be + // replaced by DynamicJsonBuffer which allocates in the heap. + // It's simpler but less efficient. + // + // DynamicJsonBuffer jsonBuffer; + + // 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. char json[] = "{\"sensor\":\"gps\",\"time\":1351824120,\"data\":[48.756080,2.302038]}"; + // 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.parseObject(json); + // Test if parsing succeeds. if (!root.success()) { Serial.println("parseObject() failed"); return; } + // Fetch values. + // + // Most of the time, you can rely on the implicit casts. + // In other case, you can do root["time"].as(); const char* sensor = root["sensor"]; long time = root["time"]; double latitude = root["data"][0]; double longitude = root["data"][1]; + // Print values. Serial.println(sensor); Serial.println(time); Serial.println(latitude, 6);