mirror of
https://github.com/esphome/esphome.git
synced 2026-03-02 10:48:22 -07:00
no dummy
This commit is contained in:
@@ -71,10 +71,22 @@ SerializationBuffer<> JsonBuilder::serialize() {
|
||||
buf[2] = '\0';
|
||||
return result;
|
||||
}
|
||||
size_t size = measureJson(doc_);
|
||||
SerializationBuffer<> result(size);
|
||||
serializeJson(doc_, result.data_writable(), size + 1);
|
||||
return result;
|
||||
// Intentionally avoid measureJson() - it instantiates DummyWriter templates that add ~700 bytes
|
||||
// of flash. Instead, try serializing to stack buffer first. 768 bytes covers typical JSON payloads
|
||||
// (sensors ~200B, lights ~170B, climate ~700B). Only entities with many options exceed this.
|
||||
// serializeJson() returns actual size needed even if truncated, so we can retry with heap if needed.
|
||||
constexpr size_t buf_size = SerializationBuffer<>::BUFFER_SIZE;
|
||||
SerializationBuffer<> result(buf_size - 1); // Max content size (reserve 1 for null)
|
||||
size_t size = serializeJson(doc_, result.data_writable(), buf_size);
|
||||
if (size < buf_size) {
|
||||
// Fits in stack buffer - update size to actual length
|
||||
result.set_size(size);
|
||||
return result;
|
||||
}
|
||||
// Needs heap allocation - serialize again with exact size
|
||||
SerializationBuffer<> heap_result(size);
|
||||
serializeJson(doc_, heap_result.data_writable(), size + 1);
|
||||
return heap_result;
|
||||
}
|
||||
|
||||
} // namespace json
|
||||
|
||||
Reference in New Issue
Block a user