14#define ARDUINOJSON_ENABLE_STD_STRING 1
16#define ARDUINOJSON_USE_LONG_LONG 1
18#include <ArduinoJson.h>
24inline JsonString
linked(
const char *s) {
return JsonString(s,
true); }
25inline JsonString
linked(
const char *s,
size_t len) {
return JsonString(s,
len,
true); }
26inline JsonString
linked(
const std::string &s) {
return JsonString(s.c_str(), s.size(),
true); }
27JsonString
linked(std::string &&) =
delete;
42 if (
size + 1 <= STACK_SIZE) {
43 buffer_ = stack_buffer_;
45 heap_buffer_ =
new char[
size + 1];
46 buffer_ = heap_buffer_;
55 : heap_buffer_(std::exchange(other.heap_buffer_,
nullptr)), size_(other.size_) {
56 if (other.buffer_ == other.stack_buffer_) {
58 std::memcpy(stack_buffer_, other.stack_buffer_, size_ + 1);
59 buffer_ = stack_buffer_;
62 buffer_ = heap_buffer_;
65 other.stack_buffer_[0] =
'\0';
66 other.buffer_ = other.stack_buffer_;
73 delete[] heap_buffer_;
74 heap_buffer_ = std::exchange(other.heap_buffer_,
nullptr);
76 if (other.buffer_ == other.stack_buffer_) {
77 std::memcpy(stack_buffer_, other.stack_buffer_, size_ + 1);
78 buffer_ = stack_buffer_;
80 buffer_ = heap_buffer_;
83 other.stack_buffer_[0] =
'\0';
84 other.buffer_ = other.stack_buffer_;
95 const char *
c_str()
const {
return buffer_; }
97 const char *
data()
const {
return buffer_; }
99 size_t size()
const {
return size_; }
104 operator std::string()
const {
return std::string(buffer_, size_); }
110 char *data_writable_() {
return buffer_; }
113 void set_size_(
size_t size) {
115 buffer_[
size] =
'\0';
120 void reallocate_heap_(
size_t size) {
121 delete[] heap_buffer_;
122 heap_buffer_ =
new char[
size + 1];
123 buffer_ = heap_buffer_;
128 char stack_buffer_[STACK_SIZE];
129 char *heap_buffer_{
nullptr};
155 return allocator.
reallocate(
static_cast<uint8_t *
>(ptr), new_size);
179 return parse_json(
reinterpret_cast<const uint8_t *
>(data.c_str()), data.size());
187constexpr size_t JSON_POOL_BYTES = ARDUINOJSON_POOL_CAPACITY *
sizeof(ArduinoJson::detail::VariantData);
190static_assert(
sizeof(
void *) != 4 ||
JSON_ARENA_SIZE == 2176,
"the arena was sized for a 1 KB pool");
194template<
size_t N>
class JsonArena final :
public ArduinoJson::Allocator {
204 return this->fallback_->allocate(
size);
206 size = (
size + ALIGN - 1) & ~(ALIGN - 1);
207 if (
size > N - this->used_) {
208 return this->fallback_->allocate(
size);
210 this->last_ = this->used_;
212 return this->buf_ + this->last_;
215 if (!this->owns_(ptr)) {
216 this->fallback_->deallocate(ptr);
220 if (!this->owns_(ptr)) {
221 return this->fallback_->reallocate(ptr, new_size);
223 const size_t off =
static_cast<uint8_t *
>(ptr) - this->buf_;
224 const size_t size = new_size > N ? N + ALIGN : (new_size + ALIGN - 1) & ~(ALIGN - 1);
225 const bool newest = off == this->last_;
226 if (newest &&
size <= N - off) {
227 this->used_ = off +
size;
231 const size_t old_size = newest ? this->used_ - off : N - off;
232 void *moved = this->fallback_->allocate(new_size);
233 if (moved ==
nullptr) {
236 std::memcpy(moved, ptr, std::min(new_size, old_size));
243 size_t used()
const {
return this->used_; }
246 static constexpr size_t ALIGN =
alignof(std::max_align_t);
247 bool owns_(
const void *ptr)
const {
return ptr >= this->buf_ && ptr < this->buf_ + N; }
248 ArduinoJson::Allocator *fallback_;
249 alignas(ALIGN) uint8_t buf_[N];
262 explicit JsonBuilder(ArduinoJson::Allocator *allocator);
266 if (!root_created_) {
267 root_ = doc_.to<JsonObject>();
268 root_created_ =
true;
284 bool root_created_{
false};
An STL allocator that uses SPI or internal RAM.
T * reallocate(T *p, size_t n)
Bump allocator over a fixed buffer for a document built and serialized in one scope.
JsonArena(ArduinoJson::Allocator *fallback=heap_json_allocator())
void * allocate(size_t size) override
void * reallocate(void *ptr, size_t new_size) override
JsonArena(const JsonArena &)=delete
JsonArena & operator=(const JsonArena &)=delete
size_t used() const
Bytes of the buffer handed out so far.
void deallocate(void *ptr) override
Builder class for creating JSON documents without lambdas.
size_t serialize_to(char *buf, size_t cap)
Serialize into a caller owned buffer.
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.
ArduinoJson::Allocator * heap_json_allocator()
The allocator a JsonBuilder uses by default (PSRAM first when available)
constexpr size_t JSON_ARENA_SIZE
One pool plus room for copied string nodes; a 40 option select with linked options fits.
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.
constexpr size_t JSON_POOL_BYTES
Size of one ArduinoJson slot pool, the first allocation every document makes (1 KB on 32 bit targets)
std::function< bool(JsonObject)> json_parse_t
Callback function typedef for parsing JsonObjects.
JsonString linked(const char *s)
SerializationBuffer build_json(const json_build_t &f)
Build a JSON string with the provided json build function.
const __FlashStringHelper * ProgmemStr
void deallocate(void *ptr) override
void * allocate(size_t size) override
void * reallocate(void *ptr, size_t new_size) override