Updated Bag of Tricks (markdown)

Benoît Blanchon
2016-01-24 16:13:49 +01:00
parent 2d7b6ae598
commit 83f51f199a

@@ -74,4 +74,33 @@ To use this in your code:
root.printTo(ChunkPrint(Serial,10,20)); // print only range [10,20[
```
See issue [#206](https://github.com/bblanchon/ArduinoJson/issues/206).
See issue [#206](https://github.com/bblanchon/ArduinoJson/issues/206).
## 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:
```c++
#include <stdexcept>
template <typename TJsonBuffer>
struct Throwing : TJsonBuffer {
virtual void *alloc(size_t bytes) {
void *ptr = TJsonBuffer::alloc(bytes);
if (ptr)
return ptr;
else
throw std::runtime_error("allocation failed");
}
};
```
To use this in your code:
```c++
Throwing<StaticJsonBuffer<200> > jsonBuffer;
// or
Throwing<DynamicJsonBuffer> jsonBuffer;
```
See issue [#205](https://github.com/bblanchon/ArduinoJson/issues/205).