Added StaticJsonDocument and DynamicJsonDocument.

Removed StaticJsonArray and DynamicJsonArray.
Removed StaticJsonObject and DynamicJsonObject.
Removed StaticJsonVariant and DynamicJsonVariant.
This commit is contained in:
Benoit Blanchon
2018-04-17 21:27:45 +02:00
parent a13b9e8bdc
commit 1feb92679d
100 changed files with 1696 additions and 1844 deletions

View File

@ -29,17 +29,19 @@ void loadConfiguration(const char *filename, Config &config) {
// Open file for reading
File file = SD.open(filename);
// Allocate the memory pool on the stack.
// Don't forget to change the capacity to match your JSON document.
// Allocate the document on the stack.
// Don't forget to change the capacity to match your requirements.
// Use arduinojson.org/assistant to compute the capacity.
StaticJsonObject<512> root;
// Parse the root object
JsonError error = deserializeJson(root, file);
StaticJsonDocument<512> doc;
// Deserialize the JSON document
JsonError error = deserializeJson(doc, file);
if (error)
Serial.println(F("Failed to read file, using default configuration"));
// Get the root object in the document
JsonObject &root = doc.as<JsonObject>();
// Copy values from the JsonObject to the Config
config.port = root["port"] | 2731;
strlcpy(config.hostname, // <- destination
@ -62,17 +64,20 @@ void saveConfiguration(const char *filename, const Config &config) {
return;
}
// 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.
StaticJsonObject<256> root;
// Allocate the document on the stack.
// Don't forget to change the capacity to match your requirements.
// Use arduinojson.org/assistant to compute the capacity.
StaticJsonDocument<256> doc;
// Set the values
// Make our document contain an object
JsonObject &root = doc.to<JsonObject>();
// Set the values in the object
root["hostname"] = config.hostname;
root["port"] = config.port;
// Serialize JSON to file
if (serializeJson(root, file) == 0) {
if (serializeJson(doc, file) == 0) {
Serial.println(F("Failed to write to file"));
}

View File

@ -11,17 +11,20 @@ void setup() {
Serial.begin(9600);
while (!Serial) continue;
// Root JSON object
// The JSON document
//
// 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.
// 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.
StaticJsonObject<200> root;
StaticJsonDocument<200> doc;
// StaticJsonObject 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 root(200);
// DynamicJsonDocument doc(200);
// Make our document be an object
JsonObject& root = doc.to<JsonObject>();
// Add values in the object
//
@ -30,10 +33,8 @@ void setup() {
root["sensor"] = "gps";
root["time"] = 1351824120;
// Add a nested array.
// Add an 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(48.756080);
data.add(2.302038);

View File

@ -70,19 +70,21 @@ void setup() {
return;
}
// Allocate JsonBuffer
// Allocate the JSON document
// Use arduinojson.org/assistant to compute the capacity.
const size_t capacity = JSON_OBJECT_SIZE(3) + JSON_ARRAY_SIZE(2) + 60;
DynamicJsonObject root(capacity);
DynamicJsonDocument doc(capacity);
// Parse JSON object
JsonError error = deserializeJson(root, client);
JsonError error = deserializeJson(doc, client);
if (error) {
Serial.println(F("Parsing failed!"));
Serial.print(F("deserializeJson() failed: "));
Serial.println(error.c_str());
return;
}
// Extract values
JsonObject& root = doc.as<JsonObject>();
Serial.println(F("Response:"));
Serial.println(root["sensor"].as<char*>());
Serial.println(root["time"].as<char*>());

View File

@ -16,12 +16,12 @@ void setup() {
// 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.
StaticJsonObject<200> root;
StaticJsonDocument<200> doc;
// StaticJsonObject 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.
//
// DynamicJsonObject root(200);
// DynamicJsonObject doc(200);
// JSON input string.
//
@ -31,23 +31,23 @@ void setup() {
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.
JsonError error = deserializeJson(root, json);
// Deserialize the JSON document
JsonError error = deserializeJson(doc, json);
// Test if parsing succeeds.
if (error) {
Serial.println("deserializeJson() failed");
Serial.print(F("deserializeJson() failed: "));
Serial.println(error.c_str());
return;
}
// Get the root object in the document
JsonObject& root = doc.as<JsonObject>();
// Fetch values.
//
// Most of the time, you can rely on the implicit casts.
// In other case, you can do root["time"].as<long>();
// In other case, you can do doc["time"].as<long>();
const char* sensor = root["sensor"];
long time = root["time"];
double latitude = root["data"][0];

View File

@ -51,9 +51,12 @@ void loop() {
// Read the request (we ignore the content in this example)
while (client.available()) client.read();
// Allocate the root JsonObject
// Allocate the JSON document
// Use arduinojson.org/assistant to compute the capacity.
StaticJsonObject<500> root;
StaticJsonDocument<500> doc;
// Make our document represent an object
JsonObject& root = doc.to<JsonObject>();
// Create the "analog" array
JsonArray& analogValues = root.createNestedArray("analog");

View File

@ -43,9 +43,12 @@ void setup() {
}
void loop() {
// Allocate the root JsonObject
// Allocate the JSON document
// Use arduinojson.org/assistant to compute the capacity.
StaticJsonObject<500> root;
StaticJsonDocument<500> doc;
// Make our document represent an object
JsonObject& root = doc.to<JsonObject>();
// Create the "analog" array
JsonArray& analogValues = root.createNestedArray("analog");

View File

@ -12,17 +12,17 @@ void setup() {
Serial.begin(9600);
while (!Serial) continue;
// Root JSON object
// Allocate the JSON document
//
// 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.
StaticJsonObject<200> root;
StaticJsonDocument<200> doc;
// StaticJsonObject allocates memory on the stack, it can be
// replaced by DynamicJsonObject which allocates in the heap.
//
// DynamicJsonObject root(200);
// DynamicJsonObject doc(200);
// MessagePack input string.
//
@ -40,23 +40,27 @@ void setup() {
// "data": [48.75608, 2.302038]
// }
// Root of the object tree.
// doc 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.
MsgPackError error = deserializeMsgPack(root, input);
MsgPackError error = deserializeMsgPack(doc, input);
// Test if parsing succeeds.
if (error) {
Serial.println("deserializeMsgPack() failed");
Serial.print("deserializeMsgPack() failed: ");
Serial.println(error.c_str());
return;
}
// Get the root object in the document
JsonObject& root = doc.as<JsonObject>();
// Fetch values.
//
// Most of the time, you can rely on the implicit casts.
// In other case, you can do root["time"].as<long>();
// In other case, you can do doc["time"].as<long>();
const char* sensor = root["sensor"];
long time = root["time"];
double latitude = root["data"][0];

View File

@ -14,33 +14,34 @@
void setup() {
#ifdef PROGMEM // <- check that Flash strings are supported
DynamicJsonObject root;
DynamicJsonDocument doc;
// You can use a Flash String as your JSON input.
// WARNING: the content of the Flash String will be duplicated in the
// JsonBuffer.
deserializeJson(root, F("{\"sensor\":\"gps\",\"time\":1351824120,"
"\"data\":[48.756080,2.302038]}"));
deserializeJson(doc, F("{\"sensor\":\"gps\",\"time\":1351824120,"
"\"data\":[48.756080,2.302038]}"));
JsonObject& obj = doc.as<JsonObject>();
// You can use a Flash String to get an element of a JsonObject
// No duplication is done.
long time = root[F("time")];
long time = obj[F("time")];
// You can use a Flash String to set an element of a JsonObject
// WARNING: the content of the Flash String will be duplicated in the
// JsonBuffer.
root[F("time")] = time;
obj[F("time")] = time;
// You can set a Flash String to a JsonObject or JsonArray:
// WARNING: the content of the Flash String will be duplicated in the
// JsonBuffer.
root["sensor"] = F("gps");
obj["sensor"] = F("gps");
// It works with RawJson too:
root["sensor"] = RawJson(F("\"gps\""));
obj["sensor"] = RawJson(F("\"gps\""));
// You can compare the content of a JsonVariant to a Flash String
if (root["sensor"] == F("gps")) {
if (obj["sensor"] == F("gps")) {
// ...
}

View File

@ -11,50 +11,51 @@
#include <ArduinoJson.h>
void setup() {
DynamicJsonObject root;
DynamicJsonDocument doc;
// 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]}";
deserializeJson(root, input);
deserializeJson(doc, input);
JsonObject& obj = doc.as<JsonObject>();
// You can use a String to get an element of a JsonObject
// No duplication is done.
long time = root[String("time")];
long time = obj[String("time")];
// You can use a String to set an element of a JsonObject
// WARNING: the content of the String will be duplicated in the JsonBuffer.
root[String("time")] = time;
obj[String("time")] = time;
// You can get a String from a JsonObject or JsonArray:
// No duplication is done, at least not in the JsonBuffer.
String sensor = root["sensor"];
String sensor = obj["sensor"];
// Unfortunately, the following doesn't work (issue #118):
// sensor = root["sensor"]; // <- error "ambiguous overload for 'operator='"
// sensor = obj["sensor"]; // <- error "ambiguous overload for 'operator='"
// As a workaround, you need to replace by:
sensor = root["sensor"].as<String>();
sensor = obj["sensor"].as<String>();
// You can set a String to a JsonObject or JsonArray:
// WARNING: the content of the String will be duplicated in the JsonBuffer.
root["sensor"] = sensor;
obj["sensor"] = sensor;
// It works with RawJson too:
root["sensor"] = RawJson(sensor);
obj["sensor"] = RawJson(sensor);
// You can also concatenate strings
// WARNING: the content of the String will be duplicated in the JsonBuffer.
root[String("sen") + "sor"] = String("gp") + "s";
obj[String("sen") + "sor"] = String("gp") + "s";
// You can compare the content of a JsonObject with a String
if (root["sensor"] == sensor) {
if (obj["sensor"] == sensor) {
// ...
}
// Lastly, you can print the resulting JSON to a String
String output;
serializeJson(root, output);
serializeJson(doc, output);
}
void loop() {