Streaming encoder for TOON format.
This module provides a way to encode large data structures incrementally, yielding iodata chunks that can be sent over the network without materializing the entire output in memory.
Usage
# For Plug/Phoenix chunked responses:
conn
|> put_resp_content_type("application/toon")
|> send_chunked(200, fn chunk ->
ToonEx.Encode.Stream.encode_stream(large_dataset, opts)
|> Enum.each(&chunk.(&1))
end)
# Or as a simple stream:
stream = ToonEx.Encode.Stream.encode_stream(data, opts)
|> Stream.map(&IO.iodata_to_binary/1)
|> Stream.each(&IO.write/1)
|> Stream.run()Performance
The streaming encoder avoids allocating the full output binary upfront. It yields iodata chunks that are converted to binaries only when consumed. Memory usage stays constant regardless of output size.
Summary
Functions
Encodes an Elixir term to a stream of TOON iodata chunks.
Types
Functions
@spec encode_stream(ToonEx.Types.input(), opts()) :: Enumerable.t()
Encodes an Elixir term to a stream of TOON iodata chunks.
Returns an Enumerable.t() that yields iodata chunks. Each chunk is a valid
TOON fragment that can be concatenated to form the complete output.
Options
Same as ToonEx.encode/2:
:indent- Number of spaces for indentation (default: 2):delimiter- Delimiter for array values: "," | " " | "|" (default: ","):length_marker- Prefix for array length marker (default: nil):key_folding- Key folding mode::off|:safe(default::off):flatten_depth- Max depth for key folding (default::infinity)
Example
stream = ToonEx.Encode.Stream.encode_stream(%{"users" => large_list}, indent: 4)
# Returns an Enumerable that yields iodata chunks