mirror of
				https://github.com/eledio-devices/thirdparty-ArduinoJson.git
				synced 2025-10-31 08:42:39 +01:00 
			
		
		
		
	JsonVariant automatically promotes to JsonObject or JsonArray on write
This commit is contained in:
		| @@ -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); | ||||
|   | ||||
		Reference in New Issue
	
	Block a user