forked from me-no-dev/ESPAsyncWebServer
arduino json 6 async web server migration
This commit is contained in:
@@ -35,6 +35,9 @@
|
|||||||
#ifndef ASYNC_JSON_H_
|
#ifndef ASYNC_JSON_H_
|
||||||
#define ASYNC_JSON_H_
|
#define ASYNC_JSON_H_
|
||||||
#include <ArduinoJson.h>
|
#include <ArduinoJson.h>
|
||||||
|
#include <Print.h>
|
||||||
|
|
||||||
|
#define DYNAMYC_JSON_DOCUMENT_SIZE 1024
|
||||||
|
|
||||||
constexpr char* JSON_MIMETYPE = "application/json";
|
constexpr char* JSON_MIMETYPE = "application/json";
|
||||||
|
|
||||||
@@ -63,27 +66,33 @@ class ChunkPrint : public Print {
|
|||||||
}
|
}
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
size_t write(const uint8_t *buffer, size_t size)
|
||||||
|
{
|
||||||
|
return this->Print::write(buffer, size);
|
||||||
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
class AsyncJsonResponse: public AsyncAbstractResponse {
|
class AsyncJsonResponse: public AsyncAbstractResponse {
|
||||||
private:
|
private:
|
||||||
DynamicJsonBuffer _jsonBuffer;
|
// DynamicJsonBuffer _jsonBuffer;
|
||||||
|
DynamicJsonDocument _jsonBuffer;
|
||||||
JsonVariant _root;
|
JsonVariant _root;
|
||||||
bool _isValid;
|
bool _isValid;
|
||||||
public:
|
public:
|
||||||
AsyncJsonResponse(bool isArray=false): _isValid{false} {
|
AsyncJsonResponse(bool isArray=false): _jsonBuffer(DYNAMYC_JSON_DOCUMENT_SIZE), _isValid{false} {
|
||||||
_code = 200;
|
_code = 200;
|
||||||
_contentType = JSON_MIMETYPE;
|
_contentType = JSON_MIMETYPE;
|
||||||
if(isArray)
|
if(isArray)
|
||||||
_root = _jsonBuffer.createArray();
|
_root = _jsonBuffer.createNestedArray();
|
||||||
else
|
else
|
||||||
_root = _jsonBuffer.createObject();
|
_root = _jsonBuffer.createNestedObject();
|
||||||
}
|
}
|
||||||
~AsyncJsonResponse() {}
|
~AsyncJsonResponse() {}
|
||||||
JsonVariant & getRoot() { return _root; }
|
JsonVariant & getRoot() { return _root; }
|
||||||
bool _sourceValid() const { return _isValid; }
|
bool _sourceValid() const { return _isValid; }
|
||||||
size_t setLength() {
|
size_t setLength() {
|
||||||
_contentLength = _root.measureLength();
|
size_t _contentLength = measureJson(_root);
|
||||||
|
// _contentLength = _root.measureLength();
|
||||||
if (_contentLength) { _isValid = true; }
|
if (_contentLength) { _isValid = true; }
|
||||||
return _contentLength;
|
return _contentLength;
|
||||||
}
|
}
|
||||||
@@ -92,7 +101,8 @@ 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);
|
||||||
_root.printTo( dest ) ;
|
serializeJson(_root, dest);
|
||||||
|
// _root.printTo( dest ) ;
|
||||||
return len;
|
return len;
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
@@ -133,9 +143,14 @@ public:
|
|||||||
virtual void handleRequest(AsyncWebServerRequest *request) override final {
|
virtual void handleRequest(AsyncWebServerRequest *request) override final {
|
||||||
if(_onRequest) {
|
if(_onRequest) {
|
||||||
if (request->_tempObject != NULL) {
|
if (request->_tempObject != NULL) {
|
||||||
DynamicJsonBuffer jsonBuffer;
|
DynamicJsonDocument jsonBuffer(DYNAMYC_JSON_DOCUMENT_SIZE);
|
||||||
JsonVariant json = jsonBuffer.parse((uint8_t*)(request->_tempObject));
|
DeserializationError error = deserializeJson(jsonBuffer, (uint8_t*)(request->_tempObject));
|
||||||
if (json.success()) {
|
if(!error) {
|
||||||
|
// DynamicJsonBuffer jsonBuffer;
|
||||||
|
// JsonVariant json = jsonBuffer.parse((uint8_t*)(request->_tempObject));
|
||||||
|
// if (json.success()) {
|
||||||
|
|
||||||
|
JsonVariant json = jsonBuffer.as<JsonVariant>();
|
||||||
_onRequest(request, json);
|
_onRequest(request, json);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
Reference in New Issue
Block a user