From 59066bd1e4b51bc3a161e94adce10a89cde0a616 Mon Sep 17 00:00:00 2001 From: Mike Dunston Date: Tue, 24 Sep 2019 11:24:18 -0700 Subject: [PATCH] PrettyAsyncJsonResponse for ArduinoJson 6.x (#539) * PrettyAsyncJsonResponse for ArduinoJson 6.x add support for PrettyAsyncJsonResponse in ArduinoJson 6.x * Update AsyncJson.h Maintain order of args to AsyncJsonResponse between ArduinoJson 5.x and 6.x compatibility for ease of migration --- src/AsyncJson.h | 11 +++++++++-- 1 file changed, 9 insertions(+), 2 deletions(-) diff --git a/src/AsyncJson.h b/src/AsyncJson.h index 4a6d6ba..6118afe 100644 --- a/src/AsyncJson.h +++ b/src/AsyncJson.h @@ -101,7 +101,7 @@ class AsyncJsonResponse: public AsyncAbstractResponse { _root = _jsonBuffer.createObject(); } #else - AsyncJsonResponse(size_t maxJsonBufferSize = DYNAMIC_JSON_DOCUMENT_SIZE, bool isArray=false) : _jsonBuffer(maxJsonBufferSize), _isValid{false} { + AsyncJsonResponse(bool isArray=false, size_t maxJsonBufferSize = DYNAMIC_JSON_DOCUMENT_SIZE) : _jsonBuffer(maxJsonBufferSize), _isValid{false} { _code = 200; _contentType = JSON_MIMETYPE; if(isArray) @@ -140,18 +140,25 @@ class AsyncJsonResponse: public AsyncAbstractResponse { } }; -#ifdef ARDUINOJSON_5_COMPATIBILITY class PrettyAsyncJsonResponse: public AsyncJsonResponse { public: PrettyAsyncJsonResponse (bool isArray=false) : AsyncJsonResponse{isArray} {} size_t setLength () { +#ifdef ARDUINOJSON_5_COMPATIBILITY _contentLength = _root.measurePrettyLength (); +#else + _contentLength = measureJsonPretty(_root); +#endif if (_contentLength) {_isValid = true;} return _contentLength; } size_t _fillBuffer (uint8_t *data, size_t len) { ChunkPrint dest (data, _sentLength, len); +#ifdef ARDUINOJSON_5_COMPATIBILITY _root.prettyPrintTo (dest); +#else + serializeJsonPretty(_root, dest); +#endif return len; } };