From 781bddf20cbf7db3a9a4b01937699cff1e52bceb Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?G=C3=A9rald=20Guiony?= <42507043+gerald-guiony@users.noreply.github.com> Date: Sat, 22 Jun 2019 18:24:01 +0200 Subject: [PATCH] json response is more human readable with PrettyAsyncJsonResponse class (#398) * Update AsyncJson.h Adding PrettyAsyncJsonResponse class * Update AsyncJson.h * Adding a PrettyAsyncJsonResponse class json response is more human readable than with AsyncJsonResponse --- src/AsyncJson.h | 17 ++++++++++++++++- 1 file changed, 16 insertions(+), 1 deletion(-) diff --git a/src/AsyncJson.h b/src/AsyncJson.h index b9631ea..52b12f7 100644 --- a/src/AsyncJson.h +++ b/src/AsyncJson.h @@ -78,7 +78,7 @@ class ChunkPrint : public Print { }; class AsyncJsonResponse: public AsyncAbstractResponse { - private: + protected: #ifdef ARDUINOJSON_5_COMPATIBILITY DynamicJsonBuffer _jsonBuffer; @@ -140,6 +140,21 @@ class AsyncJsonResponse: public AsyncAbstractResponse { } }; +class PrettyAsyncJsonResponse: public AsyncJsonResponse { +public: + PrettyAsyncJsonResponse (bool isArray=false) : AsyncJsonResponse{isArray} {} + size_t setLength () { + _contentLength = _root.measurePrettyLength (); + if (_contentLength) {_isValid = true;} + return _contentLength; + } + size_t _fillBuffer (uint8_t *data, size_t len) { + ChunkPrint dest (data, _sentLength, len); + _root.prettyPrintTo (dest); + return len; + } +}; + typedef std::function ArJsonRequestHandlerFunction; class AsyncCallbackJsonWebHandler: public AsyncWebHandler {