From 34a19df6aeceb431ea78690a65c78d66a56c564b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Beno=C3=AEt=20Blanchon?= Date: Fri, 25 Nov 2016 12:23:17 +0100 Subject: [PATCH] Updated Bag of Tricks (markdown) --- Bag-of-Tricks.md | 43 +++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 43 insertions(+) diff --git a/Bag-of-Tricks.md b/Bag-of-Tricks.md index d9dae0b..8139ee8 100644 --- a/Bag-of-Tricks.md +++ b/Bag-of-Tricks.md @@ -247,6 +247,49 @@ root.printTo(chunkPrint); See issue [#206](https://github.com/bblanchon/ArduinoJson/issues/206). +## Compute hash of JSON output + +Here is how you can compute the CRC32 hash of the JSON output without consuming a lot of memory. + +This can be very handy to compare two JSON trees. + +```c++ +#include // https://github.com/FrankBoesing/FastCRC + +class HashPrint : public Print { +public: + HashPrint() + { + _hash = _hasher.crc32(NULL, 0); + } + + virtual size_t write(uint8_t c) + { + _hash = _hasher.crc32_upd(&c, 1); + } + + uint32_t hash() const + { + return _hash; + } + +private: + FastCRC32 _hasher; + uint32_t _hash; +}; +``` + +To use this in your code: + +```c++ +HashPrint hashPrint; +root.printTo(hashPrint); +Serial.println(hashPrint.hash()); +``` + +See issue [#390](https://github.com/bblanchon/ArduinoJson/issues/390). + + ## Throw exception when JsonBuffer is too small Here is a static decorator that will behave as the decorated `JsonBuffer`, except that it will throw an exception if the allocation fails: