From e75dd5b02d29eaaeaa2127d41c05726cde737484 Mon Sep 17 00:00:00 2001 From: Mathieu Carbou Date: Mon, 30 Sep 2024 00:51:36 +0200 Subject: [PATCH] Fix compilation issue with ArduinoJson 5 and 6 (closes #108) --- platformio.ini | 2 ++ src/AsyncJson.cpp | 8 +++--- src/AsyncJson.h | 23 ++++++++--------- src/AsyncMessagePack.cpp | 31 +++++++++++++++++++--- src/AsyncMessagePack.h | 55 ++++++++++++++++++++++++++++++---------- 5 files changed, 84 insertions(+), 35 deletions(-) diff --git a/platformio.ini b/platformio.ini index d242032..c650376 100644 --- a/platformio.ini +++ b/platformio.ini @@ -24,6 +24,8 @@ upload_protocol = esptool monitor_speed = 115200 monitor_filters = esp32_exception_decoder, log2file lib_deps = + ; bblanchon/ArduinoJson @ 5.13.4 + ; bblanchon/ArduinoJson @ 6.21.5 bblanchon/ArduinoJson @ 7.2.0 mathieucarbou/AsyncTCP @ 3.2.5 board = esp32dev diff --git a/src/AsyncJson.cpp b/src/AsyncJson.cpp index ec464d1..7117a70 100644 --- a/src/AsyncJson.cpp +++ b/src/AsyncJson.cpp @@ -5,7 +5,7 @@ #if ARDUINOJSON_VERSION_MAJOR == 5 AsyncJsonResponse::AsyncJsonResponse(bool isArray) : _isValid{false} { _code = 200; - _contentType = JSON_MIMETYPE; + _contentType = asyncsrv::T_application_json; if (isArray) _root = _jsonBuffer.createArray(); else @@ -14,7 +14,7 @@ AsyncJsonResponse::AsyncJsonResponse(bool isArray) : _isValid{false} { #elif ARDUINOJSON_VERSION_MAJOR == 6 AsyncJsonResponse::AsyncJsonResponse(bool isArray, size_t maxJsonBufferSize) : _jsonBuffer(maxJsonBufferSize), _isValid{false} { _code = 200; - _contentType = JSON_MIMETYPE; + _contentType = asyncsrv::T_application_json; if (isArray) _root = _jsonBuffer.createNestedArray(); else @@ -23,7 +23,7 @@ AsyncJsonResponse::AsyncJsonResponse(bool isArray, size_t maxJsonBufferSize) : _ #else AsyncJsonResponse::AsyncJsonResponse(bool isArray) : _isValid{false} { _code = 200; - _contentType = JSON_MIMETYPE; + _contentType = asyncsrv::T_application_json; if (isArray) _root = _jsonBuffer.add(); else @@ -100,7 +100,7 @@ bool AsyncCallbackJsonWebHandler::canHandle(AsyncWebServerRequest* request) { if (_uri.length() && (_uri != request->url() && !request->url().startsWith(_uri + "/"))) return false; - if (request_method != HTTP_GET && !request->contentType().equalsIgnoreCase(JSON_MIMETYPE)) + if (request_method != HTTP_GET && !request->contentType().equalsIgnoreCase(asyncsrv::T_application_json)) return false; return true; diff --git a/src/AsyncJson.h b/src/AsyncJson.h index e0d88f0..6fc5eb7 100644 --- a/src/AsyncJson.h +++ b/src/AsyncJson.h @@ -36,10 +36,15 @@ #define ASYNC_JSON_H_ #if __has_include("ArduinoJson.h") - - #define ASYNC_JSON_SUPPORT 1 - #include + #if ARDUINOJSON_VERSION_MAJOR >= 5 + #define ASYNC_JSON_SUPPORT 1 + #else + #define ASYNC_JSON_SUPPORT 0 + #endif // ARDUINOJSON_VERSION_MAJOR >= 5 +#endif // __has_include("ArduinoJson.h") + +#if ASYNC_JSON_SUPPORT == 1 #include #include "ChunkPrint.h" @@ -50,12 +55,6 @@ #endif #endif -constexpr const char* JSON_MIMETYPE = "application/json"; - -/* - * Json Response - * */ - class AsyncJsonResponse : public AsyncAbstractResponse { protected: #if ARDUINOJSON_VERSION_MAJOR == 5 @@ -127,8 +126,6 @@ class AsyncCallbackJsonWebHandler : public AsyncWebHandler { virtual bool isRequestHandlerTrivial() override final { return !_onRequest; } }; -#else // __has_include("ArduinoJson.h") - #define ASYNC_JSON_SUPPORT 0 -#endif // __has_include("ArduinoJson.h") +#endif // ASYNC_JSON_SUPPORT == 1 -#endif +#endif // ASYNC_JSON_H_ diff --git a/src/AsyncMessagePack.cpp b/src/AsyncMessagePack.cpp index 27e0786..076f558 100644 --- a/src/AsyncMessagePack.cpp +++ b/src/AsyncMessagePack.cpp @@ -2,6 +2,16 @@ #if ASYNC_MSG_PACK_SUPPORT == 1 + #if ARDUINOJSON_VERSION_MAJOR == 6 +AsyncMessagePackResponse::AsyncMessagePackResponse(bool isArray, size_t maxJsonBufferSize) : _jsonBuffer(maxJsonBufferSize), _isValid{false} { + _code = 200; + _contentType = asyncsrv::T_application_msgpack; + if (isArray) + _root = _jsonBuffer.createNestedArray(); + else + _root = _jsonBuffer.createNestedObject(); +} + #else AsyncMessagePackResponse::AsyncMessagePackResponse(bool isArray) : _isValid{false} { _code = 200; _contentType = asyncsrv::T_application_msgpack; @@ -10,6 +20,7 @@ AsyncMessagePackResponse::AsyncMessagePackResponse(bool isArray) : _isValid{fals else _root = _jsonBuffer.add(); } + #endif size_t AsyncMessagePackResponse::setLength() { _contentLength = measureMsgPack(_root); @@ -25,8 +36,13 @@ size_t AsyncMessagePackResponse::_fillBuffer(uint8_t* data, size_t len) { return len; } -AsyncCallbackMessagePackWebHandler::AsyncCallbackMessagePackWebHandler(const String& uri, ArJsonRequestHandlerFunction onRequest) + #if ARDUINOJSON_VERSION_MAJOR == 6 +AsyncCallbackMessagePackWebHandler::AsyncCallbackMessagePackWebHandler(const String& uri, ArMessagePackRequestHandlerFunction onRequest, size_t maxJsonBufferSize) + : _uri(uri), _method(HTTP_GET | HTTP_POST | HTTP_PUT | HTTP_PATCH), _onRequest(onRequest), maxJsonBufferSize(maxJsonBufferSize), _maxContentLength(16384) {} + #else +AsyncCallbackMessagePackWebHandler::AsyncCallbackMessagePackWebHandler(const String& uri, ArMessagePackRequestHandlerFunction onRequest) : _uri(uri), _method(HTTP_GET | HTTP_POST | HTTP_PUT | HTTP_PATCH), _onRequest(onRequest), _maxContentLength(16384) {} + #endif bool AsyncCallbackMessagePackWebHandler::canHandle(AsyncWebServerRequest* request) { if (!_onRequest) @@ -51,13 +67,20 @@ void AsyncCallbackMessagePackWebHandler::handleRequest(AsyncWebServerRequest* re JsonVariant json; _onRequest(request, json); return; - } else if (request->_tempObject != NULL) { - JsonDocument jsonBuffer; - DeserializationError error = deserializeMsgPack(jsonBuffer, (uint8_t*)(request->_tempObject)); + #if ARDUINOJSON_VERSION_MAJOR == 6 + DynamicJsonDocument jsonBuffer(this->maxJsonBufferSize); + DeserializationError error = deserializeMsgPack(jsonBuffer, (uint8_t*)(request->_tempObject)); if (!error) { JsonVariant json = jsonBuffer.as(); + #else + JsonDocument jsonBuffer; + DeserializationError error = deserializeMsgPack(jsonBuffer, (uint8_t*)(request->_tempObject)); + if (!error) { + JsonVariant json = jsonBuffer.as(); + #endif + _onRequest(request, json); return; } diff --git a/src/AsyncMessagePack.h b/src/AsyncMessagePack.h index cfb9902..3194b5a 100644 --- a/src/AsyncMessagePack.h +++ b/src/AsyncMessagePack.h @@ -22,54 +22,81 @@ */ #if __has_include("ArduinoJson.h") - - #define ASYNC_MSG_PACK_SUPPORT 1 - #include + #if ARDUINOJSON_VERSION_MAJOR >= 6 + #define ASYNC_MSG_PACK_SUPPORT 1 + #else + #define ASYNC_MSG_PACK_SUPPORT 0 + #endif // ARDUINOJSON_VERSION_MAJOR >= 6 +#endif // __has_include("ArduinoJson.h") + +#if ASYNC_MSG_PACK_SUPPORT == 1 #include #include "ChunkPrint.h" + #if ARDUINOJSON_VERSION_MAJOR == 6 + #ifndef DYNAMIC_JSON_DOCUMENT_SIZE + #define DYNAMIC_JSON_DOCUMENT_SIZE 1024 + #endif + #endif + class AsyncMessagePackResponse : public AsyncAbstractResponse { protected: + #if ARDUINOJSON_VERSION_MAJOR == 6 + DynamicJsonDocument _jsonBuffer; + #else JsonDocument _jsonBuffer; + #endif + JsonVariant _root; bool _isValid; public: + #if ARDUINOJSON_VERSION_MAJOR == 6 + AsyncMessagePackResponse(bool isArray = false, size_t maxJsonBufferSize = DYNAMIC_JSON_DOCUMENT_SIZE); + #else AsyncMessagePackResponse(bool isArray = false); - + #endif JsonVariant& getRoot() { return _root; } bool _sourceValid() const { return _isValid; } size_t setLength(); size_t getSize() const { return _jsonBuffer.size(); } size_t _fillBuffer(uint8_t* data, size_t len); + #if ARDUINOJSON_VERSION_MAJOR >= 6 + bool overflowed() const { return _jsonBuffer.overflowed(); } + #endif }; -class AsyncCallbackMessagePackWebHandler : public AsyncWebHandler { - public: - typedef std::function ArJsonRequestHandlerFunction; +typedef std::function ArMessagePackRequestHandlerFunction; +class AsyncCallbackMessagePackWebHandler : public AsyncWebHandler { protected: const String _uri; WebRequestMethodComposite _method; - ArJsonRequestHandlerFunction _onRequest; + ArMessagePackRequestHandlerFunction _onRequest; size_t _contentLength; + #if ARDUINOJSON_VERSION_MAJOR == 6 + const size_t maxJsonBufferSize; + #endif size_t _maxContentLength; public: - AsyncCallbackMessagePackWebHandler(const String& uri, ArJsonRequestHandlerFunction onRequest = nullptr); + #if ARDUINOJSON_VERSION_MAJOR == 6 + AsyncCallbackMessagePackWebHandler(const String& uri, ArMessagePackRequestHandlerFunction onRequest = nullptr, size_t maxJsonBufferSize = DYNAMIC_JSON_DOCUMENT_SIZE); + #else + AsyncCallbackMessagePackWebHandler(const String& uri, ArMessagePackRequestHandlerFunction onRequest = nullptr); + #endif void setMethod(WebRequestMethodComposite method) { _method = method; } void setMaxContentLength(int maxContentLength) { _maxContentLength = maxContentLength; } - void onRequest(ArJsonRequestHandlerFunction fn) { _onRequest = fn; } + void onRequest(ArMessagePackRequestHandlerFunction fn) { _onRequest = fn; } + virtual bool canHandle(AsyncWebServerRequest* request) override final; virtual void handleRequest(AsyncWebServerRequest* request) override final; virtual void handleUpload(__unused AsyncWebServerRequest* request, __unused const String& filename, __unused size_t index, __unused uint8_t* data, __unused size_t len, __unused bool final) override final {} virtual void handleBody(AsyncWebServerRequest* request, uint8_t* data, size_t len, size_t index, size_t total) override final; - virtual bool isRequestHandlerTrivial() override final { return _onRequest ? false : true; } + virtual bool isRequestHandlerTrivial() override final { return !_onRequest; } }; -#else // __has_include("ArduinoJson.h") - #define ASYNC_MSG_PACK_SUPPORT 0 -#endif // __has_include("ArduinoJson.h") +#endif // ASYNC_MSG_PACK_SUPPORT == 1