22 JsonObject root = builder.
root();
30 return parse_json(
reinterpret_cast<const uint8_t *
>(data.c_str()), data.size(), f);
36 if (doc.overflowed() || doc.isNull())
38 return f(doc.as<JsonObject>());
45 if (data ==
nullptr ||
len == 0) {
46 ESP_LOGE(TAG,
"No data to parse");
50 if (json_document.overflowed()) {
51 ESP_LOGE(TAG,
"Could not allocate memory for JSON document!");
54 DeserializationError err = deserializeJson(json_document, data,
len);
56 if (err == DeserializationError::Ok) {
58 }
else if (err == DeserializationError::NoMemory) {
59 ESP_LOGE(TAG,
"Can not allocate more memory for deserialization. Consider making source string smaller");
62 ESP_LOGE(TAG,
"Parse error: %s", err.c_str());
74 return &global_json_allocator;
76 return ArduinoJson::detail::DefaultAllocator::instance();
81 if (doc_.overflowed()) {
82 ESP_LOGE(TAG,
"JSON document overflow");
93 return serializeJson(doc_, buf, cap);
138 if (
size < buf_size) {
140 result.set_size_(
size);
148 constexpr size_t max_heap_size = 5120;
149 size_t heap_size = buf_size * 2;
150 while (heap_size <= max_heap_size) {
151 result.reallocate_heap_(heap_size - 1);
153 if (
size < heap_size) {
154 result.set_size_(
size);
162 size_t max_content = heap_size / 2 - 1;
163 ESP_LOGW(TAG,
"JSON payload too large, truncated to %zu bytes", max_content);
164 result.set_size_(max_content);
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.
std::function< void(JsonObject)> json_build_t
Callback function typedef for building JsonObjects.
ESPHOME_LOG_TAG(TAG, "json")
ArduinoJson::Allocator * heap_json_allocator()
The allocator a JsonBuilder uses by default (PSRAM first when available)
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.