ESPHome 2025.10.3
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#ifdef USE_PSRAM
12// Global allocator that outlives all JsonDocuments returned by parse_json()
13// This prevents dangling pointer issues when JsonDocuments are returned from functions
14// NOLINTNEXTLINE(cppcoreguidelines-avoid-non-const-global-variables) - Must be mutable for ArduinoJson::Allocator
15static SpiRamAllocator global_json_allocator;
16#endif
17
18std::string build_json(const json_build_t &f) {
19 // NOLINTBEGIN(clang-analyzer-cplusplus.NewDeleteLeaks) false positive with ArduinoJson
20 JsonBuilder builder;
21 JsonObject root = builder.root();
22 f(root);
23 return builder.serialize();
24 // NOLINTEND(clang-analyzer-cplusplus.NewDeleteLeaks)
25}
26
27bool parse_json(const std::string &data, const json_parse_t &f) {
28 // NOLINTBEGIN(clang-analyzer-cplusplus.NewDeleteLeaks) false positive with ArduinoJson
29 JsonDocument doc = parse_json(reinterpret_cast<const uint8_t *>(data.c_str()), data.size());
30 if (doc.overflowed() || doc.isNull())
31 return false;
32 return f(doc.as<JsonObject>());
33 // NOLINTEND(clang-analyzer-cplusplus.NewDeleteLeaks)
34}
35
36JsonDocument parse_json(const uint8_t *data, size_t len) {
37 // NOLINTBEGIN(clang-analyzer-cplusplus.NewDeleteLeaks) false positive with ArduinoJson
38 if (data == nullptr || len == 0) {
39 ESP_LOGE(TAG, "No data to parse");
40 return JsonObject(); // return unbound object
41 }
42#ifdef USE_PSRAM
43 JsonDocument json_document(&global_json_allocator);
44#else
45 JsonDocument json_document;
46#endif
47 if (json_document.overflowed()) {
48 ESP_LOGE(TAG, "Could not allocate memory for JSON document!");
49 return JsonObject(); // return unbound object
50 }
51 DeserializationError err = deserializeJson(json_document, data, len);
52
53 if (err == DeserializationError::Ok) {
54 return json_document;
55 } else if (err == DeserializationError::NoMemory) {
56 ESP_LOGE(TAG, "Can not allocate more memory for deserialization. Consider making source string smaller");
57 return JsonObject(); // return unbound object
58 }
59 ESP_LOGE(TAG, "Parse error: %s", err.c_str());
60 return JsonObject(); // return unbound object
61 // NOLINTEND(clang-analyzer-cplusplus.NewDeleteLeaks)
62}
63
65 if (doc_.overflowed()) {
66 ESP_LOGE(TAG, "JSON document overflow");
67 return "{}";
68 }
69 std::string output;
70 serializeJson(doc_, output);
71 return output;
72}
73
74} // namespace json
75} // namespace esphome
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