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