mirror of
https://github.com/me-no-dev/ESPAsyncWebServer.git
synced 2025-09-26 14:20:54 +02:00
ArduinoJson 7 support
This commit is contained in:
@@ -38,9 +38,7 @@
|
|||||||
#include <ESPAsyncWebServer.h>
|
#include <ESPAsyncWebServer.h>
|
||||||
#include <Print.h>
|
#include <Print.h>
|
||||||
|
|
||||||
#if ARDUINOJSON_VERSION_MAJOR == 5
|
#if ARDUINOJSON_VERSION_MAJOR == 6
|
||||||
#define ARDUINOJSON_5_COMPATIBILITY
|
|
||||||
#else
|
|
||||||
#ifndef DYNAMIC_JSON_DOCUMENT_SIZE
|
#ifndef DYNAMIC_JSON_DOCUMENT_SIZE
|
||||||
#define DYNAMIC_JSON_DOCUMENT_SIZE 1024
|
#define DYNAMIC_JSON_DOCUMENT_SIZE 1024
|
||||||
#endif
|
#endif
|
||||||
@@ -82,10 +80,12 @@ class ChunkPrint : public Print {
|
|||||||
class AsyncJsonResponse: public AsyncAbstractResponse {
|
class AsyncJsonResponse: public AsyncAbstractResponse {
|
||||||
protected:
|
protected:
|
||||||
|
|
||||||
#ifdef ARDUINOJSON_5_COMPATIBILITY
|
#if ARDUINOJSON_VERSION_MAJOR == 5
|
||||||
DynamicJsonBuffer _jsonBuffer;
|
DynamicJsonBuffer _jsonBuffer;
|
||||||
#else
|
#elif ARDUINOJSON_VERSION_MAJOR == 6
|
||||||
DynamicJsonDocument _jsonBuffer;
|
DynamicJsonDocument _jsonBuffer;
|
||||||
|
#else
|
||||||
|
JsonDocument _jsonBuffer;
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
JsonVariant _root;
|
JsonVariant _root;
|
||||||
@@ -93,7 +93,7 @@ class AsyncJsonResponse: public AsyncAbstractResponse {
|
|||||||
|
|
||||||
public:
|
public:
|
||||||
|
|
||||||
#ifdef ARDUINOJSON_5_COMPATIBILITY
|
#if ARDUINOJSON_VERSION_MAJOR == 5
|
||||||
AsyncJsonResponse(bool isArray=false): _isValid{false} {
|
AsyncJsonResponse(bool isArray=false): _isValid{false} {
|
||||||
_code = 200;
|
_code = 200;
|
||||||
_contentType = JSON_MIMETYPE;
|
_contentType = JSON_MIMETYPE;
|
||||||
@@ -102,7 +102,7 @@ class AsyncJsonResponse: public AsyncAbstractResponse {
|
|||||||
else
|
else
|
||||||
_root = _jsonBuffer.createObject();
|
_root = _jsonBuffer.createObject();
|
||||||
}
|
}
|
||||||
#else
|
#elif ARDUINOJSON_VERSION_MAJOR == 6
|
||||||
AsyncJsonResponse(bool isArray=false, size_t maxJsonBufferSize = DYNAMIC_JSON_DOCUMENT_SIZE) : _jsonBuffer(maxJsonBufferSize), _isValid{false} {
|
AsyncJsonResponse(bool isArray=false, size_t maxJsonBufferSize = DYNAMIC_JSON_DOCUMENT_SIZE) : _jsonBuffer(maxJsonBufferSize), _isValid{false} {
|
||||||
_code = 200;
|
_code = 200;
|
||||||
_contentType = JSON_MIMETYPE;
|
_contentType = JSON_MIMETYPE;
|
||||||
@@ -111,6 +111,15 @@ class AsyncJsonResponse: public AsyncAbstractResponse {
|
|||||||
else
|
else
|
||||||
_root = _jsonBuffer.createNestedObject();
|
_root = _jsonBuffer.createNestedObject();
|
||||||
}
|
}
|
||||||
|
#else
|
||||||
|
AsyncJsonResponse(bool isArray=false) : _isValid{false} {
|
||||||
|
_code = 200;
|
||||||
|
_contentType = JSON_MIMETYPE;
|
||||||
|
if(isArray)
|
||||||
|
_root = _jsonBuffer.add<JsonArray>();
|
||||||
|
else
|
||||||
|
_root = _jsonBuffer.add<JsonObject>();
|
||||||
|
}
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
~AsyncJsonResponse() {}
|
~AsyncJsonResponse() {}
|
||||||
@@ -118,7 +127,7 @@ class AsyncJsonResponse: public AsyncAbstractResponse {
|
|||||||
bool _sourceValid() const { return _isValid; }
|
bool _sourceValid() const { return _isValid; }
|
||||||
size_t setLength() {
|
size_t setLength() {
|
||||||
|
|
||||||
#ifdef ARDUINOJSON_5_COMPATIBILITY
|
#if ARDUINOJSON_VERSION_MAJOR == 5
|
||||||
_contentLength = _root.measureLength();
|
_contentLength = _root.measureLength();
|
||||||
#else
|
#else
|
||||||
_contentLength = measureJson(_root);
|
_contentLength = measureJson(_root);
|
||||||
@@ -133,7 +142,7 @@ class AsyncJsonResponse: public AsyncAbstractResponse {
|
|||||||
size_t _fillBuffer(uint8_t *data, size_t len){
|
size_t _fillBuffer(uint8_t *data, size_t len){
|
||||||
ChunkPrint dest(data, _sentLength, len);
|
ChunkPrint dest(data, _sentLength, len);
|
||||||
|
|
||||||
#ifdef ARDUINOJSON_5_COMPATIBILITY
|
#if ARDUINOJSON_VERSION_MAJOR == 5
|
||||||
_root.printTo( dest ) ;
|
_root.printTo( dest ) ;
|
||||||
#else
|
#else
|
||||||
serializeJson(_root, dest);
|
serializeJson(_root, dest);
|
||||||
@@ -144,13 +153,13 @@ class AsyncJsonResponse: public AsyncAbstractResponse {
|
|||||||
|
|
||||||
class PrettyAsyncJsonResponse: public AsyncJsonResponse {
|
class PrettyAsyncJsonResponse: public AsyncJsonResponse {
|
||||||
public:
|
public:
|
||||||
#ifdef ARDUINOJSON_5_COMPATIBILITY
|
#if ARDUINOJSON_VERSION_MAJOR == 6
|
||||||
PrettyAsyncJsonResponse (bool isArray=false) : AsyncJsonResponse{isArray} {}
|
|
||||||
#else
|
|
||||||
PrettyAsyncJsonResponse (bool isArray=false, size_t maxJsonBufferSize = DYNAMIC_JSON_DOCUMENT_SIZE) : AsyncJsonResponse{isArray, maxJsonBufferSize} {}
|
PrettyAsyncJsonResponse (bool isArray=false, size_t maxJsonBufferSize = DYNAMIC_JSON_DOCUMENT_SIZE) : AsyncJsonResponse{isArray, maxJsonBufferSize} {}
|
||||||
|
#else
|
||||||
|
PrettyAsyncJsonResponse (bool isArray=false) : AsyncJsonResponse{isArray} {}
|
||||||
#endif
|
#endif
|
||||||
size_t setLength () {
|
size_t setLength () {
|
||||||
#ifdef ARDUINOJSON_5_COMPATIBILITY
|
#if ARDUINOJSON_VERSION_MAJOR == 5
|
||||||
_contentLength = _root.measurePrettyLength ();
|
_contentLength = _root.measurePrettyLength ();
|
||||||
#else
|
#else
|
||||||
_contentLength = measureJsonPretty(_root);
|
_contentLength = measureJsonPretty(_root);
|
||||||
@@ -160,7 +169,7 @@ public:
|
|||||||
}
|
}
|
||||||
size_t _fillBuffer (uint8_t *data, size_t len) {
|
size_t _fillBuffer (uint8_t *data, size_t len) {
|
||||||
ChunkPrint dest (data, _sentLength, len);
|
ChunkPrint dest (data, _sentLength, len);
|
||||||
#ifdef ARDUINOJSON_5_COMPATIBILITY
|
#if ARDUINOJSON_VERSION_MAJOR == 5
|
||||||
_root.prettyPrintTo (dest);
|
_root.prettyPrintTo (dest);
|
||||||
#else
|
#else
|
||||||
serializeJsonPretty(_root, dest);
|
serializeJsonPretty(_root, dest);
|
||||||
@@ -178,17 +187,17 @@ protected:
|
|||||||
WebRequestMethodComposite _method;
|
WebRequestMethodComposite _method;
|
||||||
ArJsonRequestHandlerFunction _onRequest;
|
ArJsonRequestHandlerFunction _onRequest;
|
||||||
size_t _contentLength;
|
size_t _contentLength;
|
||||||
#ifndef ARDUINOJSON_5_COMPATIBILITY
|
#if ARDUINOJSON_VERSION_MAJOR == 6
|
||||||
const size_t maxJsonBufferSize;
|
const size_t maxJsonBufferSize;
|
||||||
#endif
|
#endif
|
||||||
size_t _maxContentLength;
|
size_t _maxContentLength;
|
||||||
public:
|
public:
|
||||||
#ifdef ARDUINOJSON_5_COMPATIBILITY
|
#if ARDUINOJSON_VERSION_MAJOR == 6
|
||||||
AsyncCallbackJsonWebHandler(const String& uri, ArJsonRequestHandlerFunction onRequest)
|
|
||||||
: _uri(uri), _method(HTTP_POST|HTTP_PUT|HTTP_PATCH), _onRequest(onRequest), _maxContentLength(16384) {}
|
|
||||||
#else
|
|
||||||
AsyncCallbackJsonWebHandler(const String& uri, ArJsonRequestHandlerFunction onRequest, size_t maxJsonBufferSize=DYNAMIC_JSON_DOCUMENT_SIZE)
|
AsyncCallbackJsonWebHandler(const String& uri, ArJsonRequestHandlerFunction onRequest, size_t maxJsonBufferSize=DYNAMIC_JSON_DOCUMENT_SIZE)
|
||||||
: _uri(uri), _method(HTTP_POST|HTTP_PUT|HTTP_PATCH), _onRequest(onRequest), maxJsonBufferSize(maxJsonBufferSize), _maxContentLength(16384) {}
|
: _uri(uri), _method(HTTP_POST|HTTP_PUT|HTTP_PATCH), _onRequest(onRequest), maxJsonBufferSize(maxJsonBufferSize), _maxContentLength(16384) {}
|
||||||
|
#else
|
||||||
|
AsyncCallbackJsonWebHandler(const String& uri, ArJsonRequestHandlerFunction onRequest)
|
||||||
|
: _uri(uri), _method(HTTP_POST|HTTP_PUT|HTTP_PATCH), _onRequest(onRequest), _maxContentLength(16384) {}
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
void setMethod(WebRequestMethodComposite method){ _method = method; }
|
void setMethod(WebRequestMethodComposite method){ _method = method; }
|
||||||
@@ -216,15 +225,20 @@ public:
|
|||||||
if(_onRequest) {
|
if(_onRequest) {
|
||||||
if (request->_tempObject != NULL) {
|
if (request->_tempObject != NULL) {
|
||||||
|
|
||||||
#ifdef ARDUINOJSON_5_COMPATIBILITY
|
#if ARDUINOJSON_VERSION_MAJOR == 5
|
||||||
DynamicJsonBuffer jsonBuffer;
|
DynamicJsonBuffer jsonBuffer;
|
||||||
JsonVariant json = jsonBuffer.parse((uint8_t*)(request->_tempObject));
|
JsonVariant json = jsonBuffer.parse((uint8_t*)(request->_tempObject));
|
||||||
if (json.success()) {
|
if (json.success()) {
|
||||||
#else
|
#elif ARDUINOJSON_VERSION_MAJOR == 6
|
||||||
DynamicJsonDocument jsonBuffer(this->maxJsonBufferSize);
|
DynamicJsonDocument jsonBuffer(this->maxJsonBufferSize);
|
||||||
DeserializationError error = deserializeJson(jsonBuffer, (uint8_t*)(request->_tempObject));
|
DeserializationError error = deserializeJson(jsonBuffer, (uint8_t*)(request->_tempObject));
|
||||||
if(!error) {
|
if(!error) {
|
||||||
JsonVariant json = jsonBuffer.as<JsonVariant>();
|
JsonVariant json = jsonBuffer.as<JsonVariant>();
|
||||||
|
#else
|
||||||
|
JsonDocument jsonBuffer;
|
||||||
|
DeserializationError error = deserializeJson(jsonBuffer, (uint8_t*)(request->_tempObject));
|
||||||
|
if(!error) {
|
||||||
|
JsonVariant json = jsonBuffer.as<JsonVariant>();
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
_onRequest(request, json);
|
_onRequest(request, json);
|
||||||
|
Reference in New Issue
Block a user