mirror of
https://github.com/esphome/esphome.git
synced 2025-09-23 05:32:22 +01:00
preen
This commit is contained in:
@@ -31,6 +31,7 @@ struct SpiRamAllocator : ArduinoJson::Allocator {
|
||||
protected:
|
||||
RAMAllocator<uint8_t> allocator_{RAMAllocator<uint8_t>(RAMAllocator<uint8_t>::NONE)};
|
||||
};
|
||||
|
||||
#endif
|
||||
|
||||
std::string build_json(const json_build_t &f) {
|
||||
@@ -73,22 +74,14 @@ bool parse_json(const std::string &data, const json_parse_t &f) {
|
||||
JsonBuilder::JsonBuilder()
|
||||
: doc_(
|
||||
#ifdef USE_PSRAM
|
||||
[this]() {
|
||||
auto *alloc = new SpiRamAllocator(); // NOLINT(cppcoreguidelines-owning-memory)
|
||||
allocator_ = alloc;
|
||||
return alloc;
|
||||
}()
|
||||
(allocator_ = std::make_unique<SpiRamAllocator>(), allocator_.get())
|
||||
#else
|
||||
nullptr
|
||||
#endif
|
||||
) {
|
||||
}
|
||||
|
||||
JsonBuilder::~JsonBuilder() {
|
||||
#ifdef USE_PSRAM
|
||||
delete static_cast<SpiRamAllocator *>(allocator_); // NOLINT(cppcoreguidelines-owning-memory)
|
||||
#endif
|
||||
}
|
||||
JsonBuilder::~JsonBuilder() = default;
|
||||
|
||||
std::string JsonBuilder::serialize() {
|
||||
if (doc_.overflowed()) {
|
||||
|
@@ -25,6 +25,9 @@ std::string build_json(const json_build_t &f);
|
||||
/// Parse a JSON string and run the provided json parse function if it's valid.
|
||||
bool parse_json(const std::string &data, const json_parse_t &f);
|
||||
|
||||
// Forward declaration to avoid exposing implementation details
|
||||
struct SpiRamAllocator;
|
||||
|
||||
/// Builder class for creating JSON documents without lambdas
|
||||
class JsonBuilder {
|
||||
public:
|
||||
@@ -42,13 +45,12 @@ class JsonBuilder {
|
||||
std::string serialize();
|
||||
|
||||
private:
|
||||
#ifdef USE_PSRAM
|
||||
std::unique_ptr<SpiRamAllocator> allocator_; // One heap allocation, but keeps code clean
|
||||
#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