8000 Return `JsonArray` and `JsonObject` by value (closes #309) · java64/ArduinoJson@b105e6f · GitHub
[go: up one dir, main page]

Skip to content

Commit b105e6f

Browse files
committed
Return JsonArray and JsonObject by value (closes bblanchon#309)
1 parent 4fe2b11 commit b105e6f

File tree

93 files changed

+941
-1049
lines changed

Some content is hidden

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

93 files changed

+941
-1049
lines changed

CHANGELOG.md

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,36 @@
11
ArduinoJson: change log
22
=======================
33

4+
HEAD
5+
----
6+
7+
* Return `JsonArray` and `JsonObject` by value instead of reference (issue #309)
8+
* Replaced `success()` with `isNull()`
9+
10+
> ### BREAKING CHANGES
11+
>
12+
> Old code:
13+
>
14+
> ```c++
15+
> JsonObject& obj = doc.to<JsonObject>();
16+
> JsonArray& arr = obj.createNestedArray("key");
17+
> if (!arr.success()) {
18+
> Serial.println("No enough memory");
19+
> return;
20+
> }
21+
> ```
22+
>
23+
> New code:
24+
>
25+
> ```c++
26+
> JsonObject obj = doc.to<JsonObject>();
27+
> JsonArray arr = obj.createNestedArray("key");
28+
> if (arr.isNull()) {
29+
> Serial.println("No enough memory");
30+
> return;
31+
> }
32+
> ```
33+
434
v6.0.1-beta
535
-----------
636

README.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ char json[] = "{\"sensor\":\"gps\",\"time\":1351824120,\"data\":[48.756080,2.302
6363
DynamicJsonDocument doc;
6464
deserializeJson(doc, json);
6565

66-
JsonObject& root = doc.as<JsonObject>();
66+
JsonObjectRef root = doc.as<JsonObject>();
6767
const char* sensor = root["sensor"];
6868
long time = root["time"];
6969
double latitude = root["data"][0];
@@ -79,11 +79,11 @@ Here is a program that generates a JSON document with ArduinoJson:
7979
```c++
8080
DynamicJsonDocument doc;
8181
82-
JsonObject& root = doc.to<JsonObject>();
82+
JsonObject root = doc.to<JsonObject>();
8383
root["sensor"] = "gps";
8484
root["time"] = 1351824120;
8585
86-
JsonArray& data = root.createNestedArray("data");
86+
JsonArray data = root.createNestedArray("data");
8787
data.add(48.756080);
8888
data.add(2.302038);
8989

examples/JsonConfigFile/JsonConfigFile.ino

Lines changed: 3 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ void loadConfiguration(const char *filename, Config &config) {
4040
Serial.println(F("Failed to read file, using default configuration"));
4141

4242
// Get the root object in the document
43-
JsonObject &root = doc.as<JsonObject>();
43+
JsonObject root = doc.as<JsonObject>();
4444

4545
// Copy values from the JsonObject to the Config
4646
config.port = root["port"] | 2731;
@@ -70,7 +70,7 @@ void saveConfiguration(const char *filename, const Config &config) {
7070
StaticJsonDocument<256> doc;
7171

7272
// Make our document contain an object
73-
JsonObject &root = doc.to<JsonObject>();
73+
JsonObject root = doc.to<JsonObject>();
7474

7575
// Set the values in the object
7676
root["hostname"] = config.hostname;
@@ -132,15 +132,4 @@ void loop() {
132132
// not used in this example
133133
}
134134

135-
// See also
136-
// --------
137-
//
138-
// The website arduinojson.org contains the documentation for all the functions
139-
// used above. It also includes an FAQ that will help you solve any
140-
// serialization or deserialization problem.
141-
// Please check it out at: https://arduinojson.org/
142-
//
143-
// The book "Mastering ArduinoJson" contains a case study of a project that has
144-
// a complex configuration with nested members.
145-
// Contrary to this example, the project in the book uses the SPIFFS filesystem.
146-
// Please check it out at: https://arduinojson.org/book/
135+
// Visit https://arduinojson.org/v6/example/config/ for more.

examples/JsonGeneratorExample/JsonGeneratorExample.ino

Lines changed: 3 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ void setup() {
2424
// DynamicJsonDocument doc(200);
2525

2626
// Make our document be an object
27-
JsonObject& root = doc.to<JsonObject>();
27+
JsonObject root = doc.to<JsonObject>();
2828

2929
// Add values in the object
3030
//
@@ -35,7 +35,7 @@ void setup() {
3535

3636
// Add an array.
3737
//
38-
JsonArray& data = root.createNestedArray("data");
38+
JsonArray data = root.createNestedArray("data");
3939
data.add(48.756080);
4040
data.add(2.302038);
4141

@@ -61,15 +61,4 @@ void loop() {
6161
// not used in this example
6262
}
6363

64-
// See also
65-
// --------
66-
//
67-
// The website arduinojson.org contains the documentation for all the functions
68-
// used above. It also includes an FAQ that will help you solve any
69-
// serialization problem.
70-
// Please check it out at: https://arduinojson.org/
71-
//
72-
// The book "Mastering ArduinoJson" contains a tutorial on serialization.
73-
// It begins with a simple example, like the one above, and then adds more
74-
// features like serializing directly to a file or an HTTP request.
75-
// Please check it out at: https://arduinojson.org/book/
64+
// Visit https://arduinojson.org/v6/example/generator/ for more.

examples/JsonHttpClient/JsonHttpClient.ino

Lines changed: 2 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,7 @@ void setup() {
8484
}
8585

8686
// Extract values
87-
JsonObject& root = doc.as<JsonObject>();
87+
JsonObject root = doc.as<JsonObject>();
8888
Serial.println(F("Response:"));
8989
Serial.println(root["sensor"].as<char*>());
9090
Serial.println(root["time"].as<char*>());
@@ -99,16 +99,4 @@ void loop() {
9999
// not used in this example
100100
}
101101

102-
// See also
103-
// --------
104-
//
105-
// The website arduinojson.org contains the documentation for all the functions
106-
// used above. It also includes an FAQ that will help you solve any
107-
// serialization problem.
108-
// Please check it out at: https://arduinojson.org/
109-
//
110-
// The book "Mastering ArduinoJson" contains a tutorial on deserialization
111-
// showing how to parse the response from Yahoo Weather. In the last chapter,
112-
// it shows how to parse the huge documents from OpenWeatherMap
113-
// and Weather Underground.
114-
// Please check it out at: https://arduinojson.org/book/
102+
// Visit https://arduinojson.org/v6/example/http-client/ for more.

examples/JsonParserExample/JsonParserExample.ino

Lines changed: 2 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ void setup() {
4242
}
4343

4444
// Get the root object in the document
45-
JsonObject& root = doc.as<JsonObject>();
45+
JsonObject root = doc.as<JsonObject>();
4646

4747
// Fetch values.
4848
//
@@ -64,15 +64,4 @@ void loop() {
6464
// not used in this example
6565
}
6666

67-
// See also
68-
// --------
69-
//
70-
// The website arduinojson.org contains the documentation for all the functions
71-
// used above. It also includes an FAQ that will help you solve any
72-
// deserialization problem.
73-
// Please check it out at: https://arduinojson.org/
74-
//
75-
// The book "Mastering ArduinoJson" contains a tutorial on deserialization.
76-
// It begins with a simple example, like the one above, and then adds more
77-
// features like deserializing directly from a file or an HTTP request.
78-
// Please check it out at: https://arduinojson.org/book/
67+
// Visit https://arduinojson.org/v6/example/parser/ for more.

examples/JsonServer/JsonServer.ino

Lines changed: 4 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -56,10 +56,10 @@ void loop() {
5656
StaticJsonDocument<500> doc;
5757

5858
// Make our document represent an object
59-
JsonObject& root = doc.to<JsonObject>();
59+
JsonObject root = doc.to<JsonObject>();
6060

6161
// Create the "analog" array
62-
JsonArray& analogValues = root.createNestedArray("analog");
62+
JsonArray analogValues = root.createNestedArray("analog");
6363
for (int pin = 0; pin < 6; pin++) {
6464
// Read the analog input
6565
int value = analogRead(pin);
@@ -69,7 +69,7 @@ void loop() {
6969
}
7070

7171
// Create the "digital" array
72-
JsonArray& digitalValues = root.createNestedArray("digital");
72+
JsonArray digitalValues = root.createNestedArray("digital");
7373
for (int pin = 0; pin < 14; pin++) {
7474
// Read the digital input
7575
int value = digitalRead(pin);
@@ -95,15 +95,4 @@ void loop() {
9595
client.stop();
9696
}
9797

98-
// See also
99-
// --------
100-
//
101-
// The website arduinojson.org contains the documentation for all the functions
102-
// used above. It also includes an FAQ that will help you solve any
103-
// serialization problem.
104-
// Please check it out at: https://arduinojson.org/
105-
//
106-
// The book "Mastering ArduinoJson" contains a tutorial on serialization.
107-
// It begins with a simple example, then adds more features like serializing
108-
// directly to a file or an HTTP client.
109-
// Please check it out at: https://arduinojson.org/book/
98+
// Visit https://arduinojson.org/v6/example/http-server/ for more.

examples/JsonUdpBeacon/JsonUdpBeacon.ino

Lines changed: 4 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -48,10 +48,10 @@ void loop() {
4848
StaticJsonDocument<500> doc;
4949

5050
// Make our document represent an object
51-
JsonObject& root = doc.to<JsonObject>();
51+
JsonObject root = doc.to<JsonObject>();
5252

5353
// Create the "analog" array
54-
JsonArray& analogValues = root.createNestedArray("analog");
54+
JsonArray analogValues = root.createNestedArray("analog");
5555
for (int pin = 0; pin < 6; pin++) {
5656
// Read the analog input
5757
int value = analogRead(pin);
@@ -61,7 +61,7 @@ void loop() {
6161
}
6262

6363
// Create the "digital" array
64-
JsonArray& digitalValues = root.createNestedArray("digital");
64+
JsonArray digitalValues = root.createNestedArray("digital");
6565
for (int pin = 0; pin < 14; pin++) {
6666
// Read the digital input
6767
int value = digitalRead(pin);
@@ -87,15 +87,4 @@ void loop() {
8787
delay(10000);
8888
}
8989

90-
// See also
91-
// --------
92-
//
93-
// The website arduinojson.org contains the documentation for all the functions
94-
// used above. It also includes an FAQ that will help you solve any
95-
// serialization problem.
96-
// Please check it out at: https://arduinojson.org/
97-
//
98-
// The book "Mastering ArduinoJson" contains a tutorial on serialization.
99-
// It begins with a simple example, then adds more features like serializing
100-
// directly to a file or any stream.
101-
// Please check it out at: https://arduinojson.org/book/
90+
// Visit https://arduinojson.org/v6/example/udp-beacon/ for more.

examples/MsgPackParser/MsgPackParser.ino

Lines changed: 2 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ void setup() {
5555
}
5656

5757
// Get the root object in the document
58-
JsonObject& root = doc.as<JsonObject>();
58+
JsonObject root = doc.as<JsonObject>();
5959

6060
// Fetch values.
6161
//
@@ -77,15 +77,4 @@ void loop() {
7777
// not used in this example
7878
}
7979

80-
// See also
81-
// --------
82-
//
83-
// The website arduinojson.org contains the documentation for all the functions
84-
// used above. It also includes an FAQ that will help you solve any
85-
// deserialization problem.
86-
// Please check it out at: https://arduinojson.org/
87-
//
88-
// The book "Mastering ArduinoJson" contains a tutorial on deserialization.
89-
// It begins with a simple example, like the one above, and then adds more
90-
// features like deserializing directly from a file or an HTTP request.
91-
// Please check it out at: https://arduinojson.org/book/
80+
// Visit https://arduinojson.org/v6/example/msgpack-parser/ for more.

examples/ProgmemExample/ProgmemExample.ino

Lines changed: 2 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ void setup() {
2121
// JsonBuffer.
2222
deserializeJson(doc, F("{\"sensor\":\"gps\",\"time\":1351824120,"
2323
"\"data\":[48.756080,2.302038]}"));
24-
JsonObject& obj = doc.as<JsonObject>();
24+
JsonObject obj = doc.as<JsonObject>();
2525

2626
// You can use a Flash String to get an element of a JsonObject
2727
// No duplication is done.
@@ -56,15 +56,4 @@ void loop() {
5656
// not used in this example
5757
}
5858

59-
// See also
60-
// --------
61-
//
62-
// The website arduinojson.org contains the documentation for all the functions
63-
// used above. It also includes an FAQ that will help you solve any memory
64-
// problem.
65-
// Please check it out at: https://arduinojson.org/
66-
//
67-
// The book "Mastering ArduinoJson" contains a quick C++ course that explains
68-
// how your microcontroller stores strings in memory. It also tells why you
69-
// should not abuse Flash strings with ArduinoJson.
70-
// Please check it out at: https://arduinojson.org/book/
59+
// Visit https://arduinojson.org/v6/example/progmem/ for more.

examples/StringExample/StringExample.ino

Lines changed: 2 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ void setup() {
1818
String input =
1919
"{\"sensor\":\"gps\",\"time\":1351824120,\"data\":[48.756080,2.302038]}";
2020
deserializeJson(doc, input);
21-
JsonObject& obj = doc.as<JsonObject>();
21+
JsonObject obj = doc.as<JsonObject>();
2222

2323
// You can use a String to get an element of a JsonObject
2424
// No duplication is done.
@@ -62,14 +62,4 @@ void loop() {
6262
// not used in this example
6363
}
6464

65-
// See also
66-
// --------
67-
//
68-
// The website arduinojson.org contains the documentation for all the functions
69-
// used above. It also includes an FAQ that will help you solve any problem.
70-
// Please check it out at: https://arduinojson.org/
71-
//
72-
// The book "Mastering ArduinoJson" contains a quick C++ course that explains
73-
// how your microcontroller stores strings in memory. On several occasions, it
74-
// shows how you can avoid String in your program.
75-
// Please check it out at: https://arduinojson.org/book/
65+
// Visit https://arduinojson.org/v6/example/string/ for more.

src/ArduinoJson.hpp

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,13 +7,20 @@
77
#include "ArduinoJson/version.hpp"
88

99
#include "ArduinoJson/DynamicJsonDocument.hpp"
10+
#include "ArduinoJson/StaticJsonDocument.hpp"
11+
12+
#include "ArduinoJson/JsonObjectImpl.hpp"
13+
14+
#include "ArduinoJson/JsonArray.hpp"
15+
#include "ArduinoJson/JsonObject.hpp"
16+
17+
#include "ArduinoJson/JsonArrayImpl.hpp"
18+
#include "ArduinoJson/JsonArraySubscript.hpp"
19+
#include "ArduinoJson/JsonObjectSubscript.hpp"
20+
#include "ArduinoJson/JsonVariantImpl.hpp"
21+
1022
#include "ArduinoJson/Json/JsonDeserializer.hpp"
1123
#include "ArduinoJson/Json/JsonSerializer.hpp"
1224
#include "ArduinoJson/Json/PrettyJsonSerializer.hpp"
1325
#include "ArduinoJson/MsgPack/MsgPackDeserializer.hpp"
1426
#include "ArduinoJson/MsgPack/MsgPackSerializer.hpp"
15-
#include "ArduinoJson/StaticJsonDocument.hpp"
16-
17-
#include "ArduinoJson/JsonArrayImpl.hpp"
18-
#include "ArduinoJson/JsonObjectImpl.hpp"
19-
#include "ArduinoJson/JsonVariantImpl.hpp"

src/ArduinoJson/Data/JsonVariantAs.hpp

Lines changed: 2 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -19,24 +19,5 @@ struct JsonVariantAs<char*> {
1919
typedef const char* type;
2020
};
2121

22-
template <>
23-
struct JsonVariantAs<JsonArray> {
24-
typedef JsonArray& type;
25-
};
26-
27-
template <>
28-
struct JsonVariantAs<const JsonArray> {
29-
typedef const JsonArray& type;
30-
};
31-
32-
template <>
33-
struct JsonVariantAs<JsonObject> {
34-
typedef JsonObject& type;
35-
};
36-
37-
template <>
38-
struct JsonVariantAs<const JsonObject> {
39-
typedef const JsonObject& type;
40-
};
41-
}
42-
}
22+
} // namespace Internals
23+
} // namespace ArduinoJson

0 commit comments

Comments
 (0)
0