Skip to content

Commit f00574f

Browse files
committed
Make JSON document sizes configurable.
Fixes #96
1 parent 3d61517 commit f00574f

File tree

3 files changed

+65
-17
lines changed

3 files changed

+65
-17
lines changed

ESPWebThingAdapter.h

+23-7
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,22 @@
2525

2626
#define ESP_MAX_PUT_BODY_SIZE 512
2727

28+
#ifndef LARGE_JSON_DOCUMENT_SIZE
29+
#ifdef LARGE_JSON_BUFFERS
30+
#define LARGE_JSON_DOCUMENT_SIZE 4096
31+
#else
32+
#define LARGE_JSON_DOCUMENT_SIZE 1024
33+
#endif
34+
#endif
35+
36+
#ifndef SMALL_JSON_DOCUMENT_SIZE
37+
#ifdef LARGE_JSON_BUFFERS
38+
#define SMALL_JSON_DOCUMENT_SIZE 1024
39+
#else
40+
#define SMALL_JSON_DOCUMENT_SIZE 256
41+
#endif
42+
#endif
43+
2844
class WebThingAdapter {
2945
public:
3046
WebThingAdapter(String _name, IPAddress _ip, uint16_t _port = 80)
@@ -101,7 +117,7 @@ class WebThingAdapter {
101117
void sendChangedPropsOrEvents(ThingDevice *device, const char *type,
102118
ThingItem *rootItem) {
103119
// Prepare one buffer per device
104-
DynamicJsonDocument message(1024);
120+
DynamicJsonDocument message(LARGE_JSON_DOCUMENT_SIZE);
105121
message["messageType"] = type;
106122
JsonObject prop = message.createNestedObject("data");
107123
bool dataToSend = false;
@@ -224,7 +240,7 @@ class WebThingAdapter {
224240
// For now each Thing stores its own Websocket connection object therefore.
225241

226242
// Parse request
227-
DynamicJsonDocument newProp(1024);
243+
DynamicJsonDocument newProp(LARGE_JSON_DOCUMENT_SIZE);
228244
auto error = deserializeJson(newProp, rawData);
229245
if (error) {
230246
sendErrorMsg(newProp, *client, 400, "Invalid json");
@@ -270,7 +286,7 @@ class WebThingAdapter {
270286
AsyncResponseStream *response =
271287
request->beginResponseStream("application/json");
272288

273-
DynamicJsonDocument buf(1024);
289+
DynamicJsonDocument buf(LARGE_JSON_DOCUMENT_SIZE);
274290
JsonArray things = buf.to<JsonArray>();
275291
ThingDevice *device = this->firstDevice;
276292
while (device != nullptr) {
@@ -296,7 +312,7 @@ class WebThingAdapter {
296312
AsyncResponseStream *response =
297313
request->beginResponseStream("application/json");
298314

299-
DynamicJsonDocument buf(1024);
315+
DynamicJsonDocument buf(LARGE_JSON_DOCUMENT_SIZE);
300316
JsonObject descr = buf.to<JsonObject>();
301317
device->serialize(descr
302318
#ifndef WITHOUT_WS
@@ -316,7 +332,7 @@ class WebThingAdapter {
316332
AsyncResponseStream *response =
317333
request->beginResponseStream("application/json");
318334

319-
DynamicJsonDocument doc(256);
335+
DynamicJsonDocument doc(SMALL_JSON_DOCUMENT_SIZE);
320336
JsonObject prop = doc.to<JsonObject>();
321337
item->serialize(prop);
322338
serializeJson(prop, *response);
@@ -327,7 +343,7 @@ class WebThingAdapter {
327343
AsyncResponseStream *response =
328344
request->beginResponseStream("application/json");
329345

330-
DynamicJsonDocument doc(256);
346+
DynamicJsonDocument doc(LARGE_JSON_DOCUMENT_SIZE);
331347
JsonObject prop = doc.to<JsonObject>();
332348
ThingItem *item = rootItem;
333349
while (item != nullptr) {
@@ -390,7 +406,7 @@ class WebThingAdapter {
390406
return;
391407
}
392408

393-
DynamicJsonDocument newBuffer(256);
409+
DynamicJsonDocument newBuffer(SMALL_JSON_DOCUMENT_SIZE);
394410
auto error = deserializeJson(newBuffer, body_data);
395411
if (error) { // unable to parse json
396412
b_has_body_data = false;

EthernetWebThingAdapter.h

+21-5
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,22 @@ MDNS mdns(udp);
3939
#define WITHOUT_WS 1
4040
#include "Thing.h"
4141

42+
#ifndef LARGE_JSON_DOCUMENT_SIZE
43+
#ifdef LARGE_JSON_BUFFERS
44+
#define LARGE_JSON_DOCUMENT_SIZE 4096
45+
#else
46+
#define LARGE_JSON_DOCUMENT_SIZE 1024
47+
#endif
48+
#endif
49+
50+
#ifndef SMALL_JSON_DOCUMENT_SIZE
51+
#ifdef LARGE_JSON_BUFFERS
52+
#define SMALL_JSON_DOCUMENT_SIZE 1024
53+
#else
54+
#define SMALL_JSON_DOCUMENT_SIZE 256
55+
#endif
56+
#endif
57+
4258
static const bool DEBUG = false;
4359

4460
enum HTTPMethod { HTTP_ANY, HTTP_GET, HTTP_PUT, HTTP_OPTIONS };
@@ -332,7 +348,7 @@ class WebThingAdapter {
332348
sendOk();
333349
sendHeaders();
334350

335-
DynamicJsonDocument buf(1024);
351+
DynamicJsonDocument buf(LARGE_JSON_DOCUMENT_SIZE);
336352
JsonArray things = buf.to<JsonArray>();
337353
ThingDevice *device = this->firstDevice;
338354
while (device != nullptr) {
@@ -351,7 +367,7 @@ class WebThingAdapter {
351367
sendOk();
352368
sendHeaders();
353369

354-
DynamicJsonDocument buf(1024);
370+
DynamicJsonDocument buf(LARGE_JSON_DOCUMENT_SIZE);
355371
JsonObject descr = buf.to<JsonObject>();
356372
device->serialize(descr);
357373

@@ -364,7 +380,7 @@ class WebThingAdapter {
364380
sendOk();
365381
sendHeaders();
366382

367-
DynamicJsonDocument doc(256);
383+
DynamicJsonDocument doc(SMALL_JSON_DOCUMENT_SIZE);
368384
JsonObject prop = doc.to<JsonObject>();
369385
item->serialize(prop);
370386
serializeJson(prop, client);
@@ -376,7 +392,7 @@ class WebThingAdapter {
376392
sendOk();
377393
sendHeaders();
378394

379-
DynamicJsonDocument doc(256);
395+
DynamicJsonDocument doc(LARGE_JSON_DOCUMENT_SIZE);
380396
JsonObject prop = doc.to<JsonObject>();
381397
ThingItem *item = rootItem;
382398
while (item != nullptr) {
@@ -422,7 +438,7 @@ class WebThingAdapter {
422438
void handleThingPropertyPut(ThingProperty *property) {
423439
sendOk();
424440
sendHeaders();
425-
DynamicJsonDocument newBuffer(256);
441+
DynamicJsonDocument newBuffer(SMALL_JSON_DOCUMENT_SIZE);
426442
auto error = deserializeJson(newBuffer, content);
427443
if (error) { // unable to parse json
428444
handleError();

WiFi101WebThingAdapter.h

+21-5
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,22 @@
2727
#define WITHOUT_WS 1
2828
#include "Thing.h"
2929

30+
#ifndef LARGE_JSON_DOCUMENT_SIZE
31+
#ifdef LARGE_JSON_BUFFERS
32+
#define LARGE_JSON_DOCUMENT_SIZE 4096
33+
#else
34+
#define LARGE_JSON_DOCUMENT_SIZE 1024
35+
#endif
36+
#endif
37+
38+
#ifndef SMALL_JSON_DOCUMENT_SIZE
39+
#ifdef LARGE_JSON_BUFFERS
40+
#define SMALL_JSON_DOCUMENT_SIZE 1024
41+
#else
42+
#define SMALL_JSON_DOCUMENT_SIZE 256
43+
#endif
44+
#endif
45+
3046
static const bool DEBUG = false;
3147

3248
enum HTTPMethod { HTTP_ANY, HTTP_GET, HTTP_PUT, HTTP_OPTIONS };
@@ -312,7 +328,7 @@ class WebThingAdapter {
312328
sendOk();
313329
sendHeaders();
314330

315-
DynamicJsonDocument buf(1024);
331+
DynamicJsonDocument buf(LARGE_JSON_DOCUMENT_SIZE);
316332
JsonArray things = buf.to<JsonArray>();
317333
ThingDevice *device = this->firstDevice;
318334
while (device != nullptr) {
@@ -331,7 +347,7 @@ class WebThingAdapter {
331347
sendOk();
332348
sendHeaders();
333349

334-
DynamicJsonDocument buf(1024);
350+
DynamicJsonDocument buf(LARGE_JSON_DOCUMENT_SIZE);
335351
JsonObject descr = buf.to<JsonObject>();
336352
device->serialize(descr);
337353

@@ -344,7 +360,7 @@ class WebThingAdapter {
344360
sendOk();
345361
sendHeaders();
346362

347-
DynamicJsonDocument doc(256);
363+
DynamicJsonDocument doc(SMALL_JSON_DOCUMENT_SIZE);
348364
JsonObject prop = doc.to<JsonObject>();
349365
item->serialize(prop);
350366
serializeJson(prop, client);
@@ -356,7 +372,7 @@ class WebThingAdapter {
356372
sendOk();
357373
sendHeaders();
358374

359-
DynamicJsonDocument doc(256);
375+
DynamicJsonDocument doc(LARGE_JSON_DOCUMENT_SIZE);
360376
JsonObject prop = doc.to<JsonObject>();
361377
ThingItem *item = rootItem;
362378
while (item != nullptr) {
@@ -402,7 +418,7 @@ class WebThingAdapter {
402418
void handleThingPropertyPut(ThingProperty *property) {
403419
sendOk();
404420
sendHeaders();
405-
DynamicJsonDocument newBuffer(256);
421+
DynamicJsonDocument newBuffer(SMALL_JSON_DOCUMENT_SIZE);
406422
auto error = deserializeJson(newBuffer, content);
407423
if (error) { // unable to parse json
408424
handleError();

0 commit comments

Comments
 (0)