8000 Reworked all examples · java64/ArduinoJson@461e301 · GitHub
[go: up one dir, main page]

Skip to content

Commit 461e301

Browse files
committed
Reworked all examples
1 parent 57d98e4 commit 461e301

File tree

8 files changed

+293
-137
lines changed

8 files changed

+293
-137
lines changed

CHANGELOG.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ HEAD
88
* Added a clear error message when compiled as C instead of C++ (issue #629)
99
* Added detection of MPLAB XC compiler (issue #629)
1010
* Added detection of Keil ARM Compiler (issue #629)
11-
* Rewrote example `JsonHttpClient.ino` (issue #600)
11+
* Reworked all examples
1212

1313
> ### How to use the new feature?
1414
>

examples/JsonGeneratorExample/JsonGeneratorExample.ino

Lines changed: 19 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,21 @@
11
// ArduinoJson - arduinojson.org
22
// Copyright Benoit Blanchon 2014-2017
33
// MIT License
4+
//
5+
// This example shows how to generate a JSON document with ArduinoJson.
46

57
#include <ArduinoJson.h>
68

79
void setup() {
10+
// Initialize Serial port
811
Serial.begin(9600);
9-
while (!Serial) {
10-
// wait serial port initialization
11-
}
12+
while (!Serial) continue;
1213

1314
// Memory pool for JSON object tree.
1415
//
1516
// Inside the brackets, 200 is the size of the pool in bytes.
16-
// If the JSON object is more complex, you need to increase that value.
17-
// See http://arduinojson.org/assistant/
17+
// Don't forget to change this value to match your JSON document.
18+
// See https://arduinojson.org/assistant/
1819
StaticJsonBuffer<200> jsonBuffer;
1920

2021
// StaticJsonBuffer allocates memory on the stack, it can be
@@ -65,3 +66,16 @@ void setup() {
6566
void loop() {
6667
// not used in this example
6768
}
69+
70+
// See also
71+
// --------
72+
//
73+
// The website arduinojson.org contains the documentation for all the functions
74+
// used above. It also includes an FAQ that will help you solve any
75+
// serialization problem.
76+
// Please check it out at: https://arduinojson.org/
77+
//
78+
// The book "Mastering ArduinoJson" contains a tutorial on serialization.
79+
// It begins with a simple example, like the one above, and then adds more
80+
// features like serializing directly to a file or an HTTP request.
81+
// Please check it out at: https://leanpub.com/arduinojson/

examples/JsonHttpClient/JsonHttpClient.ino

Lines changed: 67 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,10 @@
22
// Copyright Benoit Blanchon 2014-2017
33
// MIT License
44
//
5-
// Example of an HTTP client parsing a JSON response.
5+
// This example shows how to parse a JSON document in an HTTP response.
6+
// It uses the Ethernet library, but can be easily adapter for Wifi.
67
//
7-
// This program perform an HTTP GET of arduinojson.org/example.json
8+
// It performs a GET resquest on arduinojson.org/example.json
89
// Here is the expected response:
910
// {
1011
// "sensor": "gps",
@@ -14,73 +15,98 @@
1415
// 2.302038
1516
// ]
1617
// }
17-
// See http://arduinojson.org/assistant/ to compute the size of the buffer.
18-
//
19-
// Disclaimer: the code emphasize the communication between client and server,
20-
// it doesn't claim to be a reference of good coding practices.
2118

2219
#include <ArduinoJson.h>
2320
#include <Ethernet.h>
2421
#include <SPI.h>
2522

2623
void setup() {
24+
// Initialize Serial port
2725
Serial.begin(9600);
28-
while (!Serial);
26+
while (!Serial) continue;
2927

30-
echo("Initialize Ethernet library");
28+
// Initialize Ethernet library
3129
byte mac[] = {0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED};
32-
Ethernet.begin(mac) || die("Failed to configure Ethernet");
30+
if (!Ethernet.begin(mac)) {
31+
Serial.println(F("Failed to configure Ethernet"));
32+
return;
33+
}
3334
delay(1000);
3435

35-
echo("Connect to HTTP server");
36+
Serial.println(F("Connecting..."));
37+
38+
// Connect to HTTP server
3639
EthernetClient client;
3740
client.setTimeout(10000);
38-
client.connect("arduinojson.org", 80) || die("Connection failed");
41+
if (!client.connect("arduinojson.org", 80)) {
42+
Serial.println(F("Connection failed"));
43+
return;
44+
}
45+
46+
Serial.println(F("Connected!"));
3947

40-
echo("Send HTTP request");
41-
client.println("GET /example.json HTTP/1.0");
42-
client.println("Host: arduinojson.org");
43-
client.println("Connection: close");
44-
client.println() || die("Failed to send request");
48+
// Send HTTP request
49+
client.println(F("GET /example.json HTTP/1.0"));
50+
client.println(F("Host: arduinojson.org"));
51+
client.println(F("Connection: close"));
52+
if (client.println() == 0) {
53+
Serial.println(F("Failed to send request"));
54+
return;
55+
}
4556

46-
echo("Check HTTP status");
57+
// Check HTTP status
4758
char status[32] = {0};
4859
client.readBytesUntil('\r', status, sizeof(status));
4960
if (strcmp(status, "HTTP/1.1 200 OK") != 0) {
50-
echo(status);
51-
die("Unexpected HTTP response");
61+
Serial.print(F("Unexpected response: "));
62+
Serial.println(status);
63+
return;
5264
}
5365

54-
echo("Skip HTTP headers");
66+
// Skip HTTP headers
5567
char endOfHeaders[] = "\r\n\r\n";
56-
client.find(endOfHeaders) || die("Invalid response");
68+
if (!client.find(endOfHeaders)) {
69+
Serial.println(F("Invalid response"));
70+
return;
71+
}
5772

58-
echo("Allocate JsonBuffer");
59-
const size_t BUFFER_SIZE = JSON_OBJECT_SIZE(3) + JSON_ARRAY_SIZE(2) + 60;
60-
DynamicJsonBuffer jsonBuffer(BUFFER_SIZE);
73+
// Allocate JsonBuffer
74+
// (see https://arduinojson.org/assistant/ to compute the capacity)
75+
const size_t capacity = JSON_OBJECT_SIZE(3) + JSON_ARRAY_SIZE(2) + 60;
76+
DynamicJsonBuffer jsonBuffer(capacity);
6177

62-
echo("Parse JSON object");
78+
// Parse JSON object
6379
JsonObject& root = jsonBuffer.parseObject(client);
64-
if (!root.success()) die("Parsing failed!");
80+
if (!root.success()) {
81+
Serial.println(F("Parsing failed!"));
82+
return;
83+
}
6584

66-
echo("Extract values");
67-
echo(root["sensor"].as<char*>());
68-
echo(root["time"].as<char*>());
69-
echo(root["data"][0].as<char*>());
70-
echo(root["data"][1].as<char*>());
85+
// Extract values
86+
Serial.println(F("Response:"));
87+
Serial.println(root["sensor"].as<char*>());
88+
Serial.println(root["time"].as<char*>());
89+
Serial.println(root["data"][0].as<char*>());
90+
Serial.println(root["data"][1].as<char*>());
7191

72-
echo("Disconnect");
92+
// Disconnect
7393
client.stop();
7494
}
7595

76-
void loop() {}
77-
78-
void echo(const char* message) {
79-
Serial.println(message);
96+
void loop() {
97+
// not used in this example
8098
}
8199

82-
bool die(const char* message) {
83-
Serial.println(message);
84-
while (true); // loop forever
85-
return false;
86-
}
100+
// See also
101+
// --------
102+
//
103+
// The website arduinojson.org contains the documentation for all the functions
104+
// used above. It also includes an FAQ that will help you solve any
105+
// serialization problem.
106+
// Please check it out at: https://arduinojson.org/
107+
//
108+
// The book "Mastering ArduinoJson" contains a tutorial on deserialization
109+
// showing how to parse the response from Yahoo Weather. In the last chapter,
110+
// it shows how to parse the huge documents from OpenWeatherMap
111+
// and Weather Underground.
112+
// Please check it out at: https://leanpub.com/arduinojson/

examples/JsonParserExample/JsonParserExample.ino

Lines changed: 20 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,21 @@
11
// ArduinoJson - arduinojson.org
22
// Copyright Benoit Blanchon 2014-2017
33
// MIT License
4+
//
5+
// This example shows how to deserialize a JSON document with ArduinoJson.
46

57
#include <ArduinoJson.h>
68

79
void setup() {
10+
// Initialize serial port
811
Serial.begin(9600);
9-
while (!Serial) {
10-
// wait serial port initialization
11-
}
12+
while (!Serial) continue;
1213

1314
// Memory pool for JSON object tree.
1415
//
15-
// Inside the brackets, 200 is the size of the pool in bytes,
16-
// If the JSON object is more complex, you need to increase that value.
17-
// See http://arduinojson.org/assistant/
16+
// Inside the brackets, 200 is the size of the pool in bytes.
17+
// Don't forget to change this value to match your JSON document.
18+
// See https://arduinojson.org/assistant/
1819
StaticJsonBuffer<200> jsonBuffer;
1920

2021
// StaticJsonBuffer allocates memory on the stack, it can be
@@ -62,3 +63,16 @@ void setup() {
6263
void loop() {
6364
// not used in this example
6465
}
66+
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://leanpub.com/arduinojson/

0 commit comments

Comments
 (0)
0