9static const char *
const TAG =
"json";
12struct SpiRamAllocator : ArduinoJson::Allocator {
13 void *allocate(
size_t size)
override {
return this->allocator_.
allocate(size); }
15 void deallocate(
void *pointer)
override {
25 void *reallocate(
void *ptr,
size_t new_size)
override {
26 return this->allocator_.
reallocate(
static_cast<uint8_t *
>(ptr), new_size);
35 auto doc_allocator = SpiRamAllocator();
36 JsonDocument json_document(&doc_allocator);
37 if (json_document.overflowed()) {
38 ESP_LOGE(TAG,
"Could not allocate memory for JSON document!");
41 JsonObject root = json_document.to<JsonObject>();
43 if (json_document.overflowed()) {
44 ESP_LOGE(TAG,
"Could not allocate memory for JSON document!");
48 serializeJson(json_document, output);
55 auto doc_allocator = SpiRamAllocator();
56 JsonDocument json_document(&doc_allocator);
57 if (json_document.overflowed()) {
58 ESP_LOGE(TAG,
"Could not allocate memory for JSON document!");
61 DeserializationError err = deserializeJson(json_document, data);
63 JsonObject root = json_document.as<JsonObject>();
65 if (err == DeserializationError::Ok) {
67 }
else if (err == DeserializationError::NoMemory) {
68 ESP_LOGE(TAG,
"Can not allocate more memory for deserialization. Consider making source string smaller");
71 ESP_LOGE(TAG,
"Parse error: %s", err.c_str());
An STL allocator that uses SPI or internal RAM.
T * reallocate(T *p, size_t n)
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::string build_json(const json_build_t &f)
Build a JSON string with the provided json build function.
std::function< bool(JsonObject)> json_parse_t
Callback function typedef for parsing JsonObjects.
Providing packet encoding functions for exchanging data with a remote host.