Struct serde_content::Serializer

source ·
pub struct Serializer<'a> { /* private fields */ }
Expand description

A structure for serialising Rust values into crate::Value.

Implementations§

source§

impl<'a> Serializer<'a>

source

pub const fn new() -> Self

Creates a serializer.

The serializer created doesn’t serialize in human-readable form. To serialize in human-readable form, call Serializer::human_readable on the resulting serializer.

Examples found in repository?
examples/point.rs (line 16)
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
fn main() -> serde_content::Result<()> {
    let point = Point { x: 1, y: 2 };

    // Convert the Point to the Value type.
    let serialized = Serializer::new().serialize(&point)?;

    // Pretty print the serialised Value.
    dbg!(&serialized);

    // Convert the Value back to a Point.
    let deserialized: Point = Deserializer::new(serialized).deserialize()?;

    // Pretty print the deserialised Point.
    dbg!(deserialized);

    Ok(())
}
source

pub const fn human_readable(self) -> Self

Make Serialize implementations serialize in human-readable form.

source

pub fn serialize<T>(self, value: T) -> Result<Value<'a>, Error>
where T: Serialize,

Convert a T into Value which is an enum that can represent any valid Rust data.

Examples found in repository?
examples/point.rs (line 16)
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
fn main() -> serde_content::Result<()> {
    let point = Point { x: 1, y: 2 };

    // Convert the Point to the Value type.
    let serialized = Serializer::new().serialize(&point)?;

    // Pretty print the serialised Value.
    dbg!(&serialized);

    // Convert the Value back to a Point.
    let deserialized: Point = Deserializer::new(serialized).deserialize()?;

    // Pretty print the deserialised Point.
    dbg!(deserialized);

    Ok(())
}

Trait Implementations§

source§

impl<'a> Clone for Serializer<'a>

source§

fn clone(&self) -> Serializer<'a>

Returns a copy of the value. Read more
1.0.0 · source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
source§

impl<'a> Debug for Serializer<'a>

source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
source§

impl<'a> Default for Serializer<'a>

source§

fn default() -> Serializer<'a>

Returns the “default value” for a type. Read more
source§

impl<'a> PartialEq for Serializer<'a>

source§

fn eq(&self, other: &Serializer<'a>) -> bool

Tests for self and other values to be equal, and is used by ==.
1.0.0 · source§

fn ne(&self, other: &Rhs) -> bool

Tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
source§

impl<'a> PartialOrd for Serializer<'a>

source§

fn partial_cmp(&self, other: &Serializer<'a>) -> Option<Ordering>

This method returns an ordering between self and other values if one exists. Read more
1.0.0 · source§

fn lt(&self, other: &Rhs) -> bool

Tests less than (for self and other) and is used by the < operator. Read more
1.0.0 · source§

fn le(&self, other: &Rhs) -> bool

Tests less than or equal to (for self and other) and is used by the <= operator. Read more
1.0.0 · source§

fn gt(&self, other: &Rhs) -> bool

Tests greater than (for self and other) and is used by the > operator. Read more
1.0.0 · source§

fn ge(&self, other: &Rhs) -> bool

Tests greater than or equal to (for self and other) and is used by the >= operator. Read more
source§

impl<'a> Serializer for Serializer<'a>

§

type Ok = Value<'a>

The output type produced by this Serializer during successful serialization. Most serializers that produce text or binary output should set Ok = () and serialize into an io::Write or buffer contained within the Serializer instance. Serializers that build in-memory data structures may be simplified by using Ok to propagate the data structure around.
§

type Error = Error

The error type when some error occurs during serialization.
§

type SerializeSeq = Seq<'a>

Type returned from serialize_seq for serializing the content of the sequence.
§

type SerializeTuple = Tuple<'a>

Type returned from serialize_tuple for serializing the content of the tuple.
§

type SerializeTupleStruct = Struct<'a>

Type returned from serialize_tuple_struct for serializing the content of the tuple struct.
§

type SerializeTupleVariant = Enum<'a>

Type returned from serialize_tuple_variant for serializing the content of the tuple variant.
§

type SerializeMap = Map<'a>

Type returned from serialize_map for serializing the content of the map.
§

type SerializeStruct = Struct<'a>

Type returned from serialize_struct for serializing the content of the struct.
§

type SerializeStructVariant = Enum<'a>

Type returned from serialize_struct_variant for serializing the content of the struct variant.
source§

fn serialize_bool(self, value: bool) -> Result<Self::Ok, Error>

Serialize a bool value. Read more
source§

fn serialize_i8(self, value: i8) -> Result<Self::Ok, Error>

Serialize an i8 value. Read more
source§

fn serialize_i16(self, value: i16) -> Result<Self::Ok, Error>

Serialize an i16 value. Read more
source§

fn serialize_i32(self, value: i32) -> Result<Self::Ok, Error>

Serialize an i32 value. Read more
source§

fn serialize_i64(self, value: i64) -> Result<Self::Ok, Error>

Serialize an i64 value. Read more
source§

fn serialize_i128(self, value: i128) -> Result<Self::Ok, Error>

Serialize an i128 value. Read more
source§

fn serialize_u8(self, value: u8) -> Result<Self::Ok, Error>

Serialize a u8 value. Read more
source§

fn serialize_u16(self, value: u16) -> Result<Self::Ok, Error>

Serialize a u16 value. Read more
source§

fn serialize_u32(self, value: u32) -> Result<Self::Ok, Error>

Serialize a u32 value. Read more
source§

fn serialize_u64(self, value: u64) -> Result<Self::Ok, Error>

Serialize a u64 value. Read more
source§

fn serialize_u128(self, value: u128) -> Result<Self::Ok, Error>

Serialize a u128 value. Read more
source§

fn serialize_f32(self, value: f32) -> Result<Self::Ok, Error>

Serialize an f32 value. Read more
source§

fn serialize_f64(self, value: f64) -> Result<Self::Ok, Error>

Serialize an f64 value. Read more
source§

fn serialize_char(self, value: char) -> Result<Self::Ok, Error>

Serialize a character. Read more
source§

fn serialize_str(self, value: &str) -> Result<Self::Ok, Error>

Serialize a &str. Read more
source§

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

Serialize a chunk of raw byte data. Read more
source§

fn serialize_unit(self) -> Result<Self::Ok, Error>

Serialize a () value. Read more
source§

fn serialize_unit_struct(self, name: &'static str) -> Result<Self::Ok, Error>

Serialize a unit struct like struct Unit or PhantomData<T>. Read more
source§

fn serialize_unit_variant( self, name: &'static str, variant_index: u32, variant: &'static str, ) -> Result<Self::Ok, Error>

Serialize a unit variant like E::A in enum E { A, B }. Read more
source§

fn serialize_newtype_struct<T>( self, name: &'static str, value: &T, ) -> Result<Self::Ok, Error>
where T: ?Sized + Serialize,

Serialize a newtype struct like struct Millimeters(u8). Read more
source§

fn serialize_newtype_variant<T>( self, name: &'static str, variant_index: u32, variant: &'static str, value: &T, ) -> Result<Self::Ok, Error>
where T: ?Sized + Serialize,

Serialize a newtype variant like E::N in enum E { N(u8) }. Read more
source§

fn serialize_none(self) -> Result<Self::Ok, Error>

Serialize a None value. Read more
source§

fn serialize_some<T>(self, value: &T) -> Result<Self::Ok, Error>
where T: ?Sized + Serialize,

Serialize a Some(T) value. Read more
source§

fn serialize_seq(self, len: Option<usize>) -> Result<Self::SerializeSeq, Error>

Begin to serialize a variably sized sequence. This call must be followed by zero or more calls to serialize_element, then a call to end. Read more
source§

fn serialize_tuple(self, len: usize) -> Result<Self::SerializeTuple, Error>

Begin to serialize a statically sized sequence whose length will be known at deserialization time without looking at the serialized data. This call must be followed by zero or more calls to serialize_element, then a call to end. Read more
source§

fn serialize_tuple_struct( self, name: &'static str, len: usize, ) -> Result<Self::SerializeTupleStruct, Error>

Begin to serialize a tuple struct like struct Rgb(u8, u8, u8). This call must be followed by zero or more calls to serialize_field, then a call to end. Read more
source§

fn serialize_tuple_variant( self, name: &'static str, variant_index: u32, variant: &'static str, len: usize, ) -> Result<Self::SerializeTupleVariant, Error>

Begin to serialize a tuple variant like E::T in enum E { T(u8, u8) }. This call must be followed by zero or more calls to serialize_field, then a call to end. Read more
source§

fn serialize_map(self, len: Option<usize>) -> Result<Self::SerializeMap, Error>

Begin to serialize a map. This call must be followed by zero or more calls to serialize_key and serialize_value, then a call to end. Read more
source§

fn serialize_struct( self, name: &'static str, len: usize, ) -> Result<Self::SerializeStruct, Error>

Begin to serialize a struct like struct Rgb { r: u8, g: u8, b: u8 }. This call must be followed by zero or more calls to serialize_field, then a call to end. Read more
source§

fn serialize_struct_variant( self, name: &'static str, variant_index: u32, variant: &'static str, len: usize, ) -> Result<Self::SerializeStructVariant, Error>

Begin to serialize a struct variant like E::S in enum E { S { r: u8, g: u8, b: u8 } }. This call must be followed by zero or more calls to serialize_field, then a call to end. Read more
source§

fn collect_str<T>(self, value: &T) -> Result<Self::Ok, Self::Error>
where T: ?Sized + Display,

Serialize a string produced by an implementation of Display. Read more
source§

fn is_human_readable(&self) -> bool

Determine whether Serialize implementations should serialize in human-readable form. Read more
source§

fn collect_seq<I>(self, iter: I) -> Result<Self::Ok, Self::Error>

Collect an iterator as a sequence. Read more
source§

fn collect_map<K, V, I>(self, iter: I) -> Result<Self::Ok, Self::Error>
where K: Serialize, V: Serialize, I: IntoIterator<Item = (K, V)>,

Collect an iterator as a map. Read more
source§

impl<'a> Copy for Serializer<'a>

source§

impl<'a> StructuralPartialEq for Serializer<'a>

Auto Trait Implementations§

§

impl<'a> Freeze for Serializer<'a>

§

impl<'a> RefUnwindSafe for Serializer<'a>

§

impl<'a> Send for Serializer<'a>

§

impl<'a> Sync for Serializer<'a>

§

impl<'a> Unpin for Serializer<'a>

§

impl<'a> UnwindSafe for Serializer<'a>

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> CloneToUninit for T
where T: Clone,

source§

default unsafe fn clone_to_uninit(&self, dst: *mut T)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dst. Read more
source§

impl<T> CloneToUninit for T
where T: Copy,

source§

unsafe fn clone_to_uninit(&self, dst: *mut T)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dst. 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> ToOwned for T
where T: Clone,

§

type Owned = T

The resulting type after obtaining ownership.
source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
source§

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

§

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>,

§

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.