Struct quick_xml::se::Serializer[][src]

pub struct Serializer<'r, W: Write> { /* fields omitted */ }

A Serializer

Implementations

impl<'r, W: Write> Serializer<'r, W>[src]

pub fn new(writer: W) -> Self[src]

Creates a new Serializer that uses struct name as a root tag name.

Note, that attempt to serialize a non-struct (including unit structs and newtype structs) will end up to an error. Use with_root to create serializer with explicitly defined root element name

pub fn with_root(writer: Writer<W>, root_tag: Option<&'r str>) -> Self[src]

Creates a new Serializer that uses specified root tag name

Examples

When serializing a primitive type, only its representation will be written:

use quick_xml::Writer;

let mut buffer = Vec::new();
let mut writer = Writer::new_with_indent(&mut buffer, b' ', 2);
let mut ser = Serializer::with_root(writer, Some("root"));

"node".serialize(&mut ser).unwrap();
assert_eq!(String::from_utf8(buffer).unwrap(), "node");

When serializing a struct, newtype struct, unit struct or tuple root_tag is used as tag name of root(s) element(s):

use quick_xml::Writer;
use quick_xml::se::Serializer;

#[derive(Debug, PartialEq, Serialize)]
struct Struct {
    question: String,
    answer: u32,
}

let mut buffer = Vec::new();
let mut writer = Writer::new_with_indent(&mut buffer, b' ', 2);
let mut ser = Serializer::with_root(writer, Some("root"));

Struct {
    question: "The Ultimate Question of Life, the Universe, and Everything".into(),
    answer: 42,
}.serialize(&mut ser).unwrap();
assert_eq!(
    String::from_utf8(buffer.clone()).unwrap(),
    r#"<root question="The Ultimate Question of Life, the Universe, and Everything" answer="42"/>"#
);

Trait Implementations

impl<'r, 'w, W: Write> Serializer for &'w mut Serializer<'r, W>[src]

type Ok = ()

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. Read more

type Error = DeError

The error type when some error occurs during serialization.

type SerializeSeq = Seq<'r, 'w, W>

Type returned from serialize_seq for serializing the content of the sequence. Read more

type SerializeTuple = Tuple<'r, 'w, W>

Type returned from serialize_tuple for serializing the content of the tuple. Read more

type SerializeTupleStruct = Tuple<'r, 'w, W>

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

type SerializeTupleVariant = Tuple<'r, 'w, W>

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

type SerializeMap = Map<'r, 'w, W>

Type returned from serialize_map for serializing the content of the map. Read more

type SerializeStruct = Struct<'r, 'w, W>

Type returned from serialize_struct for serializing the content of the struct. Read more

type SerializeStructVariant = Struct<'r, 'w, W>

Type returned from serialize_struct_variant for serializing the content of the struct variant. Read more

Auto Trait Implementations

impl<'r, W> RefUnwindSafe for Serializer<'r, W> where
    W: RefUnwindSafe

impl<'r, W> Send for Serializer<'r, W> where
    W: Send

impl<'r, W> Sync for Serializer<'r, W> where
    W: Sync

impl<'r, W> Unpin for Serializer<'r, W> where
    W: Unpin

impl<'r, W> UnwindSafe for Serializer<'r, W> where
    W: UnwindSafe

Blanket Implementations

impl<T> Any for T where
    T: 'static + ?Sized
[src]

impl<T> Borrow<T> for T where
    T: ?Sized
[src]

impl<T> BorrowMut<T> for T where
    T: ?Sized
[src]

impl<T> From<T> for T[src]

impl<T, U> Into<U> for T where
    U: From<T>, 
[src]

impl<T, U> TryFrom<U> for T where
    U: Into<T>, 
[src]

type Error = Infallible

The type returned in the event of a conversion error.

impl<T, U> TryInto<U> for T where
    U: TryFrom<T>, 
[src]

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.