66
77#if ASYNC_JSON_SUPPORT == 1
88
9+ // Json content type response classes
10+
911#if ARDUINOJSON_VERSION_MAJOR == 5
1012AsyncJsonResponse::AsyncJsonResponse (bool isArray) : _isValid{false } {
1113 _code = 200 ;
@@ -88,6 +90,27 @@ size_t PrettyAsyncJsonResponse::_fillBuffer(uint8_t *data, size_t len) {
8890 return len;
8991}
9092
93+ // MessagePack content type response
94+ #if ASYNC_MSG_PACK_SUPPORT == 1
95+
96+ size_t AsyncMessagePackResponse::setLength () {
97+ _contentLength = measureMsgPack (_root);
98+ if (_contentLength) {
99+ _isValid = true ;
100+ }
101+ return _contentLength;
102+ }
103+
104+ size_t AsyncMessagePackResponse::_fillBuffer (uint8_t *data, size_t len) {
105+ ChunkPrint dest (data, _sentLength, len);
106+ serializeMsgPack (_root, dest);
107+ return len;
108+ }
109+
110+ #endif
111+
112+ // Body handler supporting both content types: JSON and MessagePack
113+
91114#if ARDUINOJSON_VERSION_MAJOR == 6
92115AsyncCallbackJsonWebHandler::AsyncCallbackJsonWebHandler (const String &uri, ArJsonRequestHandlerFunction onRequest, size_t maxJsonBufferSize)
93116 : _uri(uri), _method(HTTP_GET | HTTP_POST | HTTP_PUT | HTTP_PATCH), _onRequest(onRequest), maxJsonBufferSize(maxJsonBufferSize), _maxContentLength(16384 ) {}
@@ -105,11 +128,12 @@ bool AsyncCallbackJsonWebHandler::canHandle(AsyncWebServerRequest *request) cons
105128 return false ;
106129 }
107130
108- if (request->method () != HTTP_GET && !request->contentType ().equalsIgnoreCase (asyncsrv::T_application_json)) {
109- return false ;
110- }
111-
112- return true ;
131+ #if ASYNC_MSG_PACK_SUPPORT == 1
132+ return request->method () == HTTP_GET || request->contentType ().equalsIgnoreCase (asyncsrv::T_application_json)
133+ || request->contentType ().equalsIgnoreCase (asyncsrv::T_application_msgpack);
134+ #else
135+ return request->method () == HTTP_GET || request->contentType ().equalsIgnoreCase (asyncsrv::T_application_json);
136+ #endif
113137}
114138
115139void AsyncCallbackJsonWebHandler::handleRequest (AsyncWebServerRequest *request) {
@@ -136,26 +160,32 @@ void AsyncCallbackJsonWebHandler::handleRequest(AsyncWebServerRequest *request)
136160 }
137161
138162#if ARDUINOJSON_VERSION_MAJOR == 5
139- DynamicJsonBuffer jsonBuffer;
140- JsonVariant json = jsonBuffer.parse ((const char *)request->_tempObject );
141- if (json.success ()) {
163+ DynamicJsonBuffer doc;
142164#elif ARDUINOJSON_VERSION_MAJOR == 6
143- DynamicJsonDocument jsonBuffer (this ->maxJsonBufferSize );
144- DeserializationError error = deserializeJson (jsonBuffer, (const char *)request->_tempObject );
145- if (!error) {
146- JsonVariant json = jsonBuffer.as <JsonVariant>();
165+ DynamicJsonDocument doc (this ->maxJsonBufferSize );
147166#else
148- JsonDocument jsonBuffer;
149- DeserializationError error = deserializeJson (jsonBuffer, (const char *)request->_tempObject );
150- if (!error) {
151- JsonVariant json = jsonBuffer.as <JsonVariant>();
167+ JsonDocument doc;
152168#endif
153169
170+ #if ARDUINOJSON_VERSION_MAJOR == 5
171+ JsonVariant json = doc.parse ((const char *)request->_tempObject );
172+ if (json.success ()) {
154173 _onRequest (request, json);
155- } else {
156- // error parsing the body
157- request->send (400 );
174+ return ;
175+ }
176+ #else
177+ DeserializationError error = request->contentType ().equalsIgnoreCase (asyncsrv::T_application_msgpack)
178+ ? deserializeMsgPack (doc, (uint8_t *)(request->_tempObject ))
179+ : deserializeJson (doc, (const char *)request->_tempObject );
180+ if (!error) {
181+ JsonVariant json = doc.as <JsonVariant>();
182+ _onRequest (request, json);
183+ return ;
158184 }
185+ #endif
186+
187+ // error parsing the body
188+ request->send (400 );
159189 }
160190}
161191
0 commit comments