mirror of
https://github.com/esphome/esphome.git
synced 2025-09-23 13:42:27 +01:00
cleanup
This commit is contained in:
@@ -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");
|
||||
|
@@ -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
|
||||
|
Reference in New Issue
Block a user