mirror of
https://github.com/esphome/esphome.git
synced 2025-09-23 21:52:23 +01:00
preen
This commit is contained in:
@@ -31,6 +31,7 @@ struct SpiRamAllocator : ArduinoJson::Allocator {
|
|||||||
protected:
|
protected:
|
||||||
RAMAllocator<uint8_t> allocator_{RAMAllocator<uint8_t>(RAMAllocator<uint8_t>::NONE)};
|
RAMAllocator<uint8_t> allocator_{RAMAllocator<uint8_t>(RAMAllocator<uint8_t>::NONE)};
|
||||||
};
|
};
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
std::string build_json(const json_build_t &f) {
|
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()
|
JsonBuilder::JsonBuilder()
|
||||||
: doc_(
|
: doc_(
|
||||||
#ifdef USE_PSRAM
|
#ifdef USE_PSRAM
|
||||||
[this]() {
|
(allocator_ = std::make_unique<SpiRamAllocator>(), allocator_.get())
|
||||||
auto *alloc = new SpiRamAllocator(); // NOLINT(cppcoreguidelines-owning-memory)
|
|
||||||
allocator_ = alloc;
|
|
||||||
return alloc;
|
|
||||||
}()
|
|
||||||
#else
|
#else
|
||||||
nullptr
|
nullptr
|
||||||
#endif
|
#endif
|
||||||
) {
|
) {
|
||||||
}
|
}
|
||||||
|
|
||||||
JsonBuilder::~JsonBuilder() {
|
JsonBuilder::~JsonBuilder() = default;
|
||||||
#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()) {
|
||||||
|
@@ -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.
|
/// 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);
|
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
|
/// Builder class for creating JSON documents without lambdas
|
||||||
class JsonBuilder {
|
class JsonBuilder {
|
||||||
public:
|
public:
|
||||||
@@ -42,13 +45,12 @@ class JsonBuilder {
|
|||||||
std::string serialize();
|
std::string serialize();
|
||||||
|
|
||||||
private:
|
private:
|
||||||
|
#ifdef USE_PSRAM
|
||||||
|
std::unique_ptr<SpiRamAllocator> allocator_; // One heap allocation, but keeps code clean
|
||||||
|
#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
|
||||||
|
Reference in New Issue
Block a user