pub struct Encoder<W: Write, const VALIDATE: bool = true> { /* private fields */ }Expand description
JSON encoder with internal buffering and indentation state.
All bytes are buffered in an internal Vec<u8> and flushed to W once a
threshold is crossed, keeping memory bounded.
The VALIDATE const parameter selects the event-protocol policy:
Encoder<W>(the default,VALIDATE = true) checks every call against the serialization event protocol and reports misuse as an error. This is the safe surface for hand-writtenNsonSerializeimplementations.Encoder<W, false>trusts the caller to follow the protocol (the repository-owned derive macros are verified to do so) and skips every per-value check. The top-levelnextencode/to_vec/to_stringentry points use this fast path.
Both emit byte-identical output for protocol-conforming callers; the fast path trades misuse diagnostics for ~2x encoding throughput.
Implementations§
Source§impl<W: Write, const VALIDATE: bool> Encoder<W, VALIDATE>
impl<W: Write, const VALIDATE: bool> Encoder<W, VALIDATE>
Sourcepub fn with_config(writer: W, config: EncodeConfig) -> Self
pub fn with_config(writer: W, config: EncodeConfig) -> Self
Create an encoder with the given config.
Sourcepub fn begin_object(&mut self) -> Result<()>
pub fn begin_object(&mut self) -> Result<()>
Open an object: write {.
Sourcepub fn end_object(&mut self) -> Result<()>
pub fn end_object(&mut self) -> Result<()>
Close an object: write }.
Sourcepub fn begin_array(&mut self) -> Result<()>
pub fn begin_array(&mut self) -> Result<()>
Open an array: write [.
Sourcepub fn separator(&mut self) -> Result<()>
pub fn separator(&mut self) -> Result<()>
Write an element / key separator.
The first entry of a container produces nothing; subsequent entries
produce , (plus newline and indent in pretty mode).
Sourcepub fn write_null(&mut self) -> Result<()>
pub fn write_null(&mut self) -> Result<()>
Write null.
Sourcepub fn write_bool(&mut self, v: bool) -> Result<()>
pub fn write_bool(&mut self, v: bool) -> Result<()>
Write a boolean.
Sourcepub fn write_char(&mut self, c: char) -> Result<()>
pub fn write_char(&mut self, c: char) -> Result<()>
Write a character (a one-scalar string) on the hot path.
Implemented directly instead of routing through
Encoder::write_str so a single character skips
the full-string raw-copy scan.
Sourcepub fn write_number(&mut self, n: &Number) -> Result<()>
pub fn write_number(&mut self, n: &Number) -> Result<()>
Write a number.
Sourcepub fn write_i128(&mut self, v: i128) -> Result<()>
pub fn write_i128(&mut self, v: i128) -> Result<()>
Write an i128.
Sourcepub fn write_u128(&mut self, v: u128) -> Result<()>
pub fn write_u128(&mut self, v: u128) -> Result<()>
Write a u128.
Trait Implementations§
Source§impl<W: Write, const VALIDATE: bool> FormatEncoder for Encoder<W, VALIDATE>
impl<W: Write, const VALIDATE: bool> FormatEncoder for Encoder<W, VALIDATE>
Source§fn separator(&mut self) -> Result<(), Self::Error>
fn separator(&mut self) -> Result<(), Self::Error>
Source§fn write_char(&mut self, value: char) -> Result<(), Self::Error>
fn write_char(&mut self, value: char) -> Result<(), Self::Error>
Source§fn write_number(&mut self, value: &Number) -> Result<(), Self::Error>
fn write_number(&mut self, value: &Number) -> Result<(), Self::Error>
Source§fn write_f64(&mut self, value: f64) -> Result<(), Self::Error>
fn write_f64(&mut self, value: f64) -> Result<(), Self::Error>
f64 (shortest round-trip).Source§fn write_f32(&mut self, value: f32) -> Result<(), Self::Error>
fn write_f32(&mut self, value: f32) -> Result<(), Self::Error>
f32 (shortest round-trip).Source§fn write_i16(&mut self, value: i16) -> Result<(), Self::Error>
fn write_i16(&mut self, value: i16) -> Result<(), Self::Error>
i16 (default: widen to write_i64).Source§fn write_i32(&mut self, value: i32) -> Result<(), Self::Error>
fn write_i32(&mut self, value: i32) -> Result<(), Self::Error>
i32 (default: widen to write_i64).Source§fn write_u8(&mut self, value: u8) -> Result<(), Self::Error>
fn write_u8(&mut self, value: u8) -> Result<(), Self::Error>
u8 (default: widen to write_u64).Source§fn write_u16(&mut self, value: u16) -> Result<(), Self::Error>
fn write_u16(&mut self, value: u16) -> Result<(), Self::Error>
u16 (default: widen to write_u64).Source§fn write_u32(&mut self, value: u32) -> Result<(), Self::Error>
fn write_u32(&mut self, value: u32) -> Result<(), Self::Error>
u32 (default: widen to write_u64).Source§fn write_bytes(&mut self, value: &[u8]) -> Result<(), Self::Error>
fn write_bytes(&mut self, value: &[u8]) -> Result<(), Self::Error>
Source§fn write_some(&mut self) -> Result<(), Self::Error>
fn write_some(&mut self) -> Result<(), Self::Error>
Option::Some. Read more