mirror of
				https://github.com/eledio-devices/thirdparty-ArduinoJson.git
				synced 2025-10-31 08:42:39 +01:00 
			
		
		
		
	Added support for Stream (issue #300)
				
					
				
			This commit is contained in:
		| @@ -9,8 +9,8 @@ | ||||
| // If you like this project, please add a star! | ||||
|  | ||||
| #include <ArduinoJson.h> | ||||
| #include <SPI.h> | ||||
| #include <Ethernet.h> | ||||
| #include <SPI.h> | ||||
|  | ||||
| EthernetClient client; | ||||
|  | ||||
| @@ -36,11 +36,8 @@ void setup() { | ||||
| void loop() { | ||||
|   if (connect(server)) { | ||||
|     if (sendRequest(server, resource) && skipResponseHeaders()) { | ||||
|       char response[MAX_CONTENT_SIZE]; | ||||
|       readReponseContent(response, sizeof(response)); | ||||
|  | ||||
|       UserData userData; | ||||
|       if (parseUserData(response, &userData)) { | ||||
|       if (readReponseContent(&userData)) { | ||||
|         printUserData(&userData); | ||||
|       } | ||||
|     } | ||||
| @@ -89,7 +86,7 @@ bool sendRequest(const char* host, const char* resource) { | ||||
|   client.print(resource); | ||||
|   client.println(" HTTP/1.0"); | ||||
|   client.print("Host: "); | ||||
|   client.println(server); | ||||
|   client.println(host); | ||||
|   client.println("Connection: close"); | ||||
|   client.println(); | ||||
|  | ||||
| @@ -111,13 +108,6 @@ bool skipResponseHeaders() { | ||||
|   return ok; | ||||
| } | ||||
|  | ||||
| // Read the body of the response from the HTTP server | ||||
| void readReponseContent(char* content, size_t maxSize) { | ||||
|   size_t length = client.readBytes(content, maxSize); | ||||
|   content[length] = 0; | ||||
|   Serial.println(content); | ||||
| } | ||||
|  | ||||
| // Parse the JSON from the input string and extract the interesting values | ||||
| // Here is the JSON we need to parse | ||||
| // { | ||||
| @@ -143,21 +133,20 @@ void readReponseContent(char* content, size_t maxSize) { | ||||
| //     "bs": "harness real-time e-markets" | ||||
| //   } | ||||
| // } | ||||
| bool parseUserData(char* content, struct UserData* userData) { | ||||
| bool readReponseContent(struct UserData* userData) { | ||||
|   // Compute optimal size of the JSON buffer according to what we need to parse. | ||||
|   // This is only required if you use StaticJsonBuffer. | ||||
|   const size_t BUFFER_SIZE = | ||||
|       JSON_OBJECT_SIZE(8)     // the root object has 8 elements | ||||
|       + JSON_OBJECT_SIZE(5)   // the "address" object has 5 elements | ||||
|       + JSON_OBJECT_SIZE(2)   // the "geo" object has 2 elements | ||||
|       + JSON_OBJECT_SIZE(3);  // the "company" object has 3 elements | ||||
|       JSON_OBJECT_SIZE(8)    // the root object has 8 elements | ||||
|       + JSON_OBJECT_SIZE(5)  // the "address" object has 5 elements | ||||
|       + JSON_OBJECT_SIZE(2)  // the "geo" object has 2 elements | ||||
|       + JSON_OBJECT_SIZE(3)  // the "company" object has 3 elements | ||||
|       + MAX_CONTENT_SIZE;    // additional space for strings | ||||
|  | ||||
|   // Allocate a temporary memory pool on the stack | ||||
|   StaticJsonBuffer<BUFFER_SIZE> jsonBuffer; | ||||
|   // If the memory pool is too big for the stack, use this instead: | ||||
|   // DynamicJsonBuffer jsonBuffer; | ||||
|   // Allocate a temporary memory pool | ||||
|   DynamicJsonBuffer jsonBuffer(BUFFER_SIZE); | ||||
|  | ||||
|   JsonObject& root = jsonBuffer.parseObject(content); | ||||
|   JsonObject& root = jsonBuffer.parseObject(client); | ||||
|  | ||||
|   if (!root.success()) { | ||||
|     Serial.println("JSON parsing failed!"); | ||||
|   | ||||
		Reference in New Issue
	
	Block a user