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