Skip to content
This repository was archived by the owner on Jan 21, 2025. It is now read-only.

Commit b054150

Browse files
Merge pull request #109 from mathieucarbou/fix-108
Fix compilation issue with ArduinoJson 5 and 6 (closes #108)
2 parents 5fbab6c + 2706a62 commit b054150

File tree

10 files changed

+91
-42
lines changed

10 files changed

+91
-42
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ This fork is based on [yubox-node-org/ESPAsyncWebServer](https://github.com/yubo
3030
**WARNING** The library name was changed from `ESP Async WebServer` to `ESPAsyncWebServer` as per the Arduino Lint recommendations.
3131

3232
```
33-
mathieucarbou/ESPAsyncWebServer @ 3.3.2
33+
mathieucarbou/ESPAsyncWebServer @ 3.3.3
3434
```
3535

3636
Dependency:

docs/index.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ This fork is based on [yubox-node-org/ESPAsyncWebServer](https://github.com/yubo
3030
**WARNING** The library name was changed from `ESP Async WebServer` to `ESPAsyncWebServer` as per the Arduino Lint recommendations.
3131

3232
```
33-
mathieucarbou/ESPAsyncWebServer @ 3.3.2
33+
mathieucarbou/ESPAsyncWebServer @ 3.3.3
3434
```
3535

3636
Dependency:

library.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "ESPAsyncWebServer",
3-
"version": "3.3.2",
3+
"version": "3.3.3",
44
"description": "Asynchronous HTTP and WebSocket Server Library for ESP32, ESP8266 and RP2040. Supports: WebSocket, SSE, Authentication, Arduino Json 7, File Upload, Static File serving, URL Rewrite, URL Redirect, etc.",
55
"keywords": "http,async,websocket,webserver",
66
"homepage": "https://github.com/mathieucarbou/ESPAsyncWebServer",

library.properties

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
name=ESPAsyncWebServer
2-
version=3.3.2
2+
version=3.3.3
33
author=Me-No-Dev
44
maintainer=Mathieu Carbou <[email protected]>
55
sentence=Asynchronous HTTP and WebSocket Server Library for ESP32, ESP8266 and RP2040

platformio.ini

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,8 @@ upload_protocol = esptool
2424
monitor_speed = 115200
2525
monitor_filters = esp32_exception_decoder, log2file
2626
lib_deps =
27+
; bblanchon/ArduinoJson @ 5.13.4
28+
; bblanchon/ArduinoJson @ 6.21.5
2729
bblanchon/ArduinoJson @ 7.2.0
2830
mathieucarbou/AsyncTCP @ 3.2.5
2931
board = esp32dev
@@ -63,7 +65,7 @@ lib_deps =
6365
[env:raspberrypi]
6466
upload_protocol = picotool
6567
; platform = raspberrypi
66-
platform = https://github.com/maxgerhardt/platform-raspberrypi.git
68+
platform = https://github.com/maxgerhardt/platform-raspberrypi.git#f2687073f73d554c9db41f29b4769fd9703f4e55
6769
board = rpipicow
6870
lib_deps =
6971
bblanchon/ArduinoJson @ 7.2.0
@@ -100,7 +102,7 @@ lib_deps =
100102

101103
[env:ci-raspberrypi]
102104
; platform = raspberrypi
103-
platform = https://github.com/maxgerhardt/platform-raspberrypi.git
105+
platform = https://github.com/maxgerhardt/platform-raspberrypi.git#f2687073f73d554c9db41f29b4769fd9703f4e55
104106
board = ${sysenv.PIO_BOARD}
105107
lib_deps =
106108
bblanchon/ArduinoJson @ 7.2.0

src/AsyncJson.cpp

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
#if ARDUINOJSON_VERSION_MAJOR == 5
66
AsyncJsonResponse::AsyncJsonResponse(bool isArray) : _isValid{false} {
77
_code = 200;
8-
_contentType = JSON_MIMETYPE;
8+
_contentType = asyncsrv::T_application_json;
99
if (isArray)
1010
_root = _jsonBuffer.createArray();
1111
else
@@ -14,7 +14,7 @@ AsyncJsonResponse::AsyncJsonResponse(bool isArray) : _isValid{false} {
1414
#elif ARDUINOJSON_VERSION_MAJOR == 6
1515
AsyncJsonResponse::AsyncJsonResponse(bool isArray, size_t maxJsonBufferSize) : _jsonBuffer(maxJsonBufferSize), _isValid{false} {
1616
_code = 200;
17-
_contentType = JSON_MIMETYPE;
17+
_contentType = asyncsrv::T_application_json;
1818
if (isArray)
1919
_root = _jsonBuffer.createNestedArray();
2020
else
@@ -23,7 +23,7 @@ AsyncJsonResponse::AsyncJsonResponse(bool isArray, size_t maxJsonBufferSize) : _
2323
#else
2424
AsyncJsonResponse::AsyncJsonResponse(bool isArray) : _isValid{false} {
2525
_code = 200;
26-
_contentType = JSON_MIMETYPE;
26+
_contentType = asyncsrv::T_application_json;
2727
if (isArray)
2828
_root = _jsonBuffer.add<JsonArray>();
2929
else
@@ -100,7 +100,7 @@ bool AsyncCallbackJsonWebHandler::canHandle(AsyncWebServerRequest* request) {
100100
if (_uri.length() && (_uri != request->url() && !request->url().startsWith(_uri + "/")))
101101
return false;
102102

103-
if (request_method != HTTP_GET && !request->contentType().equalsIgnoreCase(JSON_MIMETYPE))
103+
if (request_method != HTTP_GET && !request->contentType().equalsIgnoreCase(asyncsrv::T_application_json))
104104
return false;
105105

106106
return true;

src/AsyncJson.h

Lines changed: 10 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -36,10 +36,15 @@
3636
#define ASYNC_JSON_H_
3737

3838
#if __has_include("ArduinoJson.h")
39-
40-
#define ASYNC_JSON_SUPPORT 1
41-
4239
#include <ArduinoJson.h>
40+
#if ARDUINOJSON_VERSION_MAJOR >= 5
41+
#define ASYNC_JSON_SUPPORT 1
42+
#else
43+
#define ASYNC_JSON_SUPPORT 0
44+
#endif // ARDUINOJSON_VERSION_MAJOR >= 5
45+
#endif // __has_include("ArduinoJson.h")
46+
47+
#if ASYNC_JSON_SUPPORT == 1
4348
#include <ESPAsyncWebServer.h>
4449

4550
#include "ChunkPrint.h"
@@ -50,12 +55,6 @@
5055
#endif
5156
#endif
5257

53-
constexpr const char* JSON_MIMETYPE = "application/json";
54-
55-
/*
56-
* Json Response
57-
* */
58-
5958
class AsyncJsonResponse : public AsyncAbstractResponse {
6059
protected:
6160
#if ARDUINOJSON_VERSION_MAJOR == 5
@@ -127,8 +126,6 @@ class AsyncCallbackJsonWebHandler : public AsyncWebHandler {
127126
virtual bool isRequestHandlerTrivial() override final { return !_onRequest; }
128127
};
129128

130-
#else // __has_include("ArduinoJson.h")
131-
#define ASYNC_JSON_SUPPORT 0
132-
#endif // __has_include("ArduinoJson.h")
129+
#endif // ASYNC_JSON_SUPPORT == 1
133130

134-
#endif
131+
#endif // ASYNC_JSON_H_

src/AsyncMessagePack.cpp

Lines changed: 26 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,16 @@
22

33
#if ASYNC_MSG_PACK_SUPPORT == 1
44

5+
#if ARDUINOJSON_VERSION_MAJOR == 6
6+
AsyncMessagePackResponse::AsyncMessagePackResponse(bool isArray, size_t maxJsonBufferSize) : _jsonBuffer(maxJsonBufferSize), _isValid{false} {
7+
_code = 200;
8+
_contentType = asyncsrv::T_application_msgpack;
9+
if (isArray)
10+
_root = _jsonBuffer.createNestedArray();
11+
else
12+
_root = _jsonBuffer.createNestedObject();
13+
}
14+
#else
515
AsyncMessagePackResponse::AsyncMessagePackResponse(bool isArray) : _isValid{false} {
616
_code = 200;
717
_contentType = asyncsrv::T_application_msgpack;
@@ -10,6 +20,7 @@ AsyncMessagePackResponse::AsyncMessagePackResponse(bool isArray) : _isValid{fals
1020
else
1121
_root = _jsonBuffer.add<JsonObject>();
1222
}
23+
#endif
1324

1425
size_t AsyncMessagePackResponse::setLength() {
1526
_contentLength = measureMsgPack(_root);
@@ -25,8 +36,13 @@ size_t AsyncMessagePackResponse::_fillBuffer(uint8_t* data, size_t len) {
2536
return len;
2637
}
2738

28-
AsyncCallbackMessagePackWebHandler::AsyncCallbackMessagePackWebHandler(const String& uri, ArJsonRequestHandlerFunction onRequest)
39+
#if ARDUINOJSON_VERSION_MAJOR == 6
40+
AsyncCallbackMessagePackWebHandler::AsyncCallbackMessagePackWebHandler(const String& uri, ArMessagePackRequestHandlerFunction onRequest, size_t maxJsonBufferSize)
41+
: _uri(uri), _method(HTTP_GET | HTTP_POST | HTTP_PUT | HTTP_PATCH), _onRequest(onRequest), maxJsonBufferSize(maxJsonBufferSize), _maxContentLength(16384) {}
42+
#else
43+
AsyncCallbackMessagePackWebHandler::AsyncCallbackMessagePackWebHandler(const String& uri, ArMessagePackRequestHandlerFunction onRequest)
2944
: _uri(uri), _method(HTTP_GET | HTTP_POST | HTTP_PUT | HTTP_PATCH), _onRequest(onRequest), _maxContentLength(16384) {}
45+
#endif
3046

3147
bool AsyncCallbackMessagePackWebHandler::canHandle(AsyncWebServerRequest* request) {
3248
if (!_onRequest)
@@ -51,13 +67,20 @@ void AsyncCallbackMessagePackWebHandler::handleRequest(AsyncWebServerRequest* re
5167
JsonVariant json;
5268
_onRequest(request, json);
5369
return;
54-
5570
} else if (request->_tempObject != NULL) {
71+
72+
#if ARDUINOJSON_VERSION_MAJOR == 6
73+
DynamicJsonDocument jsonBuffer(this->maxJsonBufferSize);
74+
DeserializationError error = deserializeMsgPack(jsonBuffer, (uint8_t*)(request->_tempObject));
75+
if (!error) {
76+
JsonVariant json = jsonBuffer.as<JsonVariant>();
77+
#else
5678
JsonDocument jsonBuffer;
5779
DeserializationError error = deserializeMsgPack(jsonBuffer, (uint8_t*)(request->_tempObject));
58-
5980
if (!error) {
6081
JsonVariant json = jsonBuffer.as<JsonVariant>();
82+
#endif
83+
6184
_onRequest(request, json);
6285
return;
6386
}

src/AsyncMessagePack.h

Lines changed: 41 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -22,54 +22,81 @@
2222
*/
2323

2424
#if __has_include("ArduinoJson.h")
25-
26-
#define ASYNC_MSG_PACK_SUPPORT 1
27-
2825
#include <ArduinoJson.h>
26+
#if ARDUINOJSON_VERSION_MAJOR >= 6
27+
#define ASYNC_MSG_PACK_SUPPORT 1
28+
#else
29+
#define ASYNC_MSG_PACK_SUPPORT 0
30+
#endif // ARDUINOJSON_VERSION_MAJOR >= 6
31+
#endif // __has_include("ArduinoJson.h")
32+
33+
#if ASYNC_MSG_PACK_SUPPORT == 1
2934
#include <ESPAsyncWebServer.h>
3035

3136
#include "ChunkPrint.h"
3237

38+
#if ARDUINOJSON_VERSION_MAJOR == 6
39+
#ifndef DYNAMIC_JSON_DOCUMENT_SIZE
40+
#define DYNAMIC_JSON_DOCUMENT_SIZE 1024
41+
#endif
42+
#endif
43+
3344
class AsyncMessagePackResponse : public AsyncAbstractResponse {
3445
protected:
46+
#if ARDUINOJSON_VERSION_MAJOR == 6
47+
DynamicJsonDocument _jsonBuffer;
48+
#else
3549
JsonDocument _jsonBuffer;
50+
#endif
51+
3652
JsonVariant _root;
3753
bool _isValid;
3854

3955
public:
56+
#if ARDUINOJSON_VERSION_MAJOR == 6
57+
AsyncMessagePackResponse(bool isArray = false, size_t maxJsonBufferSize = DYNAMIC_JSON_DOCUMENT_SIZE);
58+
#else
4059
AsyncMessagePackResponse(bool isArray = false);
41-
60+
#endif
4261
JsonVariant& getRoot() { return _root; }
4362
bool _sourceValid() const { return _isValid; }
4463
size_t setLength();
4564
size_t getSize() const { return _jsonBuffer.size(); }
4665
size_t _fillBuffer(uint8_t* data, size_t len);
66+
#if ARDUINOJSON_VERSION_MAJOR >= 6
67+
bool overflowed() const { return _jsonBuffer.overflowed(); }
68+
#endif
4769
};
4870

49-
class AsyncCallbackMessagePackWebHandler : public AsyncWebHandler {
50-
public:
51-
typedef std::function<void(AsyncWebServerRequest* request, JsonVariant& json)> ArJsonRequestHandlerFunction;
71+
typedef std::function<void(AsyncWebServerRequest* request, JsonVariant& json)> ArMessagePackRequestHandlerFunction;
5272

73+
class AsyncCallbackMessagePackWebHandler : public AsyncWebHandler {
5374
protected:
5475
const String _uri;
5576
WebRequestMethodComposite _method;
56-
ArJsonRequestHandlerFunction _onRequest;
77+
ArMessagePackRequestHandlerFunction _onRequest;
5778
size_t _contentLength;
79+
#if ARDUINOJSON_VERSION_MAJOR == 6
80+
const size_t maxJsonBufferSize;
81+
#endif
5882
size_t _maxContentLength;
5983

6084
public:
61-
AsyncCallbackMessagePackWebHandler(const String& uri, ArJsonRequestHandlerFunction onRequest = nullptr);
85+
#if ARDUINOJSON_VERSION_MAJOR == 6
86+
AsyncCallbackMessagePackWebHandler(const String& uri, ArMessagePackRequestHandlerFunction onRequest = nullptr, size_t maxJsonBufferSize = DYNAMIC_JSON_DOCUMENT_SIZE);
87+
#else
88+
AsyncCallbackMessagePackWebHandler(const String& uri, ArMessagePackRequestHandlerFunction onRequest = nullptr);
89+
#endif
6290

6391
void setMethod(WebRequestMethodComposite method) { _method = method; }
6492
void setMaxContentLength(int maxContentLength) { _maxContentLength = maxContentLength; }
65-
void onRequest(ArJsonRequestHandlerFunction fn) { _onRequest = fn; }
93+
void onRequest(ArMessagePackRequestHandlerFunction fn) { _onRequest = fn; }
94+
6695
virtual bool canHandle(AsyncWebServerRequest* request) override final;
6796
virtual void handleRequest(AsyncWebServerRequest* request) override final;
6897
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 {}
6998
virtual void handleBody(AsyncWebServerRequest* request, uint8_t* data, size_t len, size_t index, size_t total) override final;
70-
virtual bool isRequestHandlerTrivial() override final { return _onRequest ? false : true; }
99+
virtual bool isRequestHandlerTrivial() override final { return !_onRequest; }
71100
};
72101

73-
#else // __has_include("ArduinoJson.h")
74-
#define ASYNC_MSG_PACK_SUPPORT 0
75-
#endif // __has_include("ArduinoJson.h")
102+
#endif // ASYNC_MSG_PACK_SUPPORT == 1

src/ESPAsyncWebServer.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -48,10 +48,10 @@
4848

4949
#include "literals.h"
5050

51-
#define ASYNCWEBSERVER_VERSION "3.3.2"
51+
#define ASYNCWEBSERVER_VERSION "3.3.3"
5252
#define ASYNCWEBSERVER_VERSION_MAJOR 3
5353
#define ASYNCWEBSERVER_VERSION_MINOR 3
54-
#define ASYNCWEBSERVER_VERSION_REVISION 2
54+
#define ASYNCWEBSERVER_VERSION_REVISION 3
5555
#define ASYNCWEBSERVER_FORK_mathieucarbou
5656

5757
#ifdef ASYNCWEBSERVER_REGEX

0 commit comments

Comments
 (0)