Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update README.md for ArduinoJSON 6 #1135

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 22 additions & 7 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -333,7 +333,7 @@ Endpoints which consume JSON can use a special handler to get ready to use JSON
#include "ArduinoJson.h"

AsyncCallbackJsonWebHandler* handler = new AsyncCallbackJsonWebHandler("/rest/endpoint", [](AsyncWebServerRequest *request, JsonVariant &json) {
JsonObject& jsonObj = json.as<JsonObject>();
JsonObject& jsonObj = json.as<JsonObject>(); //For ArduinoJson 6 use "JsonObject jsonObj ="
// ...
});
server.addHandler(handler);
Expand Down Expand Up @@ -735,22 +735,38 @@ response->print("</body></html>");
request->send(response);
```

### ArduinoJson Basic Response
### ArduinoJson 5 Basic Response
This way of sending Json is great for when the result is below 4KB
```cpp
#include "AsyncJson.h"
#include "ArduinoJson.h"


AsyncResponseStream *response = request->beginResponseStream("application/json");
DynamicJsonBuffer jsonBuffer;
DynamicJsonBuffer jsonBuffer; //Note this buffer automatically expands
JsonObject &root = jsonBuffer.createObject();

root["heap"] = ESP.getFreeHeap();
root["ssid"] = WiFi.SSID();
root.printTo(*response);
request->send(response);
```

### ArduinoJson 6 Basic Response
This way of sending Json is great for when the result is below 4KB
```cpp
#include "AsyncJson.h"
#include "ArduinoJson.h"

DynamicJsonDocument jDoc(4096); //Note this buffer is a fixed size and cannot expand.
String response;

jDoc["heap"] = ESP.getFreeHeap();
jDoc["ssid"] = WiFi.SSID();
serializeJson(jdoc, response);
request->send(response);
request->send(200, "application/json", response);
```

### ArduinoJson Advanced Response
This response can handle really large Json objects (tested to 40KB)
There isn't any noticeable speed decrease for small results with the method above
Expand All @@ -761,10 +777,9 @@ to the resulting json packets
#include "AsyncJson.h"
#include "ArduinoJson.h"


AsyncJsonResponse * response = new AsyncJsonResponse();
response->addHeader("Server","ESP Async Web Server");
JsonObject& root = response->getRoot();
JsonObject& root = response->getRoot(); //For ArduinoJson 6 use "JsonObject root ="
root["heap"] = ESP.getFreeHeap();
root["ssid"] = WiFi.SSID();
response->setLength();
Expand Down Expand Up @@ -1109,7 +1124,7 @@ When sending a web socket message using the above methods a buffer is created.
```cpp
void sendDataWs(AsyncWebSocketClient * client)
{
DynamicJsonBuffer jsonBuffer;
DynamicJsonBuffer jsonBuffer; //For ArduinoJson 6 use DynamicJsonBuffer and skip createObject() below.
JsonObject& root = jsonBuffer.createObject();
root["a"] = "abc";
root["b"] = "abcd";
Expand Down