10#define ARDUINOJSON_ENABLE_STD_STRING 1
12#define ARDUINOJSON_USE_LONG_LONG 1
14#include <ArduinoJson.h>
28 if (
size + 1 <= STACK_SIZE) {
29 buffer_ = stack_buffer_;
31 heap_buffer_ =
new char[
size + 1];
32 buffer_ = heap_buffer_;
41 if (other.buffer_ == other.stack_buffer_) {
43 std::memcpy(stack_buffer_, other.stack_buffer_, size_ + 1);
44 buffer_ = stack_buffer_;
47 buffer_ = heap_buffer_;
48 other.heap_buffer_ =
nullptr;
51 other.stack_buffer_[0] =
'\0';
52 other.buffer_ = other.stack_buffer_;
59 delete[] heap_buffer_;
60 heap_buffer_ = other.heap_buffer_;
62 if (other.buffer_ == other.stack_buffer_) {
63 std::memcpy(stack_buffer_, other.stack_buffer_, size_ + 1);
64 buffer_ = stack_buffer_;
66 buffer_ = heap_buffer_;
67 other.heap_buffer_ =
nullptr;
70 other.stack_buffer_[0] =
'\0';
71 other.buffer_ = other.stack_buffer_;
82 const char *
c_str()
const {
return buffer_; }
84 const char *
data()
const {
return buffer_; }
86 size_t size()
const {
return size_; }
91 operator std::string()
const {
return std::string(buffer_, size_); }
97 char *data_writable_() {
return buffer_; }
100 void set_size_(
size_t size) {
102 buffer_[
size] =
'\0';
107 void reallocate_heap_(
size_t size) {
108 delete[] heap_buffer_;
109 heap_buffer_ =
new char[
size + 1];
110 buffer_ = heap_buffer_;
115 char stack_buffer_[STACK_SIZE];
116 char *heap_buffer_{
nullptr};
142 return allocator.
reallocate(
static_cast<uint8_t *
>(ptr), new_size);
166 return parse_json(
reinterpret_cast<const uint8_t *
>(data.c_str()), data.size());
173 if (!root_created_) {
174 root_ = doc_.to<JsonObject>();
175 root_created_ =
true;
187 JsonDocument doc_{&allocator_};
192 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.
Providing packet encoding functions for exchanging data with a remote host.
void deallocate(void *ptr) override
void * allocate(size_t size) override
void * reallocate(void *ptr, size_t new_size) override