1
0
mirror of https://github.com/esphome/esphome.git synced 2025-09-23 13:42:27 +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() JsonBuilder::JsonBuilder()
: doc_( : doc_(
#ifdef USE_PSRAM #ifdef USE_PSRAM
(allocator_ = std::make_unique<SpiRamAllocator>(), allocator_.get()) [this]() {
auto *alloc = new SpiRamAllocator(); // NOLINT(cppcoreguidelines-owning-memory)
allocator_ = alloc;
return alloc;
}()
#else #else
nullptr nullptr
#endif #endif
) { ) {
} }
JsonBuilder::~JsonBuilder() {
#ifdef USE_PSRAM
delete static_cast<SpiRamAllocator *>(allocator_); // NOLINT(cppcoreguidelines-owning-memory)
#endif
}
std::string JsonBuilder::serialize() { std::string JsonBuilder::serialize() {
if (doc_.overflowed()) { if (doc_.overflowed()) {
ESP_LOGE(TAG, "JSON document overflow"); 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 { class JsonBuilder {
public: public:
JsonBuilder(); JsonBuilder();
~JsonBuilder();
JsonObject root() { JsonObject root() {
if (!root_created_) { if (!root_created_) {
@@ -41,12 +42,13 @@ class JsonBuilder {
std::string serialize(); std::string serialize();
private: private:
#ifdef USE_PSRAM
std::unique_ptr<ArduinoJson::Allocator> allocator_;
#endif
JsonDocument doc_; JsonDocument doc_;
JsonObject root_; JsonObject root_;
bool root_created_{false}; 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 } // namespace json