ESPHome 2025.7.1
Loading...
Searching...
No Matches
json_util.cpp
Go to the documentation of this file.
1#include "json_util.h"
2#include "esphome/core/log.h"
3
4// ArduinoJson::Allocator is included via ArduinoJson.h in json_util.h
5
6namespace esphome {
7namespace json {
8
9static const char *const TAG = "json";
10
11// Build an allocator for the JSON Library using the RAMAllocator class
12struct SpiRamAllocator : ArduinoJson::Allocator {
13 void *allocate(size_t size) override { return this->allocator_.allocate(size); }
14
15 void deallocate(void *pointer) override {
16 // ArduinoJson's Allocator interface doesn't provide the size parameter in deallocate.
17 // RAMAllocator::deallocate() requires the size, which we don't have access to here.
18 // RAMAllocator::deallocate implementation just calls free() regardless of whether
19 // the memory was allocated with heap_caps_malloc or malloc.
20 // This is safe because ESP-IDF's heap implementation internally tracks the memory region
21 // and routes free() to the appropriate heap.
22 free(pointer); // NOLINT(cppcoreguidelines-owning-memory,cppcoreguidelines-no-malloc)
23 }
24
25 void *reallocate(void *ptr, size_t new_size) override {
26 return this->allocator_.reallocate(static_cast<uint8_t *>(ptr), new_size);
27 }
28
29 protected:
31};
32
33std::string build_json(const json_build_t &f) {
34 // NOLINTBEGIN(clang-analyzer-cplusplus.NewDeleteLeaks) false positive with ArduinoJson
35 auto doc_allocator = SpiRamAllocator();
36 JsonDocument json_document(&doc_allocator);
37 if (json_document.overflowed()) {
38 ESP_LOGE(TAG, "Could not allocate memory for JSON document!");
39 return "{}";
40 }
41 JsonObject root = json_document.to<JsonObject>();
42 f(root);
43 if (json_document.overflowed()) {
44 ESP_LOGE(TAG, "Could not allocate memory for JSON document!");
45 return "{}";
46 }
47 std::string output;
48 serializeJson(json_document, output);
49 return output;
50 // NOLINTEND(clang-analyzer-cplusplus.NewDeleteLeaks)
51}
52
53bool parse_json(const std::string &data, const json_parse_t &f) {
54 // NOLINTBEGIN(clang-analyzer-cplusplus.NewDeleteLeaks) false positive with ArduinoJson
55 auto doc_allocator = SpiRamAllocator();
56 JsonDocument json_document(&doc_allocator);
57 if (json_document.overflowed()) {
58 ESP_LOGE(TAG, "Could not allocate memory for JSON document!");
59 return false;
60 }
61 DeserializationError err = deserializeJson(json_document, data);
62
63 JsonObject root = json_document.as<JsonObject>();
64
65 if (err == DeserializationError::Ok) {
66 return f(root);
67 } else if (err == DeserializationError::NoMemory) {
68 ESP_LOGE(TAG, "Can not allocate more memory for deserialization. Consider making source string smaller");
69 return false;
70 }
71 ESP_LOGE(TAG, "Parse error: %s", err.c_str());
72 return false;
73 // NOLINTEND(clang-analyzer-cplusplus.NewDeleteLeaks)
74}
75
76} // namespace json
77} // namespace esphome
An STL allocator that uses SPI or internal RAM.
Definition helpers.h:761
T * reallocate(T *p, size_t n)
Definition helpers.h:800
T * allocate(size_t n)
Definition helpers.h:781
std::function< void(JsonObject)> json_build_t
Callback function typedef for building JsonObjects.
Definition json_util.h:20
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.
Definition json_util.cpp:53
std::string build_json(const json_build_t &f)
Build a JSON string with the provided json build function.
Definition json_util.cpp:33
std::function< bool(JsonObject)> json_parse_t
Callback function typedef for parsing JsonObjects.
Definition json_util.h:17
Providing packet encoding functions for exchanging data with a remote host.
Definition a01nyub.cpp:7