17 auto free_heap = ALLOCATOR.get_max_free_block_size();
18 size_t request_size = std::min(free_heap, (
size_t) 512);
20 ESP_LOGV(TAG,
"Attempting to allocate %zu bytes for JSON serialization", request_size);
21 DynamicJsonDocument json_document(request_size);
22 if (json_document.capacity() == 0) {
23 ESP_LOGE(TAG,
"Could not allocate memory for document! Requested %zu bytes, largest free heap block: %zu bytes",
24 request_size, free_heap);
27 JsonObject root = json_document.to<JsonObject>();
29 if (json_document.overflowed()) {
30 if (request_size == free_heap) {
31 ESP_LOGE(TAG,
"Could not allocate memory for document! Overflowed largest free heap block: %zu bytes",
35 request_size = std::min(request_size * 2, free_heap);
38 json_document.shrinkToFit();
39 ESP_LOGV(TAG,
"Size after shrink %zu bytes", json_document.capacity());
41 serializeJson(json_document, output);
51 auto free_heap = ALLOCATOR.get_max_free_block_size();
52 size_t request_size = std::min(free_heap, (
size_t) (data.size() * 1.5));
54 DynamicJsonDocument json_document(request_size);
55 if (json_document.capacity() == 0) {
56 ESP_LOGE(TAG,
"Could not allocate memory for document! Requested %zu bytes, free heap: %zu", request_size,
60 DeserializationError err = deserializeJson(json_document, data);
61 json_document.shrinkToFit();
63 JsonObject root = json_document.as<JsonObject>();
65 if (err == DeserializationError::Ok) {
67 }
else if (err == DeserializationError::NoMemory) {
68 if (request_size * 2 >= free_heap) {
69 ESP_LOGE(TAG,
"Can not allocate more memory for deserialization. Consider making source string smaller");
72 ESP_LOGV(TAG,
"Increasing memory allocation.");
76 ESP_LOGE(TAG,
"Parse error: %s", err.c_str());