mirror of
				https://github.com/eledio-devices/thirdparty-ArduinoJson.git
				synced 2025-10-31 00:32:37 +01:00 
			
		
		
		
	JsonVariant automatically promotes to JsonObject or JsonArray on write
This commit is contained in:
		| @@ -15,7 +15,11 @@ | ||||
| #include <SD.h> | ||||
| #include <SPI.h> | ||||
|  | ||||
| // Configuration that we'll store on disk | ||||
| // 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. | ||||
| struct Config { | ||||
|   char hostname[64]; | ||||
|   int port; | ||||
| @@ -29,9 +33,9 @@ void loadConfiguration(const char *filename, Config &config) { | ||||
|   // Open file for reading | ||||
|   File file = SD.open(filename); | ||||
|  | ||||
|   // Allocate the document on the stack. | ||||
|   // Allocate a temporary JsonDocument | ||||
|   // Don't forget to change the capacity to match your requirements. | ||||
|   // Use arduinojson.org/assistant to compute the capacity. | ||||
|   // Use arduinojson.org/v6/assistant to compute the capacity. | ||||
|   StaticJsonDocument<512> doc; | ||||
|  | ||||
|   // Deserialize the JSON document | ||||
| @@ -39,16 +43,13 @@ void loadConfiguration(const char *filename, Config &config) { | ||||
|   if (error) | ||||
|     Serial.println(F("Failed to read file, using default configuration")); | ||||
|  | ||||
|   // Get the root object in the document | ||||
|   JsonObject root = doc.as<JsonObject>(); | ||||
|   // Copy values from the JsonDocument to the Config | ||||
|   config.port = doc["port"] | 2731; | ||||
|   strlcpy(config.hostname,                  // <- destination | ||||
|           doc["hostname"] | "example.com",  // <- source | ||||
|           sizeof(config.hostname));         // <- destination's capacity | ||||
|  | ||||
|   // Copy values from the JsonObject to the Config | ||||
|   config.port = root["port"] | 2731; | ||||
|   strlcpy(config.hostname,                   // <- destination | ||||
|           root["hostname"] | "example.com",  // <- source | ||||
|           sizeof(config.hostname));          // <- destination's capacity | ||||
|  | ||||
|   // Close the file (File's destructor doesn't close the file) | ||||
|   // Close the file (Curiously, File's destructor doesn't close the file) | ||||
|   file.close(); | ||||
| } | ||||
|  | ||||
| @@ -64,24 +65,21 @@ void saveConfiguration(const char *filename, const Config &config) { | ||||
|     return; | ||||
|   } | ||||
|  | ||||
|   // Allocate the document on the stack. | ||||
|   // Allocate a temporary JsonDocument | ||||
|   // Don't forget to change the capacity to match your requirements. | ||||
|   // Use arduinojson.org/assistant to compute the capacity. | ||||
|   StaticJsonDocument<256> doc; | ||||
|  | ||||
|   // Make our document contain an object | ||||
|   JsonObject root = doc.to<JsonObject>(); | ||||
|  | ||||
|   // Set the values in the object | ||||
|   root["hostname"] = config.hostname; | ||||
|   root["port"] = config.port; | ||||
|   // Set the values in the document | ||||
|   doc["hostname"] = config.hostname; | ||||
|   doc["port"] = config.port; | ||||
|  | ||||
|   // Serialize JSON to file | ||||
|   if (serializeJson(doc, file) == 0) { | ||||
|     Serial.println(F("Failed to write to file")); | ||||
|   } | ||||
|  | ||||
|   // Close the file (File's destructor doesn't close the file) | ||||
|   // Close the file | ||||
|   file.close(); | ||||
| } | ||||
|  | ||||
| @@ -100,7 +98,7 @@ void printFile(const char *filename) { | ||||
|   } | ||||
|   Serial.println(); | ||||
|  | ||||
|   // Close the file (File's destructor doesn't close the file) | ||||
|   // Close the file | ||||
|   file.close(); | ||||
| } | ||||
|  | ||||
|   | ||||
| @@ -15,7 +15,7 @@ void setup() { | ||||
|   // | ||||
|   // Inside the brackets, 200 is the RAM allocated to this document. | ||||
|   // Don't forget to change this value to match your requirement. | ||||
|   // Use arduinojson.org/assistant to compute the capacity. | ||||
|   // Use arduinojson.org/v6/assistant to compute the capacity. | ||||
|   StaticJsonDocument<200> doc; | ||||
|  | ||||
|   // StaticJsonObject allocates memory on the stack, it can be | ||||
| @@ -23,30 +23,30 @@ void setup() { | ||||
|   // | ||||
|   // DynamicJsonDocument  doc(200); | ||||
|  | ||||
|   // Make our document be an object | ||||
|   JsonObject root = doc.to<JsonObject>(); | ||||
|  | ||||
|   // Add values in the object | ||||
|   // Add values in the document | ||||
|   // | ||||
|   // Most of the time, you can rely on the implicit casts. | ||||
|   // In other case, you can do root.set<long>("time", 1351824120); | ||||
|   root["sensor"] = "gps"; | ||||
|   root["time"] = 1351824120; | ||||
|   doc["sensor"] = "gps"; | ||||
|   doc["time"] = 1351824120; | ||||
|  | ||||
|   // Add an array. | ||||
|   // | ||||
|   JsonArray data = root.createNestedArray("data"); | ||||
|   JsonArray data = doc.createNestedArray("data"); | ||||
|   data.add(48.756080); | ||||
|   data.add(2.302038); | ||||
|  | ||||
|   // Generate the minified JSON and send it to the Serial port. | ||||
|   // | ||||
|   serializeJson(doc, Serial); | ||||
|   // This prints: | ||||
|   // The above line prints: | ||||
|   // {"sensor":"gps","time":1351824120,"data":[48.756080,2.302038]} | ||||
|  | ||||
|   // Start a new line | ||||
|   Serial.println(); | ||||
|  | ||||
|   // Generate the prettified JSON and send it to the Serial port. | ||||
|   // | ||||
|   serializeJsonPretty(doc, Serial); | ||||
|   // This prints: | ||||
|   // The above line prints: | ||||
|   // { | ||||
|   //   "sensor": "gps", | ||||
|   //   "time": 1351824120, | ||||
|   | ||||
| @@ -71,7 +71,7 @@ void setup() { | ||||
|   } | ||||
|  | ||||
|   // Allocate the JSON document | ||||
|   // Use arduinojson.org/assistant to compute the capacity. | ||||
|   // Use arduinojson.org/v6/assistant to compute the capacity. | ||||
|   const size_t capacity = JSON_OBJECT_SIZE(3) + JSON_ARRAY_SIZE(2) + 60; | ||||
|   DynamicJsonDocument doc(capacity); | ||||
|  | ||||
| @@ -84,12 +84,11 @@ void setup() { | ||||
|   } | ||||
|  | ||||
|   // Extract values | ||||
|   JsonObject root = doc.as<JsonObject>(); | ||||
|   Serial.println(F("Response:")); | ||||
|   Serial.println(root["sensor"].as<char*>()); | ||||
|   Serial.println(root["time"].as<char*>()); | ||||
|   Serial.println(root["data"][0].as<char*>()); | ||||
|   Serial.println(root["data"][1].as<char*>()); | ||||
|   Serial.println(doc["sensor"].as<char*>()); | ||||
|   Serial.println(doc["time"].as<long>()); | ||||
|   Serial.println(doc["data"][0].as<float>(), 6); | ||||
|   Serial.println(doc["data"][1].as<float>(), 6); | ||||
|  | ||||
|   // Disconnect | ||||
|   client.stop(); | ||||
|   | ||||
| @@ -13,9 +13,9 @@ void setup() { | ||||
|  | ||||
|   // Allocate the JSON document | ||||
|   // | ||||
|   // Inside the brackets, 200 is the size of the memory pool in bytes. | ||||
|   // 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 arduinojson.org/assistant to compute the capacity. | ||||
|   // Use arduinojson.org/v6/assistant to compute the capacity. | ||||
|   StaticJsonDocument<200> doc; | ||||
|  | ||||
|   // StaticJsonDocument<N> allocates memory on the stack, it can be | ||||
| @@ -25,9 +25,12 @@ void setup() { | ||||
|  | ||||
|   // JSON input string. | ||||
|   // | ||||
|   // It's better to use a char[] as shown here. | ||||
|   // If you use a const char* or a String, ArduinoJson will | ||||
|   // have to make a copy of the input in the JsonBuffer. | ||||
|   // 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]}"; | ||||
|  | ||||
| @@ -41,17 +44,14 @@ void setup() { | ||||
|     return; | ||||
|   } | ||||
|  | ||||
|   // Get the root object in the document | ||||
|   JsonObject root = doc.as<JsonObject>(); | ||||
|  | ||||
|   // Fetch values. | ||||
|   // | ||||
|   // Most of the time, you can rely on the implicit casts. | ||||
|   // In other case, you can do root["time"].as<long>(); | ||||
|   const char* sensor = root["sensor"]; | ||||
|   long time = root["time"]; | ||||
|   double latitude = root["data"][0]; | ||||
|   double longitude = root["data"][1]; | ||||
|   // In other case, you can do doc["time"].as<long>(); | ||||
|   const char* sensor = doc["sensor"]; | ||||
|   long time = doc["time"]; | ||||
|   double latitude = doc["data"][0]; | ||||
|   double longitude = doc["data"][1]; | ||||
|  | ||||
|   // Print values. | ||||
|   Serial.println(sensor); | ||||
|   | ||||
| @@ -2,15 +2,15 @@ | ||||
| // Copyright Benoit Blanchon 2014-2018 | ||||
| // MIT License | ||||
| // | ||||
| // This example shows how to implement an HTTP server that sends JSON document | ||||
| // in the responses. | ||||
| // This example shows how to implement an HTTP server that sends a JSON document | ||||
| // in the response. | ||||
| // It uses the Ethernet library but can be easily adapted for Wifi. | ||||
| // | ||||
| // It sends the value of the analog and digital pins. | ||||
| // The JSON document looks like the following: | ||||
| // The JSON document contains the values of the analog and digital pins. | ||||
| // It looks like that: | ||||
| // { | ||||
| //   "analog": [ 0, 1, 2, 3, 4, 5 ], | ||||
| //   "digital": [ 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13 ] | ||||
| //   "analog": [0, 76, 123, 158, 192, 205], | ||||
| //   "digital": [1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 0, 0] | ||||
| // } | ||||
|  | ||||
| #include <ArduinoJson.h> | ||||
| @@ -51,15 +51,12 @@ void loop() { | ||||
|   // Read the request (we ignore the content in this example) | ||||
|   while (client.available()) client.read(); | ||||
|  | ||||
|   // Allocate the JSON document | ||||
|   // Use arduinojson.org/assistant to compute the capacity. | ||||
|   // Allocate a temporary JsonDocument | ||||
|   // Use arduinojson.org/v6/assistant to compute the capacity. | ||||
|   StaticJsonDocument<500> doc; | ||||
|  | ||||
|   // Make our document represent an object | ||||
|   JsonObject root = doc.to<JsonObject>(); | ||||
|  | ||||
|   // Create the "analog" array | ||||
|   JsonArray analogValues = root.createNestedArray("analog"); | ||||
|   JsonArray analogValues = doc.createNestedArray("analog"); | ||||
|   for (int pin = 0; pin < 6; pin++) { | ||||
|     // Read the analog input | ||||
|     int value = analogRead(pin); | ||||
| @@ -69,7 +66,7 @@ void loop() { | ||||
|   } | ||||
|  | ||||
|   // Create the "digital" array | ||||
|   JsonArray digitalValues = root.createNestedArray("digital"); | ||||
|   JsonArray digitalValues = doc.createNestedArray("digital"); | ||||
|   for (int pin = 0; pin < 14; pin++) { | ||||
|     // Read the digital input | ||||
|     int value = digitalRead(pin); | ||||
| @@ -83,9 +80,11 @@ void loop() { | ||||
|   Serial.println(); | ||||
|  | ||||
|   // Write response headers | ||||
|   client.println("HTTP/1.0 200 OK"); | ||||
|   client.println("Content-Type: application/json"); | ||||
|   client.println("Connection: close"); | ||||
|   client.println(F("HTTP/1.0 200 OK")); | ||||
|   client.println(F("Content-Type: application/json")); | ||||
|   client.println(F("Connection: close")); | ||||
|   client.print(F("Content-Length: ")); | ||||
|   client.println(measureJsonPretty(doc)); | ||||
|   client.println(); | ||||
|  | ||||
|   // Write JSON document | ||||
|   | ||||
| @@ -5,10 +5,10 @@ | ||||
| // This example shows how to send a JSON document to a UDP socket. | ||||
| // At regular interval, it sends a UDP packet that contains the status of | ||||
| // analog and digital pins. | ||||
| // The JSON document looks like the following: | ||||
| // It looks like that: | ||||
| // { | ||||
| //   "analog": [ 0, 1, 2, 3, 4, 5 ], | ||||
| //   "digital": [ 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13 ] | ||||
| //   "analog": [0, 76, 123, 158, 192, 205], | ||||
| //   "digital": [1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 0, 0] | ||||
| // } | ||||
| // | ||||
| // If you want to test this program, you need to be able to receive the UDP | ||||
| @@ -43,15 +43,12 @@ void setup() { | ||||
| } | ||||
|  | ||||
| void loop() { | ||||
|   // Allocate the JSON document | ||||
|   // Use arduinojson.org/assistant to compute the capacity. | ||||
|   // Allocate a temporary JsonDocument | ||||
|   // Use arduinojson.org/v6/assistant to compute the capacity. | ||||
|   StaticJsonDocument<500> doc; | ||||
|  | ||||
|   // Make our document represent an object | ||||
|   JsonObject root = doc.to<JsonObject>(); | ||||
|  | ||||
|   // Create the "analog" array | ||||
|   JsonArray analogValues = root.createNestedArray("analog"); | ||||
|   JsonArray analogValues = doc.createNestedArray("analog"); | ||||
|   for (int pin = 0; pin < 6; pin++) { | ||||
|     // Read the analog input | ||||
|     int value = analogRead(pin); | ||||
| @@ -61,7 +58,7 @@ void loop() { | ||||
|   } | ||||
|  | ||||
|   // Create the "digital" array | ||||
|   JsonArray digitalValues = root.createNestedArray("digital"); | ||||
|   JsonArray digitalValues = doc.createNestedArray("digital"); | ||||
|   for (int pin = 0; pin < 14; pin++) { | ||||
|     // Read the digital input | ||||
|     int value = digitalRead(pin); | ||||
|   | ||||
| @@ -14,9 +14,9 @@ void setup() { | ||||
|  | ||||
|   // Allocate the JSON document | ||||
|   // | ||||
|   // Inside the brackets, 200 is the size of the memory pool in bytes. | ||||
|   // 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 arduinojson.org/assistant to compute the capacity. | ||||
|   // Use arduinojson.org/v6/assistant to compute the capacity. | ||||
|   StaticJsonDocument<200> doc; | ||||
|  | ||||
|   // StaticJsonObject allocates memory on the stack, it can be | ||||
| @@ -26,9 +26,12 @@ void setup() { | ||||
|  | ||||
|   // MessagePack input string. | ||||
|   // | ||||
|   // It's better to use a char[] as shown here. | ||||
|   // If you use a const char* or a String, ArduinoJson will | ||||
|   // have to make a copy of the input in the JsonBuffer. | ||||
|   // 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, | ||||
| @@ -40,31 +43,23 @@ void setup() { | ||||
|   //   "data": [48.75608, 2.302038] | ||||
|   // } | ||||
|  | ||||
|   // doc of the object tree. | ||||
|   // | ||||
|   // It's a reference to the JsonObject, the actual bytes are inside the | ||||
|   // JsonBuffer with all the other nodes of the object tree. | ||||
|   // Memory is freed when jsonBuffer goes out of scope. | ||||
|   DeserializationError error = deserializeMsgPack(doc, input); | ||||
|  | ||||
|   // Test if parsing succeeds. | ||||
|   // Test if parsing succeeded. | ||||
|   if (error) { | ||||
|     Serial.print("deserializeMsgPack() failed: "); | ||||
|     Serial.println(error.c_str()); | ||||
|     return; | ||||
|   } | ||||
|  | ||||
|   // Get the root object in the document | ||||
|   JsonObject root = doc.as<JsonObject>(); | ||||
|  | ||||
|   // Fetch values. | ||||
|   // | ||||
|   // Most of the time, you can rely on the implicit casts. | ||||
|   // In other case, you can do root["time"].as<long>(); | ||||
|   const char* sensor = root["sensor"]; | ||||
|   long time = root["time"]; | ||||
|   double latitude = root["data"][0]; | ||||
|   double longitude = root["data"][1]; | ||||
|   // In other case, you can do doc["time"].as<long>(); | ||||
|   const char* sensor = doc["sensor"]; | ||||
|   long time = doc["time"]; | ||||
|   double latitude = doc["data"][0]; | ||||
|   double longitude = doc["data"][1]; | ||||
|  | ||||
|   // Print values. | ||||
|   Serial.println(sensor); | ||||
|   | ||||
| @@ -6,7 +6,7 @@ | ||||
| // ArduinoJson. | ||||
| // | ||||
| // Use Flash strings sparingly, because ArduinoJson duplicates them in the | ||||
| // JsonBuffer. Prefer plain old char*, as they are more efficient in term of | ||||
| // JsonDocument. Prefer plain old char*, as they are more efficient in term of | ||||
| // code size, speed, and memory usage. | ||||
|  | ||||
| #include <ArduinoJson.h> | ||||
| @@ -17,8 +17,7 @@ void setup() { | ||||
|   DynamicJsonDocument doc(1024); | ||||
|  | ||||
|   // You can use a Flash String as your JSON input. | ||||
|   // WARNING: the content of the Flash String will be duplicated in the | ||||
|   // JsonBuffer. | ||||
|   // WARNING: the string in the input  will be duplicated in the JsonDocument. | ||||
|   deserializeJson(doc, F("{\"sensor\":\"gps\",\"time\":1351824120," | ||||
|                          "\"data\":[48.756080,2.302038]}")); | ||||
|   JsonObject obj = doc.as<JsonObject>(); | ||||
| @@ -29,12 +28,12 @@ void setup() { | ||||
|  | ||||
|   // You can use a Flash String to set an element of a JsonObject | ||||
|   // WARNING: the content of the Flash String will be duplicated in the | ||||
|   // JsonBuffer. | ||||
|   // JsonDocument. | ||||
|   obj[F("time")] = time; | ||||
|  | ||||
|   // You can set a Flash String to a JsonObject or JsonArray: | ||||
|   // WARNING: the content of the Flash String will be duplicated in the | ||||
|   // JsonBuffer. | ||||
|   // JsonDocument. | ||||
|   obj["sensor"] = F("gps"); | ||||
|  | ||||
|   // It works with serialized() too: | ||||
|   | ||||
| @@ -5,7 +5,7 @@ | ||||
| // This example shows the different ways you can use String with ArduinoJson. | ||||
| // | ||||
| // Use String objects sparingly, because ArduinoJson duplicates them in the | ||||
| // JsonBuffer. Prefer plain old char[], as they are more efficient in term of | ||||
| // JsonDocument. Prefer plain old char[], as they are more efficient in term of | ||||
| // code size, speed, and memory usage. | ||||
|  | ||||
| #include <ArduinoJson.h> | ||||
| @@ -14,7 +14,7 @@ void setup() { | ||||
|   DynamicJsonDocument doc(1024); | ||||
|  | ||||
|   // You can use a String as your JSON input. | ||||
|   // WARNING: the content of the String will be duplicated in the JsonBuffer. | ||||
|   // WARNING: the string in the input  will be duplicated in the JsonDocument. | ||||
|   String input = | ||||
|       "{\"sensor\":\"gps\",\"time\":1351824120,\"data\":[48.756080,2.302038]}"; | ||||
|   deserializeJson(doc, input); | ||||
| @@ -25,11 +25,11 @@ void setup() { | ||||
|   long time = obj[String("time")]; | ||||
|  | ||||
|   // You can use a String to set an element of a JsonObject | ||||
|   // WARNING: the content of the String will be duplicated in the JsonBuffer. | ||||
|   // WARNING: the content of the String will be duplicated in the JsonDocument. | ||||
|   obj[String("time")] = time; | ||||
|  | ||||
|   // You can get a String from a JsonObject or JsonArray: | ||||
|   // No duplication is done, at least not in the JsonBuffer. | ||||
|   // No duplication is done, at least not in the JsonDocument. | ||||
|   String sensor = obj["sensor"]; | ||||
|  | ||||
|   // Unfortunately, the following doesn't work (issue #118): | ||||
| @@ -38,14 +38,14 @@ void setup() { | ||||
|   sensor = obj["sensor"].as<String>(); | ||||
|  | ||||
|   // You can set a String to a JsonObject or JsonArray: | ||||
|   // WARNING: the content of the String will be duplicated in the JsonBuffer. | ||||
|   // WARNING: the content of the String will be duplicated in the JsonDocument. | ||||
|   obj["sensor"] = sensor; | ||||
|  | ||||
|   // It works with serialized() too: | ||||
|   obj["sensor"] = serialized(sensor); | ||||
|  | ||||
|   // You can also concatenate strings | ||||
|   // WARNING: the content of the String will be duplicated in the JsonBuffer. | ||||
|   // WARNING: the content of the String will be duplicated in the JsonDocument. | ||||
|   obj[String("sen") + "sor"] = String("gp") + "s"; | ||||
|  | ||||
|   // You can compare the content of a JsonObject with a String | ||||
|   | ||||
		Reference in New Issue
	
	Block a user