Skip to main content

Encoder

Struct Encoder 

Source
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-written NsonSerialize implementations.
  • 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-level nextencode / to_vec / to_string entry 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>

Source

pub fn new(writer: W) -> Self

Create an encoder with the default (compact) config.

Source

pub fn with_config(writer: W, config: EncodeConfig) -> Self

Create an encoder with the given config.

Source

pub fn finish(self) -> Result<W>

Flush the internal buffer and return the underlying writer.

Source

pub fn flush(&mut self) -> Result<()>

Flush the internal buffer without consuming self.

Source

pub fn begin_object(&mut self) -> Result<()>

Open an object: write {.

Source

pub fn end_object(&mut self) -> Result<()>

Close an object: write }.

Source

pub fn begin_array(&mut self) -> Result<()>

Open an array: write [.

Source

pub fn end_array(&mut self) -> Result<()>

Close an array: write ].

Source

pub fn key(&mut self, key: &str) -> Result<()>

Write an object key: separator + "key":.

Source

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).

Source

pub fn write_null(&mut self) -> Result<()>

Write null.

Source

pub fn write_bool(&mut self, v: bool) -> Result<()>

Write a boolean.

Source

pub fn write_str(&mut self, s: &str) -> Result<()>

Write a string (auto-escaped).

Source

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.

Source

pub fn write_number(&mut self, n: &Number) -> Result<()>

Write a number.

Source

pub fn write_i64(&mut self, v: i64) -> Result<()>

Write an i64.

Source

pub fn write_u64(&mut self, v: u64) -> Result<()>

Write a u64.

Source

pub fn write_i128(&mut self, v: i128) -> Result<()>

Write an i128.

Source

pub fn write_u128(&mut self, v: u128) -> Result<()>

Write a u128.

Source

pub fn write_f64(&mut self, v: f64) -> Result<()>

Write an f64 (shortest round-trip; non-finite values error).

Integral floats are written as 1.0 rather than 1 so float-ness survives round-trips.

Source

pub fn write_f32(&mut self, v: f32) -> Result<()>

Write an f32 using its shortest round-trip representation.

Trait Implementations§

Source§

impl<W: Write, const VALIDATE: bool> FormatEncoder for Encoder<W, VALIDATE>

Source§

type Error = Error

The error type produced by this format’s methods. Read more
Source§

fn begin_array(&mut self) -> Result<(), Self::Error>

Begin an array container.
Source§

fn separator(&mut self) -> Result<(), Self::Error>

Emit the separator preceding a subsequent array element. Read more
Source§

fn end_array(&mut self) -> Result<(), Self::Error>

End the current array container.
Source§

fn begin_object(&mut self) -> Result<(), Self::Error>

Begin an object container.
Source§

fn key(&mut self, key: &str) -> Result<(), Self::Error>

Emit an object key. Read more
Source§

fn end_object(&mut self) -> Result<(), Self::Error>

End the current object container.
Source§

fn write_null(&mut self) -> Result<(), Self::Error>

Emit null.
Source§

fn write_bool(&mut self, value: bool) -> Result<(), Self::Error>

Emit a boolean.
Source§

fn write_str(&mut self, value: &str) -> Result<(), Self::Error>

Emit a UTF-8 string.
Source§

fn write_char(&mut self, value: char) -> Result<(), Self::Error>

Emit a single character (a one-scalar string).
Source§

fn write_number(&mut self, value: &Number) -> Result<(), Self::Error>

Emit a number preserving its exact internal kind.
Source§

fn write_i64(&mut self, value: i64) -> Result<(), Self::Error>

Emit an i64.
Source§

fn write_u64(&mut self, value: u64) -> Result<(), Self::Error>

Emit a u64.
Source§

fn write_i128(&mut self, value: i128) -> Result<(), Self::Error>

Emit an i128.
Source§

fn write_u128(&mut self, value: u128) -> Result<(), Self::Error>

Emit a u128.
Source§

fn write_f64(&mut self, value: f64) -> Result<(), Self::Error>

Emit an f64 (shortest round-trip).
Source§

fn write_f32(&mut self, value: f32) -> Result<(), Self::Error>

Emit an f32 (shortest round-trip).
Source§

fn write_i8(&mut self, value: i8) -> Result<(), Self::Error>

Emit an i8 (default: widen to write_i64). Read more
Source§

fn write_i16(&mut self, value: i16) -> Result<(), Self::Error>

Emit an i16 (default: widen to write_i64).
Source§

fn write_i32(&mut self, value: i32) -> Result<(), Self::Error>

Emit an i32 (default: widen to write_i64).
Source§

fn write_u8(&mut self, value: u8) -> Result<(), Self::Error>

Emit a u8 (default: widen to write_u64).
Source§

fn write_u16(&mut self, value: u16) -> Result<(), Self::Error>

Emit a u16 (default: widen to write_u64).
Source§

fn write_u32(&mut self, value: u32) -> Result<(), Self::Error>

Emit a u32 (default: widen to write_u64).
Source§

fn write_none(&mut self) -> Result<(), Self::Error>

Emit Option::None. Read more
Source§

fn write_bytes(&mut self, value: &[u8]) -> Result<(), Self::Error>

Emit a byte sequence. Read more
Source§

fn write_some(&mut self) -> Result<(), Self::Error>

Emit the marker that the following value is Option::Some. Read more
Source§

fn map_key<K: NsonSerialize>(&mut self, key: &K) -> Result<(), Self::Error>

Emit a map key. Read more
Source§

fn is_human_readable(&self) -> bool

Whether this format produces human-readable output. Read more

Auto Trait Implementations§

§

impl<W, const VALIDATE: bool> Freeze for Encoder<W, VALIDATE>
where W: Freeze,

§

impl<W, const VALIDATE: bool> RefUnwindSafe for Encoder<W, VALIDATE>
where W: RefUnwindSafe,

§

impl<W, const VALIDATE: bool> Send for Encoder<W, VALIDATE>
where W: Send,

§

impl<W, const VALIDATE: bool> Sync for Encoder<W, VALIDATE>
where W: Sync,

§

impl<W, const VALIDATE: bool> Unpin for Encoder<W, VALIDATE>
where W: Unpin,

§

impl<W, const VALIDATE: bool> UnsafeUnpin for Encoder<W, VALIDATE>
where W: UnsafeUnpin,

§

impl<W, const VALIDATE: bool> UnwindSafe for Encoder<W, VALIDATE>
where W: UnwindSafe,

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.