ESPHome 2025.10.3
Loading...
Searching...
No Matches
json_util.h
Go to the documentation of this file.
1#pragma once
2
3#include <vector>
4
7
8#define ARDUINOJSON_ENABLE_STD_STRING 1 // NOLINT
9
10#define ARDUINOJSON_USE_LONG_LONG 1 // NOLINT
11
12#include <ArduinoJson.h>
13
14namespace esphome {
15namespace json {
16
17#ifdef USE_PSRAM
18// Build an allocator for the JSON Library using the RAMAllocator class
19// This is only compiled when PSRAM is enabled
20struct SpiRamAllocator : ArduinoJson::Allocator {
21 void *allocate(size_t size) override { return allocator_.allocate(size); }
22
23 void deallocate(void *ptr) override {
24 // ArduinoJson's Allocator interface doesn't provide the size parameter in deallocate.
25 // RAMAllocator::deallocate() requires the size, which we don't have access to here.
26 // RAMAllocator::deallocate implementation just calls free() regardless of whether
27 // the memory was allocated with heap_caps_malloc or malloc.
28 // This is safe because ESP-IDF's heap implementation internally tracks the memory region
29 // and routes free() to the appropriate heap.
30 free(ptr); // NOLINT(cppcoreguidelines-owning-memory,cppcoreguidelines-no-malloc)
31 }
32
33 void *reallocate(void *ptr, size_t new_size) override {
34 return allocator_.reallocate(static_cast<uint8_t *>(ptr), new_size);
35 }
36
37 protected:
39};
40#endif
41
43using json_parse_t = std::function<bool(JsonObject)>;
44
46using json_build_t = std::function<void(JsonObject)>;
47
49std::string build_json(const json_build_t &f);
50
52bool parse_json(const std::string &data, const json_parse_t &f);
53
55JsonDocument parse_json(const uint8_t *data, size_t len);
57inline JsonDocument parse_json(const std::string &data) {
58 return parse_json(reinterpret_cast<const uint8_t *>(data.c_str()), data.size());
59}
60
63 public:
64 JsonObject root() {
65 if (!root_created_) {
66 root_ = doc_.to<JsonObject>();
67 root_created_ = true;
68 }
69 return root_;
70 }
71
72 std::string serialize();
73
74 private:
75#ifdef USE_PSRAM
76 SpiRamAllocator allocator_;
77 JsonDocument doc_{&allocator_};
78#else
79 JsonDocument doc_;
80#endif
81 JsonObject root_;
82 bool root_created_{false};
83};
84
85} // namespace json
86} // namespace esphome
An STL allocator that uses SPI or internal RAM.
Definition helpers.h:874
T * reallocate(T *p, size_t n)
Definition helpers.h:913
T * allocate(size_t n)
Definition helpers.h:894
Builder class for creating JSON documents without lambdas.
Definition json_util.h:62
std::function< void(JsonObject)> json_build_t
Callback function typedef for building JsonObjects.
Definition json_util.h:46
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:27
std::string build_json(const json_build_t &f)
Build a JSON string with the provided json build function.
Definition json_util.cpp:18
std::function< bool(JsonObject)> json_parse_t
Callback function typedef for parsing JsonObjects.
Definition json_util.h:43
Providing packet encoding functions for exchanging data with a remote host.
Definition a01nyub.cpp:7
std::string size_t len
Definition helpers.h:304
void deallocate(void *ptr) override
Definition json_util.h:23
void * allocate(size_t size) override
Definition json_util.h:21
RAMAllocator< uint8_t > allocator_
Definition json_util.h:38
void * reallocate(void *ptr, size_t new_size) override
Definition json_util.h:33