first commit

This commit is contained in:
Jérôme Delacotte
2025-03-06 11:15:32 +01:00
commit 7b30d6e298
5276 changed files with 2108927 additions and 0 deletions

View File

@@ -0,0 +1,104 @@
/*
JSON Array
This sketch demonstrates how to use various features
of the Official Arduino_JSON library, in particular for JSON arrays.
This example code is in the public domain.
*/
#include <Arduino_JSON.h>
const char input[] = "[true, 42, \"apple\"]";
void setup() {
Serial.begin(9600);
while (!Serial);
demoParse();
demoCreation();
}
void loop() {
}
void demoParse() {
Serial.println("parse");
Serial.println("=====");
JSONVar myArray = JSON.parse(input);
// JSON.typeof(jsonVar) can be used to get the type of the variable
if (JSON.typeof(myArray) == "undefined") {
Serial.println("Parsing input failed!");
return;
}
Serial.print("JSON.typeof(myArray) = ");
Serial.println(JSON.typeof(myArray)); // prints: array
// myArray.length() can be used to get the length of the array
Serial.print("myArray.length() = ");
Serial.println(myArray.length());
Serial.println();
Serial.print("JSON.typeof(myArray[0]) = ");
Serial.println(JSON.typeof(myArray[0]));
Serial.print("myArray[0] = ");
Serial.println(myArray[0]);
Serial.println();
Serial.print("myArray[1] = ");
Serial.println((int) myArray[1]);
Serial.println();
Serial.print("myArray[2] = ");
Serial.println((const char*) myArray[2]);
Serial.println();
Serial.println();
}
void demoCreation() {
Serial.println("creation");
Serial.println("========");
JSONVar myArray;
myArray[0] = false;
myArray[1] = 4242.5;
myArray[2] = "orange";
myArray[3] = "world";
myArray[4] = true;
myArray[5] = 42;
Serial.print("myArray.length() = ");
Serial.println(myArray.length());
// JSON.stringify(myVar) can be used to convert the JSONVar to a String
String jsonString = JSON.stringify(myArray);
Serial.print("JSON.stringify(myArray) = ");
Serial.println(jsonString);
Serial.println();
for (int i = 0; i < myArray.length(); i++) {
JSONVar value = myArray[i];
Serial.print("JSON.typeof(myArray[");
Serial.print(i);
Serial.print("]) = ");
Serial.println(JSON.typeof(value));
Serial.print("myArray[");
Serial.print(i);
Serial.print("] = ");
Serial.println(value);
Serial.println();
}
Serial.println();
}

View File

@@ -0,0 +1,165 @@
/*
JSON Kitchen Sink
This sketch demonstrates how to use various features
of the Official Arduino_JSON library.
This example code is in the public domain.
*/
#include <Arduino_JSON.h>
void setup() {
Serial.begin(9600);
while (!Serial);
// boolean
booleanDemo();
intDemo();
doubleDemo();
stringDemo();
arrayDemo();
objectDemo();
}
void loop() {
}
void booleanDemo() {
Serial.println("boolean");
Serial.println("=======");
JSONVar myBoolean = true;
Serial.print("JSON.typeof(myBoolean) = ");
Serial.println(JSON.typeof(myBoolean)); // prints: boolean
Serial.print("myBoolean = ");
Serial.println(myBoolean); // prints: true
myBoolean = false;
Serial.print("myBoolean = ");
Serial.println((boolean) myBoolean); // prints: 0
Serial.println();
}
void intDemo() {
Serial.println("int");
Serial.println("===");
JSONVar myInt = 42;
Serial.print("JSON.typeof(myInt) = ");
Serial.println(JSON.typeof(myInt)); // prints: number
Serial.print("myInt = ");
Serial.println(myInt); // prints: 42
myInt = 4242;
Serial.print("myInt = ");
Serial.println((int) myInt); // prints: 4242
Serial.println();
}
void doubleDemo() {
Serial.println("double");
Serial.println("======");
JSONVar myDouble = 42.5;
Serial.print("JSON.typeof(myDouble) = ");
Serial.println(JSON.typeof(myDouble)); // prints: number
Serial.print("myDouble = ");
Serial.println(myDouble); // prints: 42.5
myDouble = 4242.4242;
Serial.print("myDouble = ");
Serial.println((double) myDouble, 4); // prints: 4242.4242
Serial.println();
}
void stringDemo() {
Serial.println("string");
Serial.println("======");
JSONVar myString = "Hello World!";
Serial.print("JSON.typeof(myString) = ");
Serial.println(JSON.typeof(myString)); // prints: string
Serial.print("myString = ");
Serial.println(myString); // prints: Hello World!
myString = ":)";
Serial.print("myString = ");
Serial.println((const char*) myString); // prints: :)
Serial.println();
}
void arrayDemo() {
Serial.println("array");
Serial.println("=====");
JSONVar myArray;
myArray[0] = 42;
Serial.print("JSON.typeof(myArray) = ");
Serial.println(JSON.typeof(myArray)); // prints: array
Serial.print("myArray = ");
Serial.println(myArray); // prints: [42]
Serial.print("myArray[0] = ");
Serial.println((int)myArray[0]); // prints: 42
myArray[1] = 42.5;
Serial.print("myArray = ");
Serial.println(myArray); // prints: [42,42.5]
Serial.print("myArray[1] = ");
Serial.println((double)myArray[1]); // prints: 42.50
Serial.println();
}
void objectDemo() {
Serial.println("object");
Serial.println("======");
JSONVar myObject;
myObject["foo"] = "bar";
Serial.print("JSON.typeof(myObject) = ");
Serial.println(JSON.typeof(myObject)); // prints: object
Serial.print("myObject.keys() = ");
Serial.println(myObject.keys()); // prints: ["foo"]
Serial.print("myObject = ");
Serial.println(myObject); // prints: {"foo":"bar"}
myObject["blah"]["abc"] = 42;
Serial.print("myObject.keys() = ");
Serial.println(myObject.keys()); // prints: ["foo","blah"]
Serial.print("myObject = ");
Serial.println(myObject); // prints: {"foo":"bar","blah":{"abc":42}}
}

View File

@@ -0,0 +1,136 @@
/*
JSON Object
This sketch demonstrates how to use various features
of the Official Arduino_JSON library, in particular for JSON objects.
This example code is in the public domain.
*/
#include <Arduino_JSON.h>
#include <assert.h>
const char input[] = "{\"result\":true,\"count\":42,\"foo\":\"bar\"}";
void setup() {
Serial.begin(9600);
while (!Serial);
demoParse();
demoCreation();
}
void loop() {
}
void demoParse() {
Serial.println("parse");
Serial.println("=====");
JSONVar myObject = JSON.parse(input);
// JSON.typeof(jsonVar) can be used to get the type of the variable
if (JSON.typeof(myObject) == "undefined") {
Serial.println("Parsing input failed!");
return;
}
Serial.print("JSON.typeof(myObject) = ");
Serial.println(JSON.typeof(myObject)); // prints: object
// myObject.hasOwnProperty(key) checks if the object contains an entry for key
if (myObject.hasOwnProperty("result")) {
Serial.print("myObject[\"result\"] = ");
Serial.println((bool) myObject["result"]);
}
if (myObject.hasOwnProperty("count")) {
Serial.print("myObject[\"count\"] = ");
Serial.println((int) myObject["count"]);
}
if (myObject.hasOwnProperty("count")) {
Serial.print("myObject[\"count\"] = ");
Serial.println((double) myObject["count"]);
}
if (myObject.hasOwnProperty("foo")) {
Serial.print("myObject[\"foo\"] = ");
Serial.println((const char*) myObject["foo"]);
}
// JSONVars can be printed using print or println
Serial.print("myObject = ");
Serial.println(myObject);
Serial.println();
}
void demoCreation() {
Serial.println("creation");
Serial.println("========");
JSONVar myObject;
myObject["hello"] = "world";
myObject["true"] = true;
myObject["x1"] = (int) 42;
myObject["x2"] = (long) 42;
myObject["x3"] = (unsigned long) 42;
int x1 = myObject["x1"];
assert(x1 == 42);
long x2 = myObject["x2"];
assert(x2 == 42);
unsigned long x3 = myObject["x3"];
assert(x3 == 42);
Serial.print("myObject.keys() = ");
Serial.println(myObject.keys());
// JSON.stringify(myVar) can be used to convert the JSONVar to a String
String jsonString = JSON.stringify(myObject);
Serial.print("JSON.stringify(myObject) = ");
Serial.println(jsonString);
Serial.println();
// myObject.keys() can be used to get an array of all the keys in the object
JSONVar keys = myObject.keys();
for (int i = 0; i < keys.length(); i++) {
JSONVar value = myObject[keys[i]];
Serial.print("JSON.typeof(myObject[");
Serial.print(keys[i]);
Serial.print("]) = ");
Serial.println(JSON.typeof(value));
Serial.print("myObject[");
Serial.print(keys[i]);
Serial.print("] = ");
Serial.println(value);
Serial.println();
}
Serial.println();
// setting a value to undefined can remove it from the object
myObject["x"] = undefined;
// you can also change a value
myObject["hello"] = "there!";
Serial.print("myObject = ");
Serial.println(myObject);
}

View File

@@ -0,0 +1,69 @@
/*
JSON Value Extractor
This sketch demonstrates how to use some features
of the Official Arduino JSON library to traverse through all the
key value pair in the object and the nested objects.
Can be very helpful when searching for a specific data in a key
which is nested at multiple levels
The sketch actually use recursion to traverse all the keys in
a given JSON.
Example originally added on 24-03-2020
by Madhur Dixit https://github.com/Chester-King
This example code is in the public domain.
*/
#include <Arduino_JSON.h>
void setup() {
Serial.begin(9600);
while (!Serial);
valueExtractor();
}
void loop() {
}
void valueExtractor() {
Serial.println("object");
Serial.println("======");
JSONVar myObject;
// Making a JSON Object
myObject["foo"] = "bar";
myObject["blah"]["abc"] = 42;
myObject["blah"]["efg"] = "pod";
myObject["blah"]["cde"]["pan1"] = "King";
myObject["blah"]["cde"]["pan2"] = 3.14;
myObject["jok"]["hij"] = "bar";
Serial.println(myObject);
Serial.println();
Serial.println("Extracted Values");
Serial.println("======");
objRec(myObject);
}
void objRec(JSONVar myObject) {
Serial.println("{");
for (int x = 0; x < myObject.keys().length(); x++) {
if ((JSON.typeof(myObject[myObject.keys()[x]])).equals("object")) {
Serial.print(myObject.keys()[x]);
Serial.println(" : ");
objRec(myObject[myObject.keys()[x]]);
}
else {
Serial.print(myObject.keys()[x]);
Serial.print(" : ");
Serial.println(myObject[myObject.keys()[x]]);
}
}
Serial.println("}");
}