This commit is contained in:
2026-08-18 13:34:21 +08:00
parent 82697e1460
commit f92ced58df
179 changed files with 4707 additions and 5724 deletions

View File

@ -1,5 +1,5 @@
// ArduinoJson - https://arduinojson.org
// Copyright © 2014-2025, Benoit BLANCHON
// Copyright © 2014-2026, Benoit BLANCHON
// MIT License
//
// This example shows how to store your project configuration in a file.
@ -17,28 +17,35 @@
// * CLK <-> pin 13
// * CS <-> pin 4
//
// https://arduinojson.org/v7/example/config/
// https://arduinojson.org/v6/example/config/
#include <ArduinoJson.h>
#include <SD.h>
#include <SPI.h>
// Our configuration structure.
//
// Never use a JsonDocument to store the configuration!
// A JsonDocument is *not* a permanent storage; it's only a temporary storage
// used during the serialization phase. See:
// https://arduinojson.org/v6/faq/why-must-i-create-a-separate-config-object/
struct Config {
char hostname[64];
int port;
};
const char* filename = "/config.txt"; // <- SD library uses 8.3 filenames
const char *filename = "/config.txt"; // <- SD library uses 8.3 filenames
Config config; // <- global configuration object
// Loads the configuration from a file
void loadConfiguration(const char* filename, Config& config) {
void loadConfiguration(const char *filename, Config &config) {
// Open file for reading
File file = SD.open(filename);
// Allocate a temporary JsonDocument
JsonDocument doc;
// Don't forget to change the capacity to match your requirements.
// Use https://arduinojson.org/v6/assistant to compute the capacity.
StaticJsonDocument<512> doc;
// Deserialize the JSON document
DeserializationError error = deserializeJson(doc, file);
@ -56,7 +63,7 @@ void loadConfiguration(const char* filename, Config& config) {
}
// Saves the configuration to a file
void saveConfiguration(const char* filename, const Config& config) {
void saveConfiguration(const char *filename, const Config &config) {
// Delete existing file, otherwise the configuration is appended to the file
SD.remove(filename);
@ -68,7 +75,9 @@ void saveConfiguration(const char* filename, const Config& config) {
}
// Allocate a temporary JsonDocument
JsonDocument doc;
// Don't forget to change the capacity to match your requirements.
// Use https://arduinojson.org/assistant to compute the capacity.
StaticJsonDocument<256> doc;
// Set the values in the document
doc["hostname"] = config.hostname;
@ -84,7 +93,7 @@ void saveConfiguration(const char* filename, const Config& config) {
}
// Prints the content of a file to the Serial
void printFile(const char* filename) {
void printFile(const char *filename) {
// Open file for reading
File file = SD.open(filename);
if (!file) {
@ -105,8 +114,7 @@ void printFile(const char* filename) {
void setup() {
// Initialize serial port
Serial.begin(9600);
while (!Serial)
continue;
while (!Serial) continue;
// Initialize SD library
const int chipSelect = 4;
@ -136,7 +144,7 @@ void loop() {
// ------------------
//
// File is an unbuffered stream, which is not optimal for ArduinoJson.
// See: https://arduinojson.org/v7/how-to/improve-speed/
// See: https://arduinojson.org/v6/how-to/improve-speed/
// See also
// --------

View File

@ -1,18 +1,17 @@
// ArduinoJson - https://arduinojson.org
// Copyright © 2014-2025, Benoit BLANCHON
// Copyright © 2014-2026, Benoit BLANCHON
// MIT License
//
// This example shows how to use DeserializationOption::Filter
//
// https://arduinojson.org/v7/example/filter/
// https://arduinojson.org/v6/example/filter/
#include <ArduinoJson.h>
void setup() {
// Initialize serial port
Serial.begin(9600);
while (!Serial)
continue;
while (!Serial) continue;
// The huge input: an extract from OpenWeatherMap response
auto input_json = F(
@ -34,12 +33,12 @@ void setup() {
"1000000,\"timezone\":0,\"sunrise\":1581492085,\"sunset\":1581527294}}");
// The filter: it contains "true" for each value we want to keep
JsonDocument filter;
StaticJsonDocument<200> filter;
filter["list"][0]["dt"] = true;
filter["list"][0]["main"]["temp"] = true;
// Deserialize the document
JsonDocument doc;
StaticJsonDocument<400> doc;
deserializeJson(doc, input_json, DeserializationOption::Filter(filter));
// Print the result

View File

@ -1,32 +1,43 @@
// ArduinoJson - https://arduinojson.org
// Copyright © 2014-2025, Benoit BLANCHON
// Copyright © 2014-2026, Benoit BLANCHON
// MIT License
//
// This example shows how to generate a JSON document with ArduinoJson.
//
// https://arduinojson.org/v7/example/generator/
// https://arduinojson.org/v6/example/generator/
#include <ArduinoJson.h>
void setup() {
// Initialize Serial port
Serial.begin(9600);
while (!Serial)
continue;
while (!Serial) continue;
// Allocate the JSON document
JsonDocument doc;
//
// Inside the brackets, 200 is the RAM allocated to this document.
// Don't forget to change this value to match your requirement.
// Use https://arduinojson.org/v6/assistant to compute the capacity.
StaticJsonDocument<200> doc;
// StaticJsonObject allocates memory on the stack, it can be
// replaced by DynamicJsonDocument which allocates in the heap.
//
// DynamicJsonDocument doc(200);
// Add values in the document
//
doc["sensor"] = "gps";
doc["time"] = 1351824120;
// Add an array
JsonArray data = doc["data"].to<JsonArray>();
// Add an array.
//
JsonArray data = doc.createNestedArray("data");
data.add(48.756080);
data.add(2.302038);
// Generate the minified JSON and send it to the Serial port
// Generate the minified JSON and send it to the Serial port.
//
serializeJson(doc, Serial);
// The above line prints:
// {"sensor":"gps","time":1351824120,"data":[48.756080,2.302038]}
@ -34,7 +45,8 @@ void setup() {
// Start a new line
Serial.println();
// Generate the prettified JSON and send it to the Serial port
// Generate the prettified JSON and send it to the Serial port.
//
serializeJsonPretty(doc, Serial);
// The above line prints:
// {

View File

@ -1,5 +1,5 @@
// ArduinoJson - https://arduinojson.org
// Copyright © 2014-2025, Benoit BLANCHON
// Copyright © 2014-2026, Benoit BLANCHON
// MIT License
//
// This example shows how to parse a JSON document in an HTTP response.
@ -16,7 +16,7 @@
// ]
// }
//
// https://arduinojson.org/v7/example/http-client/
// https://arduinojson.org/v6/example/http-client/
#include <ArduinoJson.h>
#include <Ethernet.h>
@ -25,8 +25,7 @@
void setup() {
// Initialize Serial port
Serial.begin(9600);
while (!Serial)
continue;
while (!Serial) continue;
// Initialize Ethernet library
byte mac[] = {0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED};
@ -78,7 +77,9 @@ void setup() {
}
// Allocate the JSON document
JsonDocument doc;
// Use https://arduinojson.org/v6/assistant to compute the capacity.
const size_t capacity = JSON_OBJECT_SIZE(3) + JSON_ARRAY_SIZE(2) + 60;
DynamicJsonDocument doc(capacity);
// Parse JSON object
DeserializationError error = deserializeJson(doc, client);
@ -108,7 +109,7 @@ void loop() {
// ------------------
//
// EthernetClient is an unbuffered stream, which is not optimal for ArduinoJson.
// See: https://arduinojson.org/v7/how-to/improve-speed/
// See: https://arduinojson.org/v6/how-to/improve-speed/
// See also
// --------

View File

@ -1,37 +1,52 @@
// ArduinoJson - https://arduinojson.org
// Copyright © 2014-2025, Benoit BLANCHON
// Copyright © 2014-2026, Benoit BLANCHON
// MIT License
//
// This example shows how to deserialize a JSON document with ArduinoJson.
//
// https://arduinojson.org/v7/example/parser/
// https://arduinojson.org/v6/example/parser/
#include <ArduinoJson.h>
void setup() {
// Initialize serial port
Serial.begin(9600);
while (!Serial)
continue;
while (!Serial) continue;
// Allocate the JSON document
JsonDocument doc;
//
// Inside the brackets, 200 is the capacity of the memory pool in bytes.
// Don't forget to change this value to match your JSON document.
// Use https://arduinojson.org/v6/assistant to compute the capacity.
StaticJsonDocument<200> doc;
// StaticJsonDocument<N> allocates memory on the stack, it can be
// replaced by DynamicJsonDocument which allocates in the heap.
//
// DynamicJsonDocument doc(200);
// JSON input string.
const char* json =
//
// Using a char[], as shown here, enables the "zero-copy" mode. This mode uses
// the minimal amount of memory because the JsonDocument stores pointers to
// the input buffer.
// If you use another type of input, ArduinoJson must copy the strings from
// the input to the JsonDocument, so you need to increase the capacity of the
// JsonDocument.
char json[] =
"{\"sensor\":\"gps\",\"time\":1351824120,\"data\":[48.756080,2.302038]}";
// Deserialize the JSON document
DeserializationError error = deserializeJson(doc, json);
// Test if parsing succeeds
// Test if parsing succeeds.
if (error) {
Serial.print(F("deserializeJson() failed: "));
Serial.println(error.f_str());
return;
}
// Fetch the values
// Fetch values.
//
// Most of the time, you can rely on the implicit casts.
// In other case, you can do doc["time"].as<long>();
@ -40,7 +55,7 @@ void setup() {
double latitude = doc["data"][0];
double longitude = doc["data"][1];
// Print the values
// Print values.
Serial.println(sensor);
Serial.println(time);
Serial.println(latitude, 6);

View File

@ -1,5 +1,5 @@
// ArduinoJson - https://arduinojson.org
// Copyright © 2014-2025, Benoit BLANCHON
// Copyright © 2014-2026, Benoit BLANCHON
// MIT License
//
// This example shows how to implement an HTTP server that sends a JSON document
@ -13,7 +13,7 @@
// "digital": [1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 0, 0]
// }
//
// https://arduinojson.org/v7/example/http-server/
// https://arduinojson.org/v6/example/http-server/
#include <ArduinoJson.h>
#include <Ethernet.h>
@ -25,8 +25,7 @@ EthernetServer server(80);
void setup() {
// Initialize serial port
Serial.begin(9600);
while (!Serial)
continue;
while (!Serial) continue;
// Initialize Ethernet libary
if (!Ethernet.begin(mac)) {
@ -53,14 +52,14 @@ void loop() {
Serial.println(F("New client"));
// Read the request (we ignore the content in this example)
while (client.available())
client.read();
while (client.available()) client.read();
// Allocate a temporary JsonDocument
JsonDocument doc;
// Use https://arduinojson.org/v6/assistant to compute the capacity.
StaticJsonDocument<500> doc;
// Create the "analog" array
JsonArray analogValues = doc["analog"].to<JsonArray>();
JsonArray analogValues = doc.createNestedArray("analog");
for (int pin = 0; pin < 6; pin++) {
// Read the analog input
int value = analogRead(pin);
@ -70,7 +69,7 @@ void loop() {
}
// Create the "digital" array
JsonArray digitalValues = doc["digital"].to<JsonArray>();
JsonArray digitalValues = doc.createNestedArray("digital");
for (int pin = 0; pin < 14; pin++) {
// Read the digital input
int value = digitalRead(pin);
@ -102,7 +101,7 @@ void loop() {
// ------------------
//
// EthernetClient is an unbuffered stream, which is not optimal for ArduinoJson.
// See: https://arduinojson.org/v7/how-to/improve-speed/
// See: https://arduinojson.org/v6/how-to/improve-speed/
// See also
// --------

View File

@ -1,5 +1,5 @@
// ArduinoJson - https://arduinojson.org
// Copyright © 2014-2025, Benoit BLANCHON
// Copyright © 2014-2026, Benoit BLANCHON
// MIT License
//
// This example shows how to send a JSON document to a UDP socket.
@ -17,7 +17,7 @@
// $ ncat -ulp 8888
// See https://nmap.org/ncat/
//
// https://arduinojson.org/v7/example/udp-beacon/
// https://arduinojson.org/v6/example/udp-beacon/
#include <ArduinoJson.h>
#include <Ethernet.h>
@ -32,8 +32,7 @@ EthernetUDP udp;
void setup() {
// Initialize serial port
Serial.begin(9600);
while (!Serial)
continue;
while (!Serial) continue;
// Initialize Ethernet libary
if (!Ethernet.begin(mac)) {
@ -47,10 +46,11 @@ void setup() {
void loop() {
// Allocate a temporary JsonDocument
JsonDocument doc;
// Use https://arduinojson.org/v6/assistant to compute the capacity.
StaticJsonDocument<500> doc;
// Create the "analog" array
JsonArray analogValues = doc["analog"].to<JsonArray>();
JsonArray analogValues = doc.createNestedArray("analog");
for (int pin = 0; pin < 6; pin++) {
// Read the analog input
int value = analogRead(pin);
@ -60,7 +60,7 @@ void loop() {
}
// Create the "digital" array
JsonArray digitalValues = doc["digital"].to<JsonArray>();
JsonArray digitalValues = doc.createNestedArray("digital");
for (int pin = 0; pin < 14; pin++) {
// Read the digital input
int value = digitalRead(pin);
@ -90,7 +90,7 @@ void loop() {
// ------------------
//
// EthernetUDP is an unbuffered stream, which is not optimal for ArduinoJson.
// See: https://arduinojson.org/v7/how-to/improve-speed/
// See: https://arduinojson.org/v6/how-to/improve-speed/
// See also
// --------

View File

@ -1,24 +1,39 @@
// ArduinoJson - https://arduinojson.org
// Copyright © 2014-2025, Benoit BLANCHON
// Copyright © 2014-2026, Benoit BLANCHON
// MIT License
//
// This example shows how to deserialize a MessagePack document with
// ArduinoJson.
//
// https://arduinojson.org/v7/example/msgpack-parser/
// https://arduinojson.org/v6/example/msgpack-parser/
#include <ArduinoJson.h>
void setup() {
// Initialize serial port
Serial.begin(9600);
while (!Serial)
continue;
while (!Serial) continue;
// Allocate the JSON document
JsonDocument doc;
//
// Inside the brackets, 200 is the capacity of the memory pool in bytes.
// Don't forget to change this value to match your JSON document.
// Use https://arduinojson.org/v6/assistant to compute the capacity.
StaticJsonDocument<200> doc;
// The MessagePack input string
// StaticJsonObject allocates memory on the stack, it can be
// replaced by DynamicJsonObject which allocates in the heap.
//
// DynamicJsonObject doc(200);
// MessagePack input string.
//
// Using a char[], as shown here, enables the "zero-copy" mode. This mode uses
// the minimal amount of memory because the JsonDocument stores pointers to
// the input buffer.
// If you use another type of input, ArduinoJson must copy the strings from
// the input to the JsonDocument, so you need to increase the capacity of the
// JsonDocument.
uint8_t input[] = {131, 166, 115, 101, 110, 115, 111, 114, 163, 103, 112, 115,
164, 116, 105, 109, 101, 206, 80, 147, 50, 248, 164, 100,
97, 116, 97, 146, 203, 64, 72, 96, 199, 58, 188, 148,
@ -30,17 +45,16 @@ void setup() {
// "data": [48.75608, 2.302038]
// }
// Parse the input
DeserializationError error = deserializeMsgPack(doc, input);
// Test if parsing succeeded
// Test if parsing succeeded.
if (error) {
Serial.print("deserializeMsgPack() failed: ");
Serial.println(error.f_str());
return;
}
// Fetch the values
// Fetch values.
//
// Most of the time, you can rely on the implicit casts.
// In other case, you can do doc["time"].as<long>();
@ -49,7 +63,7 @@ void setup() {
double latitude = doc["data"][0];
double longitude = doc["data"][1];
// Print the values
// Print values.
Serial.println(sensor);
Serial.println(time);
Serial.println(latitude, 6);

View File

@ -1,5 +1,5 @@
// ArduinoJson - https://arduinojson.org
// Copyright © 2014-2025, Benoit BLANCHON
// Copyright © 2014-2026, Benoit BLANCHON
// MIT License
//
// This example shows the different ways you can use Flash strings with
@ -9,12 +9,12 @@
// JsonDocument. Prefer plain old char*, as they are more efficient in term of
// code size, speed, and memory usage.
//
// https://arduinojson.org/v7/example/progmem/
// https://arduinojson.org/v6/example/progmem/
#include <ArduinoJson.h>
void setup() {
JsonDocument doc;
DynamicJsonDocument doc(1024);
// You can use a Flash String as your JSON input.
// WARNING: the strings in the input will be duplicated in the JsonDocument.

View File

@ -1,5 +1,5 @@
// ArduinoJson - https://arduinojson.org
// Copyright © 2014-2025, Benoit BLANCHON
// Copyright © 2014-2026, Benoit BLANCHON
// MIT License
//
// This example shows the different ways you can use String with ArduinoJson.
@ -8,12 +8,12 @@
// JsonDocument. Prefer plain old char[], as they are more efficient in term of
// code size, speed, and memory usage.
//
// https://arduinojson.org/v7/example/string/
// https://arduinojson.org/v6/example/string/
#include <ArduinoJson.h>
void setup() {
JsonDocument doc;
DynamicJsonDocument doc(1024);
// You can use a String as your JSON input.
// WARNING: the string in the input will be duplicated in the JsonDocument.
@ -55,6 +55,7 @@ void setup() {
}
// Lastly, you can print the resulting JSON to a String
// WARNING: it doesn't replace the content but appends to it
String output;
serializeJson(doc, output);
}