mirror of
https://github.com/bblanchon/ArduinoJson.git
synced 2025-08-12 17:14:48 +02:00
Updated Bag of Tricks (markdown)
@@ -74,4 +74,33 @@ To use this in your code:
|
|||||||
root.printTo(ChunkPrint(Serial,10,20)); // print only range [10,20[
|
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).
|
Reference in New Issue
Block a user