mirror of
				https://github.com/eledio-devices/thirdparty-ArduinoJson.git
				synced 2025-10-31 08:42:39 +01:00 
			
		
		
		
	Return JsonArray and JsonObject by value (closes #309)
				
					
				
			This commit is contained in:
		| @@ -40,7 +40,7 @@ void loadConfiguration(const char *filename, Config &config) { | ||||
|     Serial.println(F("Failed to read file, using default configuration")); | ||||
|  | ||||
|   // Get the root object in the document | ||||
|   JsonObject &root = doc.as<JsonObject>(); | ||||
|   JsonObject root = doc.as<JsonObject>(); | ||||
|  | ||||
|   // Copy values from the JsonObject to the Config | ||||
|   config.port = root["port"] | 2731; | ||||
| @@ -70,7 +70,7 @@ void saveConfiguration(const char *filename, const Config &config) { | ||||
|   StaticJsonDocument<256> doc; | ||||
|  | ||||
|   // Make our document contain an object | ||||
|   JsonObject &root = doc.to<JsonObject>(); | ||||
|   JsonObject root = doc.to<JsonObject>(); | ||||
|  | ||||
|   // Set the values in the object | ||||
|   root["hostname"] = config.hostname; | ||||
| @@ -132,15 +132,4 @@ void loop() { | ||||
|   // not used in this example | ||||
| } | ||||
|  | ||||
| // See also | ||||
| // -------- | ||||
| // | ||||
| // The website arduinojson.org contains the documentation for all the functions | ||||
| // used above. It also includes an FAQ that will help you solve any | ||||
| // serialization or deserialization problem. | ||||
| // Please check it out at: https://arduinojson.org/ | ||||
| // | ||||
| // The book "Mastering ArduinoJson" contains a case study of a project that has | ||||
| // a complex configuration with nested members. | ||||
| // Contrary to this example, the project in the book uses the SPIFFS filesystem. | ||||
| // Please check it out at: https://arduinojson.org/book/ | ||||
| // Visit https://arduinojson.org/v6/example/config/ for more. | ||||
|   | ||||
| @@ -24,7 +24,7 @@ void setup() { | ||||
|   // DynamicJsonDocument  doc(200); | ||||
|  | ||||
|   // Make our document be an object | ||||
|   JsonObject& root = doc.to<JsonObject>(); | ||||
|   JsonObject root = doc.to<JsonObject>(); | ||||
|  | ||||
|   // Add values in the object | ||||
|   // | ||||
| @@ -35,7 +35,7 @@ void setup() { | ||||
|  | ||||
|   // Add an array. | ||||
|   // | ||||
|   JsonArray& data = root.createNestedArray("data"); | ||||
|   JsonArray data = root.createNestedArray("data"); | ||||
|   data.add(48.756080); | ||||
|   data.add(2.302038); | ||||
|  | ||||
| @@ -61,15 +61,4 @@ void loop() { | ||||
|   // not used in this example | ||||
| } | ||||
|  | ||||
| // See also | ||||
| // -------- | ||||
| // | ||||
| // The website arduinojson.org contains the documentation for all the functions | ||||
| // used above. It also includes an FAQ that will help you solve any | ||||
| // serialization problem. | ||||
| // Please check it out at: https://arduinojson.org/ | ||||
| // | ||||
| // The book "Mastering ArduinoJson" contains a tutorial on serialization. | ||||
| // It begins with a simple example, like the one above, and then adds more | ||||
| // features like serializing directly to a file or an HTTP request. | ||||
| // Please check it out at: https://arduinojson.org/book/ | ||||
| // Visit https://arduinojson.org/v6/example/generator/ for more. | ||||
|   | ||||
| @@ -84,7 +84,7 @@ void setup() { | ||||
|   } | ||||
|  | ||||
|   // Extract values | ||||
|   JsonObject& root = doc.as<JsonObject>(); | ||||
|   JsonObject root = doc.as<JsonObject>(); | ||||
|   Serial.println(F("Response:")); | ||||
|   Serial.println(root["sensor"].as<char*>()); | ||||
|   Serial.println(root["time"].as<char*>()); | ||||
| @@ -99,16 +99,4 @@ void loop() { | ||||
|   // not used in this example | ||||
| } | ||||
|  | ||||
| // See also | ||||
| // -------- | ||||
| // | ||||
| // The website arduinojson.org contains the documentation for all the functions | ||||
| // used above. It also includes an FAQ that will help you solve any | ||||
| // serialization  problem. | ||||
| // Please check it out at: https://arduinojson.org/ | ||||
| // | ||||
| // The book "Mastering ArduinoJson" contains a tutorial on deserialization | ||||
| // showing how to parse the response from Yahoo Weather. In the last chapter, | ||||
| // it shows how to parse the huge documents from OpenWeatherMap | ||||
| // and Weather Underground. | ||||
| // Please check it out at: https://arduinojson.org/book/ | ||||
| // Visit https://arduinojson.org/v6/example/http-client/ for more. | ||||
|   | ||||
| @@ -42,7 +42,7 @@ void setup() { | ||||
|   } | ||||
|  | ||||
|   // Get the root object in the document | ||||
|   JsonObject& root = doc.as<JsonObject>(); | ||||
|   JsonObject root = doc.as<JsonObject>(); | ||||
|  | ||||
|   // Fetch values. | ||||
|   // | ||||
| @@ -64,15 +64,4 @@ void loop() { | ||||
|   // not used in this example | ||||
| } | ||||
|  | ||||
| // See also | ||||
| // -------- | ||||
| // | ||||
| // The website arduinojson.org contains the documentation for all the functions | ||||
| // used above. It also includes an FAQ that will help you solve any | ||||
| // deserialization problem. | ||||
| // Please check it out at: https://arduinojson.org/ | ||||
| // | ||||
| // The book "Mastering ArduinoJson" contains a tutorial on deserialization. | ||||
| // It begins with a simple example, like the one above, and then adds more | ||||
| // features like deserializing directly from a file or an HTTP request. | ||||
| // Please check it out at: https://arduinojson.org/book/ | ||||
| // Visit https://arduinojson.org/v6/example/parser/ for more. | ||||
|   | ||||
| @@ -56,10 +56,10 @@ void loop() { | ||||
|   StaticJsonDocument<500> doc; | ||||
|  | ||||
|   // Make our document represent an object | ||||
|   JsonObject& root = doc.to<JsonObject>(); | ||||
|   JsonObject root = doc.to<JsonObject>(); | ||||
|  | ||||
|   // Create the "analog" array | ||||
|   JsonArray& analogValues = root.createNestedArray("analog"); | ||||
|   JsonArray analogValues = root.createNestedArray("analog"); | ||||
|   for (int pin = 0; pin < 6; pin++) { | ||||
|     // Read the analog input | ||||
|     int value = analogRead(pin); | ||||
| @@ -69,7 +69,7 @@ void loop() { | ||||
|   } | ||||
|  | ||||
|   // Create the "digital" array | ||||
|   JsonArray& digitalValues = root.createNestedArray("digital"); | ||||
|   JsonArray digitalValues = root.createNestedArray("digital"); | ||||
|   for (int pin = 0; pin < 14; pin++) { | ||||
|     // Read the digital input | ||||
|     int value = digitalRead(pin); | ||||
| @@ -95,15 +95,4 @@ void loop() { | ||||
|   client.stop(); | ||||
| } | ||||
|  | ||||
| // See also | ||||
| // -------- | ||||
| // | ||||
| // The website arduinojson.org contains the documentation for all the functions | ||||
| // used above. It also includes an FAQ that will help you solve any | ||||
| // serialization problem. | ||||
| // Please check it out at: https://arduinojson.org/ | ||||
| // | ||||
| // The book "Mastering ArduinoJson" contains a tutorial on serialization. | ||||
| // It begins with a simple example, then adds more features like serializing | ||||
| // directly to a file or an HTTP client. | ||||
| // Please check it out at: https://arduinojson.org/book/ | ||||
| // Visit https://arduinojson.org/v6/example/http-server/ for more. | ||||
|   | ||||
| @@ -48,10 +48,10 @@ void loop() { | ||||
|   StaticJsonDocument<500> doc; | ||||
|  | ||||
|   // Make our document represent an object | ||||
|   JsonObject& root = doc.to<JsonObject>(); | ||||
|   JsonObject root = doc.to<JsonObject>(); | ||||
|  | ||||
|   // Create the "analog" array | ||||
|   JsonArray& analogValues = root.createNestedArray("analog"); | ||||
|   JsonArray analogValues = root.createNestedArray("analog"); | ||||
|   for (int pin = 0; pin < 6; pin++) { | ||||
|     // Read the analog input | ||||
|     int value = analogRead(pin); | ||||
| @@ -61,7 +61,7 @@ void loop() { | ||||
|   } | ||||
|  | ||||
|   // Create the "digital" array | ||||
|   JsonArray& digitalValues = root.createNestedArray("digital"); | ||||
|   JsonArray digitalValues = root.createNestedArray("digital"); | ||||
|   for (int pin = 0; pin < 14; pin++) { | ||||
|     // Read the digital input | ||||
|     int value = digitalRead(pin); | ||||
| @@ -87,15 +87,4 @@ void loop() { | ||||
|   delay(10000); | ||||
| } | ||||
|  | ||||
| // See also | ||||
| // -------- | ||||
| // | ||||
| // The website arduinojson.org contains the documentation for all the functions | ||||
| // used above. It also includes an FAQ that will help you solve any | ||||
| // serialization problem. | ||||
| // Please check it out at: https://arduinojson.org/ | ||||
| // | ||||
| // The book "Mastering ArduinoJson" contains a tutorial on serialization. | ||||
| // It begins with a simple example, then adds more features like serializing | ||||
| // directly to a file or any stream. | ||||
| // Please check it out at: https://arduinojson.org/book/ | ||||
| // Visit https://arduinojson.org/v6/example/udp-beacon/ for more. | ||||
|   | ||||
| @@ -55,7 +55,7 @@ void setup() { | ||||
|   } | ||||
|  | ||||
|   // Get the root object in the document | ||||
|   JsonObject& root = doc.as<JsonObject>(); | ||||
|   JsonObject root = doc.as<JsonObject>(); | ||||
|  | ||||
|   // Fetch values. | ||||
|   // | ||||
| @@ -77,15 +77,4 @@ void loop() { | ||||
|   // not used in this example | ||||
| } | ||||
|  | ||||
| // See also | ||||
| // -------- | ||||
| // | ||||
| // The website arduinojson.org contains the documentation for all the functions | ||||
| // used above. It also includes an FAQ that will help you solve any | ||||
| // deserialization problem. | ||||
| // Please check it out at: https://arduinojson.org/ | ||||
| // | ||||
| // The book "Mastering ArduinoJson" contains a tutorial on deserialization. | ||||
| // It begins with a simple example, like the one above, and then adds more | ||||
| // features like deserializing directly from a file or an HTTP request. | ||||
| // Please check it out at: https://arduinojson.org/book/ | ||||
| // Visit https://arduinojson.org/v6/example/msgpack-parser/ for more. | ||||
|   | ||||
| @@ -21,7 +21,7 @@ void setup() { | ||||
|   // JsonBuffer. | ||||
|   deserializeJson(doc, F("{\"sensor\":\"gps\",\"time\":1351824120," | ||||
|                          "\"data\":[48.756080,2.302038]}")); | ||||
|   JsonObject& obj = doc.as<JsonObject>(); | ||||
|   JsonObject obj = doc.as<JsonObject>(); | ||||
|  | ||||
|   // You can use a Flash String to get an element of a JsonObject | ||||
|   // No duplication is done. | ||||
| @@ -56,15 +56,4 @@ void loop() { | ||||
|   // not used in this example | ||||
| } | ||||
|  | ||||
| // See also | ||||
| // -------- | ||||
| // | ||||
| // The website arduinojson.org contains the documentation for all the functions | ||||
| // used above. It also includes an FAQ that will help you solve any memory | ||||
| // problem. | ||||
| // Please check it out at: https://arduinojson.org/ | ||||
| // | ||||
| // The book "Mastering ArduinoJson" contains a quick C++ course that explains | ||||
| // how your microcontroller stores strings in memory. It also tells why you | ||||
| // should not abuse Flash strings with ArduinoJson. | ||||
| // Please check it out at: https://arduinojson.org/book/ | ||||
| // Visit https://arduinojson.org/v6/example/progmem/ for more. | ||||
|   | ||||
| @@ -18,7 +18,7 @@ void setup() { | ||||
|   String input = | ||||
|       "{\"sensor\":\"gps\",\"time\":1351824120,\"data\":[48.756080,2.302038]}"; | ||||
|   deserializeJson(doc, input); | ||||
|   JsonObject& obj = doc.as<JsonObject>(); | ||||
|   JsonObject obj = doc.as<JsonObject>(); | ||||
|  | ||||
|   // You can use a String to get an element of a JsonObject | ||||
|   // No duplication is done. | ||||
| @@ -62,14 +62,4 @@ void loop() { | ||||
|   // not used in this example | ||||
| } | ||||
|  | ||||
| // See also | ||||
| // -------- | ||||
| // | ||||
| // The website arduinojson.org contains the documentation for all the functions | ||||
| // used above. It also includes an FAQ that will help you solve any problem. | ||||
| // Please check it out at: https://arduinojson.org/ | ||||
| // | ||||
| // The book "Mastering ArduinoJson" contains a quick C++ course that explains | ||||
| // how your microcontroller stores strings in memory. On several occasions, it | ||||
| // shows how you can avoid String in your program. | ||||
| // Please check it out at: https://arduinojson.org/book/ | ||||
| // Visit https://arduinojson.org/v6/example/string/ for more. | ||||
|   | ||||
		Reference in New Issue
	
	Block a user