FormatSerializer

Trait FormatSerializer 

Source
pub trait FormatSerializer {
    type Error: Debug;

    // Required methods
    fn begin_struct(&mut self) -> Result<(), Self::Error>;
    fn field_key(&mut self, key: &str) -> Result<(), Self::Error>;
    fn end_struct(&mut self) -> Result<(), Self::Error>;
    fn begin_seq(&mut self) -> Result<(), Self::Error>;
    fn end_seq(&mut self) -> Result<(), Self::Error>;
    fn scalar(&mut self, scalar: ScalarValue<'_>) -> Result<(), Self::Error>;

    // Provided methods
    fn field_metadata(&mut self, _field: &FieldItem) -> Result<(), Self::Error> { ... }
    fn struct_metadata(&mut self, _shape: &Shape) -> Result<(), Self::Error> { ... }
    fn preferred_field_order(&self) -> FieldOrdering { ... }
    fn raw_serialize_shape(&self) -> Option<&'static Shape> { ... }
    fn raw_scalar(&mut self, content: &str) -> Result<(), Self::Error> { ... }
}
Expand description

Low-level serializer interface implemented by each format backend.

This is intentionally event-ish: the shared serializer logic owns traversal (struct/enum/seq decisions), while formats own representation details.

Required Associated Types§

Source

type Error: Debug

Format-specific error type.

Required Methods§

Source

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

Begin a map/object/struct.

Source

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

Emit a field key within a struct.

Source

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

End a map/object/struct.

Source

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

Begin a sequence/array.

Source

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

End a sequence/array.

Source

fn scalar(&mut self, scalar: ScalarValue<'_>) -> Result<(), Self::Error>

Emit a scalar value.

Provided Methods§

Source

fn field_metadata(&mut self, _field: &FieldItem) -> Result<(), Self::Error>

Optional: Provide field metadata before field_key is called. This allows formats like XML to extract namespace information. Default implementation does nothing.

Source

fn struct_metadata(&mut self, _shape: &Shape) -> Result<(), Self::Error>

Optional: Provide struct/enum type metadata when beginning to serialize it. This allows formats to extract container-level attributes like xml::ns_all. Default implementation does nothing.

Source

fn preferred_field_order(&self) -> FieldOrdering

Preferred field ordering for this format. Formats like XML can request attributes-first ordering to avoid buffering. Default is declaration order.

Source

fn raw_serialize_shape(&self) -> Option<&'static Shape>

Returns the shape of the format’s raw capture type for serialization.

When serializing a value whose shape matches this, the serializer will extract the inner string and call FormatSerializer::raw_scalar instead of normal serialization.

Source

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

Emit a raw scalar value (for RawJson, etc.) without any encoding/escaping.

The content is the format-specific raw representation that should be output directly.

Implementors§