10#define ARDUINOJSON_ENABLE_STD_STRING 1
12#define ARDUINOJSON_USE_LONG_LONG 1
14#include <ArduinoJson.h>
27 if (
size + 1 <= STACK_SIZE) {
28 buffer_ = stack_buffer_;
30 heap_buffer_ =
new char[
size + 1];
31 buffer_ = heap_buffer_;
40 if (other.buffer_ == other.stack_buffer_) {
42 std::memcpy(stack_buffer_, other.stack_buffer_, size_ + 1);
43 buffer_ = stack_buffer_;
46 buffer_ = heap_buffer_;
47 other.heap_buffer_ =
nullptr;
50 other.stack_buffer_[0] =
'\0';
51 other.buffer_ = other.stack_buffer_;
58 delete[] heap_buffer_;
59 heap_buffer_ = other.heap_buffer_;
61 if (other.buffer_ == other.stack_buffer_) {
62 std::memcpy(stack_buffer_, other.stack_buffer_, size_ + 1);
63 buffer_ = stack_buffer_;
65 buffer_ = heap_buffer_;
66 other.heap_buffer_ =
nullptr;
69 other.stack_buffer_[0] =
'\0';
70 other.buffer_ = other.stack_buffer_;
81 const char *
c_str()
const {
return buffer_; }
83 const char *
data()
const {
return buffer_; }
85 size_t size()
const {
return size_; }
90 operator std::string()
const {
return std::string(buffer_, size_); }
96 char *data_writable_() {
return buffer_; }
99 void set_size_(
size_t size) {
101 buffer_[
size] =
'\0';
106 void reallocate_heap_(
size_t size) {
107 delete[] heap_buffer_;
108 heap_buffer_ =
new char[
size + 1];
109 buffer_ = heap_buffer_;
114 char stack_buffer_[STACK_SIZE];
115 char *heap_buffer_{
nullptr};
141 return allocator.
reallocate(
static_cast<uint8_t *
>(ptr), new_size);
165 return parse_json(
reinterpret_cast<const uint8_t *
>(data.c_str()), data.size());
172 if (!root_created_) {
173 root_ = doc_.to<JsonObject>();
174 root_created_ =
true;
186 JsonDocument doc_{&allocator_};
191 bool root_created_{
false};
An STL allocator that uses SPI or internal RAM.
T * reallocate(T *p, size_t n)
Builder class for creating JSON documents without lambdas.
SerializationBuffer serialize()
Serialize the JSON document to a SerializationBuffer (stack-first allocation) Uses 512-byte stack buf...
Buffer for JSON serialization that uses stack allocation for small payloads.
static constexpr size_t BUFFER_SIZE
Stack buffer size for this instantiation.
size_t size() const
Get string length (excluding null terminator)
SerializationBuffer(size_t size)
Construct with known size (typically from measureJson)
SerializationBuffer & operator=(const SerializationBuffer &)=delete
const char * data() const
Get data pointer.
SerializationBuffer(const SerializationBuffer &)=delete
const char * c_str() const
Get null-terminated C string.
SerializationBuffer & operator=(SerializationBuffer &&other) noexcept
SerializationBuffer(SerializationBuffer &&other) noexcept
std::function< void(JsonObject)> json_build_t
Callback function typedef for building JsonObjects.
bool parse_json(const std::string &data, const json_parse_t &f)
Parse a JSON string and run the provided json parse function if it's valid.
std::function< bool(JsonObject)> json_parse_t
Callback function typedef for parsing JsonObjects.
SerializationBuffer build_json(const json_build_t &f)
Build a JSON string with the provided json build function.
void deallocate(void *ptr) override
void * allocate(size_t size) override
void * reallocate(void *ptr, size_t new_size) override