mirror of
https://github.com/bblanchon/ArduinoJson.git
synced 2025-08-11 08:34:45 +02:00
Updated Bag of Tricks (markdown)
@@ -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 <FastCRC.h> // 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:
|
||||
|
Reference in New Issue
Block a user