Fix TypeError (#453)

This commit is contained in:
cschwinne 2019-12-18 00:41:45 +01:00
parent 6ef988549d
commit 2c70d66d4a
3 changed files with 21 additions and 17 deletions

View File

@ -15,7 +15,7 @@
#include "ArduinoJson-v6.h" #include "ArduinoJson-v6.h"
#include <Print.h> #include <Print.h>
#define DYNAMYC_JSON_DOCUMENT_SIZE 8192 #define DYNAMIC_JSON_DOCUMENT_SIZE 8192
constexpr const char* JSON_MIMETYPE = "application/json"; constexpr const char* JSON_MIMETYPE = "application/json";
@ -60,7 +60,7 @@ class AsyncJsonResponse: public AsyncAbstractResponse {
public: public:
AsyncJsonResponse(size_t maxJsonBufferSize = DYNAMYC_JSON_DOCUMENT_SIZE, bool isArray=false) : _jsonBuffer(maxJsonBufferSize), _isValid{false} { AsyncJsonResponse(size_t maxJsonBufferSize = DYNAMIC_JSON_DOCUMENT_SIZE, bool isArray=false) : _jsonBuffer(maxJsonBufferSize), _isValid{false} {
_code = 200; _code = 200;
_contentType = JSON_MIMETYPE; _contentType = JSON_MIMETYPE;
if(isArray) if(isArray)
@ -90,7 +90,7 @@ class AsyncJsonResponse: public AsyncAbstractResponse {
} }
}; };
typedef std::function<void(AsyncWebServerRequest *request, JsonObject json)> ArJsonRequestHandlerFunction; typedef std::function<void(AsyncWebServerRequest *request)> ArJsonRequestHandlerFunction;
class AsyncCallbackJsonWebHandler: public AsyncWebHandler { class AsyncCallbackJsonWebHandler: public AsyncWebHandler {
private: private:
@ -103,7 +103,7 @@ protected:
int _maxContentLength; int _maxContentLength;
public: public:
AsyncCallbackJsonWebHandler(const String& uri, ArJsonRequestHandlerFunction onRequest, size_t maxJsonBufferSize=DYNAMYC_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) {}
void setMethod(WebRequestMethodComposite method){ _method = method; } void setMethod(WebRequestMethodComposite method){ _method = method; }
@ -127,16 +127,9 @@ 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) {
_onRequest(request);
DynamicJsonDocument jsonBuffer(this->maxJsonBufferSize);
DeserializationError error = deserializeJson(jsonBuffer, (uint8_t*)(request->_tempObject));
if(!error) {
JsonObject json = jsonBuffer.as<JsonObject>();
_onRequest(request, json);
return; return;
} }
}
request->send(_contentLength > _maxContentLength ? 413 : 400); request->send(_contentLength > _maxContentLength ? 413 : 400);
} else { } else {
request->send(500); request->send(500);

View File

@ -98,7 +98,7 @@
//version code in format yymmddb (b = daily build) //version code in format yymmddb (b = daily build)
#define VERSION 1912131 #define VERSION 1912181
char versionString[] = "0.9.0-b1"; char versionString[] = "0.9.0-b1";

View File

@ -102,9 +102,20 @@ void initServer()
serveJson(request); serveJson(request);
}); });
AsyncCallbackJsonWebHandler* handler = new AsyncCallbackJsonWebHandler("/json", [](AsyncWebServerRequest *request, JsonObject root) { AsyncCallbackJsonWebHandler* handler = new AsyncCallbackJsonWebHandler("/json", [](AsyncWebServerRequest *request) {
if (root.isNull()){request->send(500, "application/json", "{\"error\":\"Parsing failed\"}"); return;} bool verboseResponse = false;
if (deserializeState(root)) { serveJson(request); return; } //if JSON contains "v" (verbose response) if (1) { //scope JsonDocument so it releases its buffer
DynamicJsonDocument jsonBuffer(8192);
DeserializationError error = deserializeJson(jsonBuffer, (uint8_t*)(request->_tempObject));
JsonObject root = jsonBuffer.as<JsonObject>();
if (error || root.isNull()) {
request->send(400, "application/json", "{\"error\":10}"); return;
}
verboseResponse = deserializeState(root);
}
if (verboseResponse) { //if JSON contains "v"
serveJson(request); return;
}
request->send(200, "application/json", "{\"success\":true}"); request->send(200, "application/json", "{\"success\":true}");
}); });
server.addHandler(handler); server.addHandler(handler);