8000 JsonVariant automatically promotes to JsonObject or JsonArray on write · Cube-Line/ArduinoJson@6f55d1e · GitHub
[go: up one dir, main page]

Skip to content

Commit 6f55d1e

Browse files
committed
JsonVariant automatically promotes to JsonObject or JsonArray on write
1 parent 5aea136 commit 6f55d1e

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

53 files changed

+1194
-538
lines changed

CHANGELOG.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,12 @@ HEAD
2222
* Added `JsonDocument::operator[]`
2323
* Added `ARDUINOJSON_TAB` to configure the indentation character
2424
* Reduced the size of the pretty JSON serializer
25+
* Added `add()`, `createNestedArray()` and `createNestedObject()` to `JsonVariant`
26+
* `JsonVariant` automatically promotes to `JsonObject` or `JsonArray` on write.
27+
Calling `JsonVariant::to<T>()` is not required anymore.
28+
* `JsonDocument` now support the same operations as `JsonVariant`.
29+
Calling `JsonDocument::as<T>()` is not required anymore.
30+
* Fixed example `JsonHttpClient.ino`
2531

2632
> ### BREAKING CHANGES
2733
>

examples/JsonConfigFile/JsonConfigFile.ino

Lines changed: 19 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,11 @@
1515
#include <SD.h>
1616
#include <SPI.h>
1717

18-
// Configuration that we'll store on disk
18+
// Our configuration structure.
19+
//
20+
// Never use a JsonDocument to store the configuration!
21+
// A JsonDocument is *not* a permanent storage; it's only a temporary storage
22+
// used during the serialization phase.
1923
struct Config {
2024
char hostname[64];
2125
int port;
@@ -29,26 +33,23 @@ void loadConfiguration(const char *filename, Config &config) {
2933
// Open file for reading
3034
File file = SD.open(filename);
3135

32-
// Allocate the document on the stack.
36+
// Allocate a temporary JsonDocument
3337
// Don't forget to change the capacity to match your requirements.
34-
// Use arduinojson.org/assistant to compute the capacity.
38+
// Use arduinojson.org/v6/assistant to compute the capacity.
3539
StaticJsonDocument<512> doc;
3640

3741
// Deserialize the JSON document
3842
DeserializationError error = deserializeJson(doc, file);
3943
if (error)
4044
Serial.println(F("Failed to read file, using default configuration"));
4145

42-
// Get the root object in the document
43-
JsonObject root = doc.as<JsonObject>();
46+
// Copy values from the JsonDocument to the Config
47+
config.port = doc["port"] | 2731;
48+
strlcpy(config.hostname, // <- destination
49+
doc["hostname"] | "example.com", // <- source
50+
sizeof(config.hostname)); // <- destination's capacity
4451

45-
// Copy values from the JsonObject to the Config
46-
config.port = root["port"] | 2731;
47-
strlcpy(config.hostname, // <- destination
48-
root["hostname"] | "example.com", // <- source
49-
sizeof(config.hostname)); // <- destination's capacity
50-
51-
// Close the file (File's destructor doesn't close the file)
52+
// Close the file (Curiously, File's destructor doesn't close the file)
5253
file.close();
5354
}
5455

@@ -64,24 +65,21 @@ void saveConfiguration(const char *filename, const Config &config) {
6465
return;
6566
}
6667

67-
// Allocate the document on the stack.
68+
// Allocate a temporary JsonDocument
6869
// Don't forget to change the capacity to match your requirements.
6970
// Use arduinojson.org/assistant to compute the capacity.
7071
StaticJsonDocument<256> doc;
7172

72-
// Make our document contain an object
73-
JsonObject root = doc.to<JsonObject>();
74-
75-
// Set the values in the object
76-
root["hostname"] = config.hostname;
77-
root["port"] = config.port;
73+
// Set the values in the document
74+
doc["hostname"] = config.hostname;
75+
doc["port"] = config.port;
7876

7977
// Serialize JSON to file
8078
if (serializeJson(doc, file) == 0) {
8179
Serial.println(F("Failed to write to file"));
8280
}
8381

84-
// Close the file (File's destructor doesn't close the file)
82+
// Close the file
8583
file.close();
8684
}
8785

@@ -100,7 +98,7 @@ void printFile(const char *filename) {
10098
}
10199
Serial.println();
102100

103-
// Close the file (File's destructor doesn't close the file)
101+
// Close the file
104102
file.close();
105103
}
106104

examples/JsonGeneratorExample/JsonGeneratorExample.ino

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -15,38 +15,38 @@ void setup() {
1515
//
1616
// Inside the brackets, 200 is the RAM allocated to this document.
1717
// Don't forget to change this value to match your requirement.
18-
// Use arduinojson.org/assistant to compute the capacity.
18+
// Use arduinojson.org/v6/assistant to compute the capacity.
1919
StaticJsonDocument<200> doc;
2020

2121
// StaticJsonObject allocates memory on the stack, it can be
2222
// replaced by DynamicJsonDocument which allocates in the heap.
2323
//
2424
// DynamicJsonDocument doc(200);
2525

26-
// Make our document be an object
27-
JsonObject root = doc.to<JsonObject>();
28-
29-
// Add values in the object
26+
// Add values in the document
3027
//
31-
// Most of the time, you can rely on the implicit casts.
32-
// In other case, you can do root.set<long>("time", 1351824120);
33-
root["sensor"] = "gps";
34-
root["time"] = 1351824120;
28+
doc["sensor"] = "gps";
29+
doc["time"] = 1351824120;
3530

3631
// Add an array.
3732
//
38-
JsonArray data = root.createNestedArray("data");
33+
JsonArray data = doc.createNestedArray("data");
3934
data.add(48.756080);
4035
data.add(2.302038);
4136

37+
// Generate the minified JSON and send it to the Serial port.
38+
//
4239
serializeJson(doc, Serial);
43-
// This prints:
40+
// The above line prints:
4441
// {"sensor":"gps","time":1351824120,"data":[48.756080,2.302038]}
4542

43+
// Start a new line
4644
Serial.println();
4745

46+
// Generate the prettified JSON and send it to the Serial port.
47+
//
4848
serializeJsonPretty(doc, Serial);
49-
// This prints:
49+
// The above line prints:
5050
// {
5151
// "sensor": "gps",
5252
// "time": 1351824120,

examples/JsonHttpClient/JsonHttpClient.ino

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,7 @@ void setup() {
7171
}
7272

7373
// Allocate the JSON document
74-
// Use arduinojson.org/assistant to compute the capacity.
74+
// Use arduinojson.org/v6/assistant to compute the capacity.
7575
const size_t capacity = JSON_OBJECT_SIZE(3) + JSON_ARRAY_SIZE(2) + 60;
7676
DynamicJsonDocument doc(capacity);
7777

@@ -84,12 +84,11 @@ void setup() {
8484
}
8585

8686
// Extract values
87-
JsonObject root = doc.as<JsonObject>();
8887
Serial.println(F("Response:"));
89-
Serial.println(root["sensor"].as<char*>());
90-
Serial.println(root["time"].as<char*>());
91-
Serial.println(root["data"][0].as<char*>());
92-
Serial.println(root["data"][1].as<char*>());
88+
Serial.println(doc["sensor"].as<char*>());
89+
Serial.println(doc["time"].as<long>());
90+
Serial.println(doc["data"][0].as<float>(), 6);
91+
Serial.println(doc["data"][1].as<float>(), 6);
9392

9493
// Disconnect
9594
client.stop();

examples/JsonParserExample/JsonParserExample.ino

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -13,9 +13,9 @@ void setup() {
1313

1414
// Allocate the JSON document
1515
//
16-
// Inside the brackets, 200 is the size of the memory pool in bytes.
16+
// Inside the brackets, 200 is the capacity of the memory pool in bytes.
1717
// Don't forget to change this value to match your JSON document.
18-
// Use arduinojson.org/assistant to compute the capacity.
18+
// Use arduinojson.org/v6/assistant to compute the capacity.
1919
StaticJsonDocument<200> doc;
2020

2121
// StaticJsonDocument<N> allocates memory on the stack, it can be
@@ -25,9 +25,12 @@ void setup() {
2525

2626
// JSON input string.
2727
//
28-
// It's better to use a char[] as shown here.
29-
// If you use a const char* or a String, ArduinoJson will
30-
// have to make a copy of the input in the JsonBuffer.
28+
// Using a char[], as shown here, enables the "zero-copy" mode. This mode uses
29+
// the minimal amount of memory because the JsonDocument stores pointers to
30+
// the input buffer.
31+
// If you use another type of input, ArduinoJson must copy the strings from
32+
// the input to the JsonDocument, so you need to increase the capacity of the
33+
// JsonDocument.
3134
char json[] =
3235
"{\"sensor\":\"gps\",\"time\":1351824120,\"data\":[48.756080,2.302038]}";
3336

@@ -41,17 +44,14 @@ void setup() {
4144
return;
4245
}
4346

44-
// Get the root object in the document
45-
JsonObject root = doc.as<JsonObject>();
46-
4747
// Fetch values.
4848
//
4949
// Most of the time, you can rely on the implicit casts.
50-
// In other case, you can do root["time"].as<long>();
51-
const char* sensor = root["sensor"];
52-
long time = root["time"];
53-
double latitude = root["data"][0];
54-
double longitude = root["data"][1];
50+
// In other case, you can do doc["time"].as<long>();
51+
const char* sensor = doc["sensor"];
52+
long time = doc["time"];
53+
double latitude = doc["data"][0];
54+
double longitude = doc["data"][1];
5555

5656
// Print values.
5757
Serial.println(sensor);

examples/JsonServer/JsonServer.ino

Lines changed: 15 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -2,15 +2,15 @@
22
// Copyright Benoit Blanchon 2014-2018
33
// MIT License
44
//
5-
// This example shows how to implement an HTTP server that sends JSON document
6-
// in the responses.
5+
// This example shows how to implement an HTTP server that sends a JSON document
6+
// in the response.
77
// It uses the Ethernet library but can be easily adapted for Wifi.
88
//
9-
// It sends the value of the analog and digital pins.
10-
// The JSON document looks like the following:
9+
// The JSON document contains the values of the analog and digital pins.
10+
// It looks like that:
1111
// {
12-
// "analog": [ 0, 1, 2, 3, 4, 5 ],
13-
// "digital": [ 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13 ]
12+
// "analog": [0, 76, 123, 158, 192, 205],
13+
// "digital": [1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 0, 0]
1414
// }
1515

1616
#include <ArduinoJson.h>
@@ -51,15 +51,12 @@ void loop() {
5151
// Read the request (we ignore the content in this example)
5252
while (client.available()) client.read();
5353

54-
// Allocate the JSON document
55-
// Use arduinojson.org/assistant to compute the capacity.
54+
// Allocate a temporary JsonDocument
55+
// Use arduinojson.org/v6/assistant to compute the capacity.
5656
StaticJsonDocument<500> doc;
5757

58-
// Make our document represent an object
59-
JsonObject root = doc.to<JsonObject>();
60-
6158
// Create the "analog" array
62-
JsonArray analogValues = root.createNestedArray("analog");
59+
JsonArray analogValues = doc.createNestedArray("analog");
6360
for (int pin = 0; pin < 6; pin++) {
6461
// Read the analog input
6562
int value = analogRead(pin);
@@ -69,7 +66,7 @@ void loop() {
6966
}
7067

7168
// Create the "digital" array
72-
JsonArray digitalValues = root.createNestedArray("digital");
69+
JsonArray digitalValues = doc.createNestedArray("digital");
7370
for (int pin = 0; pin < 14; pin++) {
7471
// Read the digital input
7572
int value = digitalRead(pin);
@@ -83,9 +80,11 @@ void loop() {
8380
Serial.println();
8481

8582
// Write response headers
86-
client.println("HTTP/1.0 200 OK");
87-
client.println("Content-Type: application/json");
88-
client.println("Connection: close");
83+
client.println(F("HTTP/1.0 200 OK"));
84+
client.println(F("Content-Type: application/json"));
85+
client.println(F("Connection: close"));
86+
client.print(F("Content-Length: "));
87+
client.println(measureJsonPretty(doc));
8988
client.println();
9089

9190
// Write JSON document

examples/JsonUdpBeacon/JsonUdpBeacon.ino

Lines changed: 7 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,10 @@
55
// This example shows how to send a JSON document to a UDP socket.
66
// At regular interval, it sends a UDP packet that contains the status of
77
// analog and digital pins.
8-
// The JSON document looks like the following:
8+
// It looks like that:
99
// {
10-
// "analog": [ 0, 1, 2, 3, 4, 5 ],
11-
// "digital": [ 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13 ]
10+
// "analog": [0, 76, 123, 158, 192, 205],
11+
// "digital": [1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 0, 0]
1212
// }
1313
//
1414
// If you want to test this program, you need to be able to receive the UDP
@@ -43,15 +43,12 @@ void setup() {
4343
}
4444

4545
void loop() {
46-
// Allocate the JSON document
47-
// Use arduinojson.org/assistant to compute the capacity.
46+
// Allocate a temporary JsonDocument
47+
// Use arduinojson.org/v6/assistant to compute the capacity.
4848
StaticJsonDocument<500> doc;
4949

50-
// Make our document represent an object
51-
JsonObject root = doc.to<JsonObject>();
52-
5350
// Create the "analog" array
54-
JsonArray analogValues = root.createNestedArray("analog");
51+
JsonArray analogValues = doc.createNestedArray("analog");
5552
for (int pin = 0; pin < 6; pin++) {
5653
// Read the analog input
5754
int value = analogRead(pin);
@@ -61,7 +58,7 @@ void loop() {
6158
}
6259

6360
// Create the "digital" array
64-
JsonArray digitalValues = root.createNestedArray("digital");
61+
JsonArray digitalValues = doc.createNestedArray("digital");
6562
for (int pin = 0; pin < 14; pin++) {
6663
// Read the digital input
6764
int value = digitalRead(pin);

0 commit comments

Comments
 (0)
0