diff --git a/Bag-of-Tricks.md b/Bag-of-Tricks.md index 57aa4df..9c1c64c 100644 --- a/Bag-of-Tricks.md +++ b/Bag-of-Tricks.md @@ -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). \ No newline at end of file +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 + +template +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 > jsonBuffer; +// or +Throwing jsonBuffer; +``` + +See issue [#205](https://github.com/bblanchon/ArduinoJson/issues/205). \ No newline at end of file