mirror of
https://github.com/esphome/esphome.git
synced 2025-09-17 18:52:19 +01:00
[json] Only compile SpiRamAllocator when PSRAM is enabled
This commit is contained in:
@@ -8,7 +8,9 @@ namespace json {
|
|||||||
|
|
||||||
static const char *const TAG = "json";
|
static const char *const TAG = "json";
|
||||||
|
|
||||||
|
#ifdef USE_PSRAM
|
||||||
// Build an allocator for the JSON Library using the RAMAllocator class
|
// Build an allocator for the JSON Library using the RAMAllocator class
|
||||||
|
// This is only compiled when PSRAM is enabled
|
||||||
struct SpiRamAllocator : ArduinoJson::Allocator {
|
struct SpiRamAllocator : ArduinoJson::Allocator {
|
||||||
void *allocate(size_t size) override { return this->allocator_.allocate(size); }
|
void *allocate(size_t size) override { return this->allocator_.allocate(size); }
|
||||||
|
|
||||||
@@ -29,11 +31,16 @@ struct SpiRamAllocator : ArduinoJson::Allocator {
|
|||||||
protected:
|
protected:
|
||||||
RAMAllocator<uint8_t> allocator_{RAMAllocator<uint8_t>(RAMAllocator<uint8_t>::NONE)};
|
RAMAllocator<uint8_t> allocator_{RAMAllocator<uint8_t>(RAMAllocator<uint8_t>::NONE)};
|
||||||
};
|
};
|
||||||
|
#endif
|
||||||
|
|
||||||
std::string build_json(const json_build_t &f) {
|
std::string build_json(const json_build_t &f) {
|
||||||
// NOLINTBEGIN(clang-analyzer-cplusplus.NewDeleteLeaks) false positive with ArduinoJson
|
// NOLINTBEGIN(clang-analyzer-cplusplus.NewDeleteLeaks) false positive with ArduinoJson
|
||||||
|
#ifdef USE_PSRAM
|
||||||
auto doc_allocator = SpiRamAllocator();
|
auto doc_allocator = SpiRamAllocator();
|
||||||
JsonDocument json_document(&doc_allocator);
|
JsonDocument json_document(&doc_allocator);
|
||||||
|
#else
|
||||||
|
JsonDocument json_document;
|
||||||
|
#endif
|
||||||
if (json_document.overflowed()) {
|
if (json_document.overflowed()) {
|
||||||
ESP_LOGE(TAG, "Could not allocate memory for JSON document!");
|
ESP_LOGE(TAG, "Could not allocate memory for JSON document!");
|
||||||
return "{}";
|
return "{}";
|
||||||
@@ -52,8 +59,12 @@ std::string build_json(const json_build_t &f) {
|
|||||||
|
|
||||||
bool parse_json(const std::string &data, const json_parse_t &f) {
|
bool parse_json(const std::string &data, const json_parse_t &f) {
|
||||||
// NOLINTBEGIN(clang-analyzer-cplusplus.NewDeleteLeaks) false positive with ArduinoJson
|
// NOLINTBEGIN(clang-analyzer-cplusplus.NewDeleteLeaks) false positive with ArduinoJson
|
||||||
|
#ifdef USE_PSRAM
|
||||||
auto doc_allocator = SpiRamAllocator();
|
auto doc_allocator = SpiRamAllocator();
|
||||||
JsonDocument json_document(&doc_allocator);
|
JsonDocument json_document(&doc_allocator);
|
||||||
|
#else
|
||||||
|
JsonDocument json_document;
|
||||||
|
#endif
|
||||||
if (json_document.overflowed()) {
|
if (json_document.overflowed()) {
|
||||||
ESP_LOGE(TAG, "Could not allocate memory for JSON document!");
|
ESP_LOGE(TAG, "Could not allocate memory for JSON document!");
|
||||||
return false;
|
return false;
|
||||||
|
Reference in New Issue
Block a user