1+ #include " AsyncJson.h"
2+
3+ #if ARDUINOJSON_VERSION_MAJOR == 5
4+ AsyncJsonResponse::AsyncJsonResponse (bool isArray) : _isValid{false } {
5+ _code = 200 ;
6+ _contentType = JSON_MIMETYPE;
7+ if (isArray)
8+ _root = _jsonBuffer.createArray ();
9+ else
10+ _root = _jsonBuffer.createObject ();
11+ }
12+ #elif ARDUINOJSON_VERSION_MAJOR == 6
13+ AsyncJsonResponse::AsyncJsonResponse (bool isArray, size_t maxJsonBufferSize) : _jsonBuffer(maxJsonBufferSize), _isValid{false } {
14+ _code = 200 ;
15+ _contentType = JSON_MIMETYPE;
16+ if (isArray)
17+ _root = _jsonBuffer.createNestedArray ();
18+ else
19+ _root = _jsonBuffer.createNestedObject ();
20+ }
21+ #else
22+ AsyncJsonResponse::AsyncJsonResponse (bool isArray) : _isValid{false } {
23+ _code = 200 ;
24+ _contentType = JSON_MIMETYPE;
25+ if (isArray)
26+ _root = _jsonBuffer.add <JsonArray>();
27+ else
28+ _root = _jsonBuffer.add <JsonObject>();
29+ }
30+ #endif
31+
32+ size_t AsyncJsonResponse::setLength () {
33+ #if ARDUINOJSON_VERSION_MAJOR == 5
34+ _contentLength = _root.measureLength ();
35+ #else
36+ _contentLength = measureJson (_root);
37+ #endif
38+ if (_contentLength) {
39+ _isValid = true ;
40+ }
41+ return _contentLength;
42+ }
43+
44+ size_t AsyncJsonResponse::_fillBuffer (uint8_t * data, size_t len) {
45+ ChunkPrint dest (data, _sentLength, len);
46+ #if ARDUINOJSON_VERSION_MAJOR == 5
47+ _root.printTo (dest);
48+ #else
49+ serializeJson (_root, dest);
50+ #endif
51+ return len;
52+ }
53+
54+ #if ARDUINOJSON_VERSION_MAJOR == 6
55+ PrettyAsyncJsonResponse::PrettyAsyncJsonResponse (bool isArray, size_t maxJsonBufferSize) : AsyncJsonResponse{isArray, maxJsonBufferSize} {}
56+ #else
57+ PrettyAsyncJsonResponse::PrettyAsyncJsonResponse (bool isArray) : AsyncJsonResponse{isArray} {}
58+ #endif
59+
60+ size_t PrettyAsyncJsonResponse::setLength () {
61+ #if ARDUINOJSON_VERSION_MAJOR == 5
62+ _contentLength = _root.measurePrettyLength ();
63+ #else
64+ _contentLength = measureJsonPretty (_root);
65+ #endif
66+ if (_contentLength) {
67+ _isValid = true ;
68+ }
69+ return _contentLength;
70+ }
71+
72+ size_t PrettyAsyncJsonResponse::_fillBuffer (uint8_t * data, size_t len) {
73+ ChunkPrint dest (data, _sentLength, len);
74+ #if ARDUINOJSON_VERSION_MAJOR == 5
75+ _root.prettyPrintTo (dest);
76+ #else
77+ serializeJsonPretty (_root, dest);
78+ #endif
79+ return len;
80+ }
81+
82+ #if ARDUINOJSON_VERSION_MAJOR == 6
83+ AsyncCallbackJsonWebHandler::AsyncCallbackJsonWebHandler (const String& uri, ArJsonRequestHandlerFunction onRequest, size_t maxJsonBufferSize)
84+ : _uri(uri), _method(HTTP_GET | HTTP_POST | HTTP_PUT | HTTP_PATCH), _onRequest(onRequest), maxJsonBufferSize(maxJsonBufferSize), _maxContentLength(16384 ) {}
85+ #else
86+ AsyncCallbackJsonWebHandler::AsyncCallbackJsonWebHandler (const String& uri, ArJsonRequestHandlerFunction onRequest)
87+ : _uri(uri), _method(HTTP_GET | HTTP_POST | HTTP_PUT | HTTP_PATCH), _onRequest(onRequest), _maxContentLength(16384 ) {}
88+ #endif
89+
90+ bool AsyncCallbackJsonWebHandler::canHandle (AsyncWebServerRequest* request) {
91+ if (!_onRequest)
92+ return false ;
93+
94+ WebRequestMethodComposite request_method = request->method ();
95+ if (!(_method & request_method))
96+ return false ;
97+
98+ if (_uri.length () && (_uri != request->url () && !request->url ().startsWith (_uri + " /" )))
99+ return false ;
100+
101+ if (request_method != HTTP_GET && !request->contentType ().equalsIgnoreCase (JSON_MIMETYPE))
102+ return false ;
103+
104+ return true ;
105+ }
106+
107+ void AsyncCallbackJsonWebHandler::handleRequest (AsyncWebServerRequest* request) {
108+ if (_onRequest) {
109+ if (request->method () == HTTP_GET) {
110+ JsonVariant json;
111+ _onRequest (request, json);
112+ return ;
113+ } else if (request->_tempObject != NULL ) {
114+
115+ #if ARDUINOJSON_VERSION_MAJOR == 5
116+ DynamicJsonBuffer jsonBuffer;
117+ JsonVariant json = jsonBuffer.parse ((uint8_t *)(request->_tempObject ));
118+ if (json.success ()) {
119+ #elif ARDUINOJSON_VERSION_MAJOR == 6
120+ DynamicJsonDocument jsonBuffer (this ->maxJsonBufferSize );
121+ DeserializationError error = deserializeJson (jsonBuffer, (uint8_t *)(request->_tempObject ));
122+ if (!error) {
123+ JsonVariant json = jsonBuffer.as <JsonVariant>();
124+ #else
125+ JsonDocument jsonBuffer;
126+ DeserializationError error = deserializeJson (jsonBuffer, (uint8_t *)(request->_tempObject ));
127+ if (!error) {
128+ JsonVariant json = jsonBuffer.as <JsonVariant>();
129+ #endif
130+
131+ _onRequest (request, json);
132+ return ;
133+ }
134+ }
135+ request->send (_contentLength > _maxContentLength ? 413 : 400 );
136+ } else {
137+ request->send (500 );
138+ }
139+ }
140+
141+ void AsyncCallbackJsonWebHandler::handleBody (AsyncWebServerRequest* request, uint8_t * data, size_t len, size_t index, size_t total) {
142+ if (_onRequest) {
143+ _contentLength = total;
144+ if (total > 0 && request->_tempObject == NULL && total < _maxContentLength) {
145+ request->_tempObject = malloc (total);
146+ }
147+ if (request->_tempObject != NULL ) {
148+ memcpy ((uint8_t *)(request->_tempObject ) + index, data, len);
149+ }
150+ }
151+ }
0 commit comments