Skip to content

feat(stl): JsonStreamWriter — stream JSON without an N-sized buffer (#3588 follow-up) - #3604

Merged
zackees merged 1 commit into
masterfrom
feat/json-stream-writer-3588
Jul 5, 2026
Merged

zackees merged 1 commit into
masterfrom
feat/json-stream-writer-3588

Conversation

@zackees

@zackees zackees commented Jul 5, 2026 •

Copy link
Copy Markdown
Member

The #3588 root-cause fix (#3603, make_shared null-construction under OOM) stops the heap corruption, but building a large JSON-RPC response as an fl::json object and serializing it to an fl::string still allocates two buffers proportional to the whole response. A help listing of ~200 methods (or a big runSingleTest result) can still exhaust the heap on classic ESP32 with WiFi up.

JsonStreamWriter emits a JSON document incrementally to a byte sink through a fixed 128-byte scratch buffer and a fixed-depth nesting stack — no heap allocation of its own. An arbitrarily large document streams with a bounded, tiny footprint (peak memory = one small buffer, not the document). Handles commas, key/value alternation, string escaping, numbers/bools/null.

fl::JsonStreamWriter w(sink);
w.beginObject();
w.key("result"); w.beginArray();
for (m : methods) { w.beginObject(); w.member("name", m.name); ... w.endObject(); }
w.endArray();
w.endObject();  // dtor flushes

Host-tested: escaping, nesting, null, and a 1000-element array verified to stream through the fixed buffer with no single write exceeding kBufSize. Host 349/349, lint clean, esp32dev builds.

Integration path (follow-up): wire the RPC transport to expose a raw byte sink so help/large handlers stream directly instead of returning a full fl::json. This primitive is the mechanism; the RPC-protocol plumbing needs on-device verification and lands separately.

Refs #3588.

🤖 Generated with Claude Code

Summary by CodeRabbit

  • New Features

    • Added a streaming JSON writer for incremental output, including support for objects, arrays, keys, strings, numbers, booleans, and null values.
    • Added automatic buffering and flushing to help generate JSON with limited memory use.
  • Tests

    • Added coverage for JSON formatting, escaping, nested structures, and large outputs to verify consistent behavior.

…3588 follow-up)

The #3588 root-cause fix (make_shared null-construction under OOM) stops
the heap CORRUPTION, but building a large JSON-RPC response as an fl::json
object and then serializing it to an fl::string still allocates two
buffers proportional to the whole response. On classic ESP32 with WiFi up
(free heap ~30 KB) a `help` listing of ~200 methods, or a big
runSingleTest result, can still exhaust the heap during construction.

JsonStreamWriter emits a JSON document incrementally to a byte sink
(serial, socket, …) through a fixed 128-byte scratch buffer and a
fixed-depth nesting stack — it performs NO heap allocation of its own, so
an arbitrarily large document is produced with a bounded, tiny footprint
(peak memory = one small buffer, not the document). It handles commas,
object key/value alternation, JSON string escaping, and numbers/bools/null.

  fl::JsonStreamWriter w(sink);
  w.beginObject();
  w.key("result"); w.beginArray();
  for (m : methods) { w.beginObject(); w.member("name", m.name); ... w.endObject(); }
  w.endArray();
  w.endObject();   // dtor flushes

Host-tested (escaping, nesting, null, and a 1000-element array verified to
stream through the fixed buffer with no single write exceeding kBufSize).

Integration path (separate PR): wire the RPC transport to expose a raw
byte sink so handlers like `help` can stream their response directly
instead of returning a fully-built fl::json — the writer is the mechanism;
the RPC-protocol plumbing is a focused follow-up that needs on-device
protocol verification.

Co-Authored-By: Claude Fable 5 <[email protected]>
@coderabbitai

coderabbitai Bot commented Jul 5, 2026 •

Copy link
Copy Markdown

Review Change Stack

Caution

Review failed

The pull request is closed.

ℹ️ Recent review info
⚙️ Run configuration

Configuration used: Path: .coderabbit.yaml

Review profile: CHILL

Plan: Pro

Run ID: 6ab551ec-6c76-4161-ab95-4ffd6db2f0e6

📥 Commits

Reviewing files that changed from the base of the PR and between 4c6414e and 22adffc.

📒 Files selected for processing (4)
  • src/fl/stl/_build.cpp.hpp
  • src/fl/stl/json_stream_writer.cpp.hpp
  • src/fl/stl/json_stream_writer.h
  • tests/fl/stl/json_stream_writer.cpp

📝 Walkthrough

Walkthrough

Adds a new fl::JsonStreamWriter class providing fixed-memory, sink-based streaming JSON output with bounded nesting depth. Includes header declaration, implementation with buffering and escaping logic, unity build inclusion, and unit tests validating object/array emission, escaping, null handling, and bounded-memory behavior.

Changes

JSON Stream Writer

Layer / File(s) Summary
Public API contract
src/fl/stl/json_stream_writer.h
Declares fl::JsonStreamWriter with a Sink callback type, kMaxDepth/kBufSize constants, lifecycle methods (constructor/destructor, deleted copy ops), structure controls (begin/end object/array, key), value writers for strings/integers/booleans/null, member() conveniences, flush(), and private buffer/depth state.
Buffering, escaping, and lifecycle implementation
src/fl/stl/json_stream_writer.cpp.hpp
Implements constructor/destructor (flush-on-destroy), flush(), putc(), puts(), and putEscaped() for JSON string escaping of quotes, backslashes, control characters, and \u00XX sequences.
Structure and value emitters
src/fl/stl/json_stream_writer.cpp.hpp
Implements comma-insertion logic (prefixForValue/prefixForKey), beginObject/endObject, beginArray/endArray, key(), value() overloads for strings (via itoa64 for integers) and booleans, and valueNull().
Build wiring and unit tests
src/fl/stl/_build.cpp.hpp, tests/fl/stl/json_stream_writer.cpp
Adds the new implementation file to the unity build header and adds tests covering flat objects, nested arrays/objects, string escaping, null values, and a bounded-memory check ensuring single writes stay within kBufSize.

Estimated code review effort: 2 (Simple) | ~15 minutes

Sequence Diagram(s)

sequenceDiagram
    participant Caller
    participant JsonStreamWriter
    participant ScratchBuffer
    participant Sink

    Caller->>JsonStreamWriter: beginObject()
    JsonStreamWriter->>ScratchBuffer: putc('{')
    Caller->>JsonStreamWriter: key("name")
    JsonStreamWriter->>ScratchBuffer: putEscaped("name") + ':'
    Caller->>JsonStreamWriter: value("Alice")
    JsonStreamWriter->>ScratchBuffer: putEscaped("Alice")
    Caller->>JsonStreamWriter: value(42)
    JsonStreamWriter->>ScratchBuffer: itoa64 digits
    ScratchBuffer->>ScratchBuffer: buffer full?
    ScratchBuffer->>Sink: flush() writes buffered bytes
    Caller->>JsonStreamWriter: endObject()
    JsonStreamWriter->>ScratchBuffer: putc('}')
    Caller->>JsonStreamWriter: (destructor)
    JsonStreamWriter->>Sink: final flush()
Loading
✨ Finishing Touches
📝 Generate docstrings
  • Create stacked PR
  • Commit on current branch
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Commit unit tests in branch feat/json-stream-writer-3588

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands.

@zackees
zackees merged commit 612cd86 into master Jul 5, 2026
10 of 11 checks passed
@fastled-project-sync fastled-project-sync Bot moved this from Triage to Done in FastLED Tracker Jul 5, 2026
eelstretching pushed a commit to eelstretching/FastLED-pico that referenced this pull request Jul 11, 2026
…astLED#3588 follow-up) (FastLED#3604)

The FastLED#3588 root-cause fix (make_shared null-construction under OOM) stops
the heap CORRUPTION, but building a large JSON-RPC response as an fl::json
object and then serializing it to an fl::string still allocates two
buffers proportional to the whole response. On classic ESP32 with WiFi up
(free heap ~30 KB) a `help` listing of ~200 methods, or a big
runSingleTest result, can still exhaust the heap during construction.

JsonStreamWriter emits a JSON document incrementally to a byte sink
(serial, socket, …) through a fixed 128-byte scratch buffer and a
fixed-depth nesting stack — it performs NO heap allocation of its own, so
an arbitrarily large document is produced with a bounded, tiny footprint
(peak memory = one small buffer, not the document). It handles commas,
object key/value alternation, JSON string escaping, and numbers/bools/null.

  fl::JsonStreamWriter w(sink);
  w.beginObject();
  w.key("result"); w.beginArray();
  for (m : methods) { w.beginObject(); w.member("name", m.name); ... w.endObject(); }
  w.endArray();
  w.endObject();   // dtor flushes

Host-tested (escaping, nesting, null, and a 1000-element array verified to
stream through the fixed buffer with no single write exceeding kBufSize).

Integration path (separate PR): wire the RPC transport to expose a raw
byte sink so handlers like `help` can stream their response directly
instead of returning a fully-built fl::json — the writer is the mechanism;
the RPC-protocol plumbing is a focused follow-up that needs on-device
protocol verification.

Co-authored-by: Claude Fable 5 <[email protected]>
@zackees
zackees deleted the feat/json-stream-writer-3588 branch August 21, 2026 19:15
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

Status: Done

Development

Successfully merging this pull request may close these issues.

1 participant