pub struct Serializer { /* private fields */ }Expand description
Serializes values to XML.
This is the XML implementation of the Serializer
trait, which allows serializing to XML where the format is not known
upfront. For a value that is serialized once
SerializerConfig::to_string (or to_string) is simpler.
An XML document has a single root element, serializing a second value
fails. Values without a name (like maps) need a Root
or the name of the root element in the configuration (see
SerializerConfig::set_root).
use std::collections::BTreeMap;
use deser_xml::{Serializer, SerializerConfig};
let mut serializer = Serializer::with_config(SerializerConfig::builder().root("r").build());
serializer.serialize(&BTreeMap::from([("@a", 1), ("b", 2)])).unwrap();
assert!(serializer.serialize(&BTreeMap::from([("b", 3)])).is_err());
assert_eq!(serializer.finish(), r#"<r a="1"><b>2</b></r>"#);The serializer is also the stream serializer of XML (see
StreamSerializer): the output can be taken
while the document is written, and large documents can be written in
parts. Output is final once the start tag it’s in is complete: the
start tag of an element is held back until no more attributes can come,
which for maps (whose keys are not known upfront) is the end of the
element. The namespaces that are found once the start tag of the root
element was written are declared on the elements that use them rather
than on the root element (configured
namespaces are always declared on the
root element). To write to a Write use
SerializerConfig::writer.
Implementations§
Source§impl Serializer
impl Serializer
Sourcepub fn new() -> Serializer
pub fn new() -> Serializer
Creates a serializer.
Sourcepub fn with_config(config: SerializerConfig) -> Serializer
pub fn with_config(config: SerializerConfig) -> Serializer
Creates a serializer with the given configuration.
Sourcepub fn config(&self) -> &SerializerConfig
pub fn config(&self) -> &SerializerConfig
Returns the configuration.
Sourcepub fn serialize<T: Serialize + ?Sized>(
&mut self,
value: &T,
) -> Result<(), Error>
pub fn serialize<T: Serialize + ?Sized>( &mut self, value: &T, ) -> Result<(), Error>
Serializes a value.
If the value fails to serialize, nothing is written.
Trait Implementations§
Source§impl Clone for Serializer
impl Clone for Serializer
Source§fn clone(&self) -> Serializer
fn clone(&self) -> Serializer
Clones the serializer.
The clone of a serializer that writes a document in parts cannot
write more values (see
StreamSerializer::in_progress).
1.0.0 (const: unstable) · Source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
source. Read moreSource§impl Debug for Serializer
impl Debug for Serializer
Source§impl Default for Serializer
impl Default for Serializer
Source§fn default() -> Serializer
fn default() -> Serializer
Source§impl Serializer for Serializer
impl Serializer for Serializer
Source§fn drive(&mut self, driver: &mut SerializeDriver<'_>) -> Result<(), Error>
fn drive(&mut self, driver: &mut SerializeDriver<'_>) -> Result<(), Error>
Source§fn serialize_with<T, F>(&mut self, value: &T, setup: F) -> Result<(), Error>
fn serialize_with<T, F>(&mut self, value: &T, setup: F) -> Result<(), Error>
Source§fn serialize_ref(&mut self, value: SerializeRef<'_>) -> Result<(), Error>
fn serialize_ref(&mut self, value: SerializeRef<'_>) -> Result<(), Error>
Source§impl StreamSerializer for Serializer
impl StreamSerializer for Serializer
Source§fn output(&self) -> &[u8] ⓘ
fn output(&self) -> &[u8] ⓘ
Source§fn clear_output(&mut self)
fn clear_output(&mut self)
output, for
instance because they were written.Source§fn supports_partial(&self) -> bool
fn supports_partial(&self) -> bool
Source§fn drive_partial(
&mut self,
driver: &mut SerializeDriver<'_>,
limit: usize,
) -> Result<bool, Error>
fn drive_partial( &mut self, driver: &mut SerializeDriver<'_>, limit: usize, ) -> Result<bool, Error>
Source§fn in_progress(&self) -> bool
fn in_progress(&self) -> bool
true if a value is partially serialized. Read more