Skip to main content

Module stream

Module stream 

Source
Expand description

Streaming JSON decoding from a std::io::Read source.

StreamDecoder pulls bytes from the reader on demand instead of buffering the complete input up front, which is what the top-level crate::from_reader helper needs for network sockets, pipes and other incremental sources. It implements the same format-neutral FormatDecoder contract as crate::Decoder, so every derived NsonDeserialize works against it unchanged.

Two trade-offs follow from streaming:

  • Owned strings - a streamed input cannot be borrowed for the lifetime of the decoded value, so string() / bytes() always return Cow::Owned. Types that require borrowing (&str, &[u8], nextjson::Bytes) cannot be decoded from a stream.
  • Retained buffer - to honour the save/restore backtracking contract used by untagged enums without an error channel in restore, the decoder keeps every byte it has read (including consumed prefixes). Memory therefore grows with the total input; the win is that decoding starts as soon as the first bytes arrive and does not wait for the whole payload. Applications that need constant-memory streaming for a single value should chunk at the protocol level instead.

Structsยง

StreamDecoder
A JSON decoder that incrementally pulls its input from R: Read.