mirror of
https://github.com/esphome/esphome.git
synced 2025-08-03 00:47:47 +00:00
bump ArduinoJSON library to 7.4.1
This commit is contained in:
parent
e7d819a656
commit
c069a66625
@ -12,6 +12,6 @@ CONFIG_SCHEMA = cv.All(
|
|||||||
|
|
||||||
@coroutine_with_priority(1.0)
|
@coroutine_with_priority(1.0)
|
||||||
async def to_code(config):
|
async def to_code(config):
|
||||||
cg.add_library("bblanchon/ArduinoJson", "6.18.5")
|
cg.add_library("bblanchon/ArduinoJson", "7.4.1")
|
||||||
cg.add_define("USE_JSON")
|
cg.add_define("USE_JSON")
|
||||||
cg.add_global(json_ns.using)
|
cg.add_global(json_ns.using)
|
||||||
|
@ -6,30 +6,39 @@ namespace json {
|
|||||||
|
|
||||||
static const char *const TAG = "json";
|
static const char *const TAG = "json";
|
||||||
|
|
||||||
static std::vector<char> global_json_build_buffer; // NOLINT
|
static auto ALLOCATOR = RAMAllocator<uint8_t>(
|
||||||
static const auto ALLOCATOR = RAMAllocator<uint8_t>(RAMAllocator<uint8_t>::ALLOC_INTERNAL);
|
RAMAllocator<uint8_t>::NONE); // Attempt to allocate in PSRAM before falling back into internal
|
||||||
|
|
||||||
|
// Build an allocator for the JSON Library using the RAMAllocator class
|
||||||
|
struct SpiRamAllocator : ArduinoJson::Allocator {
|
||||||
|
void *allocate(size_t size) { return ALLOCATOR.allocate(size); }
|
||||||
|
|
||||||
|
void deallocate(void *pointer) {
|
||||||
|
free(pointer); // NOLINT(cppcoreguidelines-owning-memory,cppcoreguidelines-no-malloc)
|
||||||
|
}
|
||||||
|
|
||||||
|
void *reallocate(void *ptr, size_t new_size) { return ALLOCATOR.reallocate(static_cast<uint8_t *>(ptr), new_size); }
|
||||||
|
};
|
||||||
|
|
||||||
|
static auto DOC_ALLOCATOR = SpiRamAllocator();
|
||||||
|
|
||||||
std::string build_json(const json_build_t &f) {
|
std::string build_json(const json_build_t &f) {
|
||||||
// Here we are allocating up to 5kb of memory,
|
// Here we are allocating up to 5kb of memory,
|
||||||
// with the heap size minus 2kb to be safe if less than 5kb
|
// with the heap size minus 2kb to be safe if less than 5kb
|
||||||
// as we can not have a true dynamic sized document.
|
// as we can not have a true dynamic sized document.
|
||||||
// The excess memory is freed below with `shrinkToFit()`
|
// The excess memory is freed below with `shrinkToFit()`
|
||||||
auto free_heap = ALLOCATOR.get_max_free_block_size();
|
|
||||||
size_t request_size = std::min(free_heap, (size_t) 512);
|
|
||||||
while (true) {
|
while (true) {
|
||||||
ESP_LOGV(TAG, "Attempting to allocate %zu bytes for JSON serialization", request_size);
|
ESP_LOGV(TAG, "Attempting to allocate %zu bytes for JSON serialization", request_size);
|
||||||
DynamicJsonDocument json_document(request_size);
|
DynamicJsonDocument json_document(request_size);
|
||||||
if (json_document.capacity() == 0) {
|
if (json_document.capacity() == 0) {
|
||||||
ESP_LOGE(TAG, "Could not allocate memory for document! Requested %zu bytes, largest free heap block: %zu bytes",
|
ESP_LOGE(TAG, "Could not allocate memory for JSON document!");
|
||||||
request_size, free_heap);
|
|
||||||
return "{}";
|
return "{}";
|
||||||
}
|
}
|
||||||
JsonObject root = json_document.to<JsonObject>();
|
JsonObject root = json_document.to<JsonObject>();
|
||||||
f(root);
|
f(root);
|
||||||
if (json_document.overflowed()) {
|
if (json_document.overflowed()) {
|
||||||
if (request_size == free_heap) {
|
if (request_size == free_heap) {
|
||||||
ESP_LOGE(TAG, "Could not allocate memory for document! Overflowed largest free heap block: %zu bytes",
|
ESP_LOGE(TAG, "Could not allocate memory for JSON document!");
|
||||||
free_heap);
|
|
||||||
return "{}";
|
return "{}";
|
||||||
}
|
}
|
||||||
request_size = std::min(request_size * 2, free_heap);
|
request_size = std::min(request_size * 2, free_heap);
|
||||||
@ -48,30 +57,21 @@ bool parse_json(const std::string &data, const json_parse_t &f) {
|
|||||||
// with the heap size minus 2kb to be safe if less than that
|
// with the heap size minus 2kb to be safe if less than that
|
||||||
// as we can not have a true dynamic sized document.
|
// as we can not have a true dynamic sized document.
|
||||||
// The excess memory is freed below with `shrinkToFit()`
|
// The excess memory is freed below with `shrinkToFit()`
|
||||||
auto free_heap = ALLOCATOR.get_max_free_block_size();
|
|
||||||
size_t request_size = std::min(free_heap, (size_t) (data.size() * 1.5));
|
|
||||||
while (true) {
|
while (true) {
|
||||||
DynamicJsonDocument json_document(request_size);
|
DynamicJsonDocument json_document(request_size);
|
||||||
if (json_document.capacity() == 0) {
|
if (json_document.capacity() == 0) {
|
||||||
ESP_LOGE(TAG, "Could not allocate memory for document! Requested %zu bytes, free heap: %zu", request_size,
|
ESP_LOGE(TAG, "Could not allocate memory for JSON document!");
|
||||||
free_heap);
|
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
DeserializationError err = deserializeJson(json_document, data);
|
DeserializationError err = deserializeJson(json_document, data);
|
||||||
json_document.shrinkToFit();
|
|
||||||
|
|
||||||
JsonObject root = json_document.as<JsonObject>();
|
JsonObject root = json_document.as<JsonObject>();
|
||||||
|
|
||||||
if (err == DeserializationError::Ok) {
|
if (err == DeserializationError::Ok) {
|
||||||
return f(root);
|
return f(root);
|
||||||
} else if (err == DeserializationError::NoMemory) {
|
} else if (err == DeserializationError::NoMemory) {
|
||||||
if (request_size * 2 >= free_heap) {
|
ESP_LOGE(TAG, "Can not allocate more memory for deserialization. Consider making source string smaller");
|
||||||
ESP_LOGE(TAG, "Can not allocate more memory for deserialization. Consider making source string smaller");
|
return false;
|
||||||
return false;
|
|
||||||
}
|
|
||||||
ESP_LOGV(TAG, "Increasing memory allocation.");
|
|
||||||
request_size *= 2;
|
|
||||||
continue;
|
|
||||||
} else {
|
} else {
|
||||||
ESP_LOGE(TAG, "Parse error: %s", err.c_str());
|
ESP_LOGE(TAG, "Parse error: %s", err.c_str());
|
||||||
return false;
|
return false;
|
||||||
|
@ -35,7 +35,7 @@ build_flags =
|
|||||||
lib_deps =
|
lib_deps =
|
||||||
esphome/noise-c@0.1.10 ; api
|
esphome/noise-c@0.1.10 ; api
|
||||||
improv/Improv@1.2.4 ; improv_serial / esp32_improv
|
improv/Improv@1.2.4 ; improv_serial / esp32_improv
|
||||||
bblanchon/ArduinoJson@6.18.5 ; json
|
bblanchon/ArduinoJson@7.4.1 ; json
|
||||||
wjtje/qr-code-generator-library@1.7.0 ; qr_code
|
wjtje/qr-code-generator-library@1.7.0 ; qr_code
|
||||||
functionpointer/arduino-MLX90393@1.0.2 ; mlx90393
|
functionpointer/arduino-MLX90393@1.0.2 ; mlx90393
|
||||||
pavlodn/HaierProtocol@0.9.31 ; haier
|
pavlodn/HaierProtocol@0.9.31 ; haier
|
||||||
|
Loading…
x
Reference in New Issue
Block a user