6
6
7
7
#if ASYNC_JSON_SUPPORT == 1
8
8
9
+ // Json content type response classes
10
+
9
11
#if ARDUINOJSON_VERSION_MAJOR == 5
10
12
AsyncJsonResponse::AsyncJsonResponse (bool isArray) : _isValid{false } {
11
13
_code = 200 ;
@@ -88,6 +90,27 @@ size_t PrettyAsyncJsonResponse::_fillBuffer(uint8_t *data, size_t len) {
88
90
return len;
89
91
}
90
92
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
+
91
114
#if ARDUINOJSON_VERSION_MAJOR == 6
92
115
AsyncCallbackJsonWebHandler::AsyncCallbackJsonWebHandler (const String &uri, ArJsonRequestHandlerFunction onRequest, size_t maxJsonBufferSize)
93
116
: _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
105
128
return false ;
106
129
}
107
130
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
113
137
}
114
138
115
139
void AsyncCallbackJsonWebHandler::handleRequest (AsyncWebServerRequest *request) {
@@ -136,26 +160,32 @@ void AsyncCallbackJsonWebHandler::handleRequest(AsyncWebServerRequest *request)
136
160
}
137
161
138
162
#if ARDUINOJSON_VERSION_MAJOR == 5
139
- DynamicJsonBuffer jsonBuffer;
140
- JsonVariant json = jsonBuffer.parse ((const char *)request->_tempObject );
141
- if (json.success ()) {
163
+ DynamicJsonBuffer doc;
142
164
#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 );
147
166
#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;
152
168
#endif
153
169
170
+ #if ARDUINOJSON_VERSION_MAJOR == 5
171
+ JsonVariant json = doc.parse ((const char *)request->_tempObject );
172
+ if (json.success ()) {
154
173
_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 ;
158
184
}
185
+ #endif
186
+
187
+ // error parsing the body
188
+ request->send (400 );
159
189
}
160
190
}
161
191
0 commit comments