1
0
mirror of https://github.com/esphome/esphome.git synced 2025-09-23 05:32:22 +01:00

Merge branch 'integration' into memory_api

This commit is contained in:
J. Nick Koston
2025-09-15 21:20:26 -05:00
2 changed files with 16 additions and 4 deletions

View File

@@ -73,13 +73,23 @@ bool parse_json(const std::string &data, const json_parse_t &f) {
JsonBuilder::JsonBuilder()
: doc_(
#ifdef USE_PSRAM
(allocator_ = std::make_unique<SpiRamAllocator>(), allocator_.get())
[this]() {
auto *alloc = new SpiRamAllocator(); // NOLINT(cppcoreguidelines-owning-memory)
allocator_ = alloc;
return alloc;
}()
#else
nullptr
#endif
) {
}
JsonBuilder::~JsonBuilder() {
#ifdef USE_PSRAM
delete static_cast<SpiRamAllocator *>(allocator_); // NOLINT(cppcoreguidelines-owning-memory)
#endif
}
std::string JsonBuilder::serialize() {
if (doc_.overflowed()) {
ESP_LOGE(TAG, "JSON document overflow");

View File

@@ -29,6 +29,7 @@ bool parse_json(const std::string &data, const json_parse_t &f);
class JsonBuilder {
public:
JsonBuilder();
~JsonBuilder();
JsonObject root() {
if (!root_created_) {
@@ -41,12 +42,13 @@ class JsonBuilder {
std::string serialize();
private:
#ifdef USE_PSRAM
std::unique_ptr<ArduinoJson::Allocator> allocator_;
#endif
JsonDocument doc_;
JsonObject root_;
bool root_created_{false};
// Allocator must be last member to ensure it's destroyed after doc_
#ifdef USE_PSRAM
void *allocator_{nullptr}; // Will store SpiRamAllocator*, managed in cpp file
#endif
};
} // namespace json