From 2c70d66d4a6e10809e3335b9f61804b9c01d4445 Mon Sep 17 00:00:00 2001 From: cschwinne Date: Wed, 18 Dec 2019 00:41:45 +0100 Subject: [PATCH] Fix TypeError (#453) --- wled00/src/dependencies/json/AsyncJson-v6.h | 19 ++++++------------- wled00/wled00.ino | 2 +- wled00/wled18_server.ino | 17 ++++++++++++++--- 3 files changed, 21 insertions(+), 17 deletions(-) diff --git a/wled00/src/dependencies/json/AsyncJson-v6.h b/wled00/src/dependencies/json/AsyncJson-v6.h index 5bf87e1b..9187a3e9 100644 --- a/wled00/src/dependencies/json/AsyncJson-v6.h +++ b/wled00/src/dependencies/json/AsyncJson-v6.h @@ -15,7 +15,7 @@ #include "ArduinoJson-v6.h" #include -#define DYNAMYC_JSON_DOCUMENT_SIZE 8192 +#define DYNAMIC_JSON_DOCUMENT_SIZE 8192 constexpr const char* JSON_MIMETYPE = "application/json"; @@ -60,7 +60,7 @@ class AsyncJsonResponse: public AsyncAbstractResponse { 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; _contentType = JSON_MIMETYPE; if(isArray) @@ -90,7 +90,7 @@ class AsyncJsonResponse: public AsyncAbstractResponse { } }; -typedef std::function ArJsonRequestHandlerFunction; +typedef std::function ArJsonRequestHandlerFunction; class AsyncCallbackJsonWebHandler: public AsyncWebHandler { private: @@ -103,7 +103,7 @@ protected: int _maxContentLength; 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) {} void setMethod(WebRequestMethodComposite method){ _method = method; } @@ -127,15 +127,8 @@ public: virtual void handleRequest(AsyncWebServerRequest *request) override final { if(_onRequest) { if (request->_tempObject != NULL) { - - DynamicJsonDocument jsonBuffer(this->maxJsonBufferSize); - DeserializationError error = deserializeJson(jsonBuffer, (uint8_t*)(request->_tempObject)); - if(!error) { - JsonObject json = jsonBuffer.as(); - - _onRequest(request, json); - return; - } + _onRequest(request); + return; } request->send(_contentLength > _maxContentLength ? 413 : 400); } else { diff --git a/wled00/wled00.ino b/wled00/wled00.ino index 1abf9a57..237dd80d 100644 --- a/wled00/wled00.ino +++ b/wled00/wled00.ino @@ -98,7 +98,7 @@ //version code in format yymmddb (b = daily build) -#define VERSION 1912131 +#define VERSION 1912181 char versionString[] = "0.9.0-b1"; diff --git a/wled00/wled18_server.ino b/wled00/wled18_server.ino index c48d3785..e101c997 100644 --- a/wled00/wled18_server.ino +++ b/wled00/wled18_server.ino @@ -102,9 +102,20 @@ void initServer() serveJson(request); }); - AsyncCallbackJsonWebHandler* handler = new AsyncCallbackJsonWebHandler("/json", [](AsyncWebServerRequest *request, JsonObject root) { - if (root.isNull()){request->send(500, "application/json", "{\"error\":\"Parsing failed\"}"); return;} - if (deserializeState(root)) { serveJson(request); return; } //if JSON contains "v" (verbose response) + AsyncCallbackJsonWebHandler* handler = new AsyncCallbackJsonWebHandler("/json", [](AsyncWebServerRequest *request) { + bool verboseResponse = false; + if (1) { //scope JsonDocument so it releases its buffer + DynamicJsonDocument jsonBuffer(8192); + DeserializationError error = deserializeJson(jsonBuffer, (uint8_t*)(request->_tempObject)); + JsonObject root = jsonBuffer.as(); + 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}"); }); server.addHandler(handler);