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
impl Builder
sourcepub fn is_human_readable(&mut self, is_human_readable: bool) -> &mut Self
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();sourcepub fn serialize_struct_as(
&mut self,
serialize_struct_as: SerializeStructAs
) -> &mut Self
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,
]
);sourcepub fn build(&mut self) -> Serializer
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();