From cacf3b0c49e4f63a8753493def5e51ac027e7350 Mon Sep 17 00:00:00 2001 From: Andrew Melvin Date: Thu, 28 Jan 2016 18:34:27 +0200 Subject: [PATCH 1/4] Add header to extend server for arduinojson --- src/ESPasyncJson.h | 76 ++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 76 insertions(+) create mode 100644 src/ESPasyncJson.h diff --git a/src/ESPasyncJson.h b/src/ESPasyncJson.h new file mode 100644 index 0000000..198a941 --- /dev/null +++ b/src/ESPasyncJson.h @@ -0,0 +1,76 @@ +// ESPasyncJson.h +/* + Async Response to use with arduinoJson and asyncwebserver + Written by Andrew Melvin (SticilFace) with help from me-no-dev and BBlanchon. + + example of callback in use + + server.on("/json", HTTP_ANY, [](AsyncWebServerRequest * request) { + + AsyncJsonResponse * response = new AsyncJsonResponse(); + JsonObject& root = response->getRoot(); + root["key1"] = "key number one"; + JsonObject& nested = root.createNestedObject("nested"); + nested["key1"] = "key number one"; + + response->setLength(); + request->send(response); + }); + +*/ + +#pragma once +#include + +/* + * Json Response + * */ + +class ChunkPrint : public Print{ + private: + uint8_t* _destination; + size_t _to_skip; + size_t _to_write; + size_t _pos; + public: + ChunkPrint(uint8_t* destination, size_t from, size_t len) + : _destination(destination), _to_skip(from), _to_write(len), _pos{0} {} + size_t write(uint8_t c){ + if (_to_skip > 0) { + _to_skip--; + return 0; + } else if (_to_write > 0) { + _to_write--; + _destination[_pos++] = c; + return 1; + } + return 0; + } +}; + +class AsyncJsonResponse: public AsyncAbstractResponse { + private: + DynamicJsonBuffer _jsonBuffer; + JsonVariant _root; + bool _isValid; + public: + AsyncJsonResponse(): _isValid{false} { + _code = 200; + _contentType = "text/json"; + _root = _jsonBuffer.createObject(); + } + ~AsyncJsonResponse() {} + JsonVariant & getRoot() { return _root; } + bool _sourceValid() { return _isValid; } + bool setLength() { + + _contentLength = _root.measureLength(); + if (_contentLength) { _isValid = true; } + } + + size_t _fillBuffer(uint8_t *data, size_t len){ + ChunkPrint dest(data, _sentLength, len); + _root.printTo( dest ) ; + return len; + } +}; \ No newline at end of file From d9234dbc9c72e9a8daa2c394818666abcd767b0a Mon Sep 17 00:00:00 2001 From: Andrew Melvin Date: Thu, 28 Jan 2016 18:49:55 +0200 Subject: [PATCH 2/4] rename to AsyncJson.h from ESPasyncJson.h --- src/{ESPasyncJson.h => AsyncJson.h} | 0 1 file changed, 0 insertions(+), 0 deletions(-) rename src/{ESPasyncJson.h => AsyncJson.h} (100%) diff --git a/src/ESPasyncJson.h b/src/AsyncJson.h similarity index 100% rename from src/ESPasyncJson.h rename to src/AsyncJson.h From 45b74f0cb6e6152f3a0e6cc7c93ee066a6256c1a Mon Sep 17 00:00:00 2001 From: Andrew Melvin Date: Thu, 28 Jan 2016 20:31:56 +0200 Subject: [PATCH 3/4] fix return in skip make setLength void make destructor virtual --- src/AsyncJson.h | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/AsyncJson.h b/src/AsyncJson.h index 198a941..481a27b 100644 --- a/src/AsyncJson.h +++ b/src/AsyncJson.h @@ -38,7 +38,7 @@ class ChunkPrint : public Print{ size_t write(uint8_t c){ if (_to_skip > 0) { _to_skip--; - return 0; + return 1; } else if (_to_write > 0) { _to_write--; _destination[_pos++] = c; @@ -59,10 +59,10 @@ class AsyncJsonResponse: public AsyncAbstractResponse { _contentType = "text/json"; _root = _jsonBuffer.createObject(); } - ~AsyncJsonResponse() {} + virtual ~AsyncJsonResponse() {} JsonVariant & getRoot() { return _root; } bool _sourceValid() { return _isValid; } - bool setLength() { + void setLength() { _contentLength = _root.measureLength(); if (_contentLength) { _isValid = true; } From fe49272cae3d927e0760547b97052221c561f0d3 Mon Sep 17 00:00:00 2001 From: Andrew Melvin Date: Thu, 28 Jan 2016 20:47:55 +0200 Subject: [PATCH 4/4] Oops --- src/AsyncJson.h | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/AsyncJson.h b/src/AsyncJson.h index 481a27b..816bd3c 100644 --- a/src/AsyncJson.h +++ b/src/AsyncJson.h @@ -35,6 +35,7 @@ class ChunkPrint : public Print{ public: ChunkPrint(uint8_t* destination, size_t from, size_t len) : _destination(destination), _to_skip(from), _to_write(len), _pos{0} {} + virtual ~ChunkPrint(); size_t write(uint8_t c){ if (_to_skip > 0) { _to_skip--; @@ -59,7 +60,7 @@ class AsyncJsonResponse: public AsyncAbstractResponse { _contentType = "text/json"; _root = _jsonBuffer.createObject(); } - virtual ~AsyncJsonResponse() {} + ~AsyncJsonResponse() {} JsonVariant & getRoot() { return _root; } bool _sourceValid() { return _isValid; } void setLength() {