Struct serde_assert::ser::Builder

source ·
pub struct Builder { /* private fields */ }
Expand description

A builder for a Serializer.

Construction of a Serializer follows the builder pattern. Configuration options can be set on the Builder, and then the actual Serializer is constructed by calling build().

Example

use serde_assert::Serializer;

let serializer = Serializer::builder().is_human_readable(false).build();

Implementations§

source§

impl Builder

source

pub fn is_human_readable(&mut self, is_human_readable: bool) -> &mut Self

Determines whether the serializer will serialize values in a readable format or a compact format.

Useful for complicated structs wishing to provide different outputs depending on the readability of the serialization type.

If not set, the default value is true.

Example
use serde_assert::Serializer;

let serializer = Serializer::builder().is_human_readable(false).build();
source

pub fn serialize_struct_as( &mut self, serialize_struct_as: SerializeStructAs ) -> &mut Self

Specifies how the serializer should serialize structs.

Compact formats often serialize structs as sequences. By enabling this setting, tokens can be produced in this format, and can then be deserialized to ensure structs deserialized as sequences are deserialized correctly.

If not set, the default value is SerializeStructAs::Struct.

Example
use claims::assert_ok_eq;
use serde::Serialize;
use serde_assert::{
    ser::SerializeStructAs,
    Serializer,
    Token,
};

#[derive(Serialize)]
struct Struct {
    foo: bool,
    bar: u32,
}

let some_struct = Struct {
    foo: false,
    bar: 42,
};
let serializer = Serializer::builder()
    .serialize_struct_as(SerializeStructAs::Seq)
    .build();

assert_ok_eq!(
    some_struct.serialize(&serializer),
    [
        Token::Seq { len: Some(2) },
        Token::Bool(false),
        Token::U32(42),
        Token::SeqEnd,
    ]
);
source

pub fn build(&mut self) -> Serializer

Build a new Serializer using this Builder.

Constructs a new Serializer using the configuration options set on this Builder.

Example
use serde_assert::Serializer;

let serializer = Serializer::builder().is_human_readable(false).build();

Trait Implementations§

source§

impl Debug for Builder

source§

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

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

impl Default for Builder

source§

fn default() -> Self

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

Auto Trait Implementations§

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

§

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.