9static const char *
const TAG =
"json";
21 JsonObject root = builder.
root();
29 return parse_json(
reinterpret_cast<const uint8_t *
>(data.c_str()), data.size(), f);
35 if (doc.overflowed() || doc.isNull())
37 return f(doc.as<JsonObject>());
43 if (data ==
nullptr ||
len == 0) {
44 ESP_LOGE(TAG,
"No data to parse");
48 JsonDocument json_document(&global_json_allocator);
50 JsonDocument json_document;
52 if (json_document.overflowed()) {
53 ESP_LOGE(TAG,
"Could not allocate memory for JSON document!");
56 DeserializationError err = deserializeJson(json_document, data,
len);
58 if (err == DeserializationError::Ok) {
60 }
else if (err == DeserializationError::NoMemory) {
61 ESP_LOGE(TAG,
"Can not allocate more memory for deserialization. Consider making source string smaller");
64 ESP_LOGE(TAG,
"Parse error: %s", err.c_str());
110 if (doc_.overflowed()) {
111 ESP_LOGE(TAG,
"JSON document overflow");
112 auto *buf = result.data_writable_();
120 size_t size = serializeJson(doc_, result.data_writable_(), buf_size);
121 if (
size < buf_size) {
123 result.set_size_(
size);
131 constexpr size_t max_heap_size = 5120;
132 size_t heap_size = buf_size * 2;
133 while (heap_size <= max_heap_size) {
134 result.reallocate_heap_(heap_size - 1);
135 size = serializeJson(doc_, result.data_writable_(), heap_size);
136 if (
size < heap_size) {
137 result.set_size_(
size);
143 ESP_LOGW(TAG,
"JSON payload too large, truncated to %zu bytes",
size);
144 result.set_size_(
size);
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.
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.