Skip to main content

FormatEncoder

Trait FormatEncoder 

Source
pub trait FormatEncoder {
    type Error: FormatError;

Show 28 methods // Required methods fn begin_array(&mut self) -> Result<(), Self::Error>; fn separator(&mut self) -> Result<(), Self::Error>; fn end_array(&mut self) -> Result<(), Self::Error>; fn begin_object(&mut self) -> Result<(), Self::Error>; fn key(&mut self, key: &str) -> Result<(), Self::Error>; fn end_object(&mut self) -> Result<(), Self::Error>; fn write_null(&mut self) -> Result<(), Self::Error>; fn write_bool(&mut self, value: bool) -> Result<(), Self::Error>; fn write_str(&mut self, value: &str) -> Result<(), Self::Error>; fn write_char(&mut self, value: char) -> Result<(), Self::Error>; fn write_number(&mut self, value: &Number) -> Result<(), Self::Error>; fn write_i64(&mut self, value: i64) -> Result<(), Self::Error>; fn write_u64(&mut self, value: u64) -> Result<(), Self::Error>; fn write_i128(&mut self, value: i128) -> Result<(), Self::Error>; fn write_u128(&mut self, value: u128) -> Result<(), Self::Error>; fn write_f64(&mut self, value: f64) -> Result<(), Self::Error>; fn write_f32(&mut self, value: f32) -> Result<(), Self::Error>; // Provided methods fn write_i8(&mut self, value: i8) -> Result<(), Self::Error> { ... } fn write_i16(&mut self, value: i16) -> Result<(), Self::Error> { ... } fn write_i32(&mut self, value: i32) -> Result<(), Self::Error> { ... } fn write_u8(&mut self, value: u8) -> Result<(), Self::Error> { ... } fn write_u16(&mut self, value: u16) -> Result<(), Self::Error> { ... } fn write_u32(&mut self, value: u32) -> Result<(), Self::Error> { ... } fn write_bytes(&mut self, value: &[u8]) -> Result<(), Self::Error> { ... } fn write_none(&mut self) -> Result<(), Self::Error> { ... } fn write_some(&mut self) -> Result<(), Self::Error> { ... } fn map_key<K: NsonSerialize>(&mut self, key: &K) -> Result<(), Self::Error> { ... } fn is_human_readable(&self) -> bool { ... }
}
Expand description

Format-neutral emission contract implemented by every destination codec.

NsonSerialize::nextencode is generic over this trait, so one type implementation can target every codec whose data model represents the emitted values. Binary codecs can use separator and key as counting signals and patch length prefixes when a container closes. Some document-oriented codecs collect the event stream before emission.

Required Associated Types§

Source

type Error: FormatError

The error type produced by this format’s methods.

External codecs may use their own error type; it only needs to wrap crate::error::Error so generic serialization code can propagate format failures. The built-in formats all use crate::error::Error.

Required Methods§

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.

Called once per element, including the first, so binary codecs can use it as an element counter.

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.

Called once per entry, including the first, so binary codecs can use it as an entry counter.

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

Provided Methods§

Source

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

Emit an i8 (default: widen to write_i64).

Binary codecs that preserve source width on the wire override this.

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_bytes(&mut self, value: &[u8]) -> Result<(), Self::Error>

Emit a byte sequence.

The default emits a sequence of u8 values, which is lossless in every self-describing format (JSON text becomes [1, 2, 3], matching serde_json). Binary codecs override this to emit a native byte-string wire type (length prefix + raw bytes), which is far more compact.

Source

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

Emit Option::None.

The default maps to write_null, which is exactly the JSON shape. Binary codecs override this to emit a distinguishing tag so None stays distinct from a Some payload.

Source

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

Emit the marker that the following value is Option::Some.

The default emits nothing (the payload follows immediately), which is the JSON shape. Binary codecs override this to emit a distinguishing tag.

Source

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

Emit a map key.

The default serializes the key to a string and emits it with key (the JSON shape). Binary codecs override this to write the key as a plain value, which supports non-string keys such as BTreeMap<u8, V> without string round-tripping.

Source

fn is_human_readable(&self) -> bool

Whether this format produces human-readable output.

Text formats return true; binary codecs return false. Types that encode differently for humans (timestamps, identifiers, byte strings) branch on this, mirroring serde’s Serializer::is_human_readable.

Dyn Compatibility§

This trait is not dyn compatible.

In older versions of Rust, dyn compatibility was called "object safety".

Implementors§