Struct quick_xml::de::Deserializer

source ·
pub struct Deserializer<'de, R>where
    R: XmlRead<'de>,
{ /* private fields */ }
Available on crate feature serialize only.
Expand description

A structure that deserializes XML into Rust values.

Implementations§

Available on crate feature overlapped-lists only.

Set the maximum number of events that could be skipped during deserialization of sequences.

If <element> contains more than specified nested elements, $text or CDATA nodes, then DeError::TooManyEvents will be returned during deserialization of sequence field (any type that uses deserialize_seq for the deserialization, for example, Vec<T>).

This method can be used to prevent a DoS attack and infinite memory consumption when parsing a very large XML to a sequence field.

It is strongly recommended to set limit to some value when you parse data from untrusted sources. You should choose a value that your typical XMLs can have between different elements that corresponds to the same sequence.

Examples

Let’s imagine, that we deserialize such structure:

struct List {
  item: Vec<()>,
}

The XML that we try to parse look like this:

<any-name>
  <item/>
  <!-- Bufferization starts at this point -->
  <another-item>
    <some-element>with text</some-element>
    <yet-another-element/>
  </another-item>
  <!-- Buffer will be emptied at this point; 7 events were buffered -->
  <item/>
  <!-- There is nothing to buffer, because elements follows each other -->
  <item/>
</any-name>

There, when we deserialize the item field, we need to buffer 7 events, before we can deserialize the second <item/>:

  • <another-item>
  • <some-element>
  • $text(with text)
  • </some-element>
  • <yet-another-element/> (virtual start event)
  • <yet-another-element/> (virtual end event)
  • </another-item>

Note, that <yet-another-element/> internally represented as 2 events: one for the start tag and one for the end tag. In the future this can be eliminated, but for now we use auto-expanding feature of a reader, because this simplifies deserializer code.

Create new deserializer that will borrow data from the specified string

Create new deserializer that will copy data from the specified reader into internal buffer. If you already have a string use Self::from_str instead, because it will borrow instead of copy. If you have &[u8] which is known to represent UTF-8, you can decode it first before using from_str.

Trait Implementations§

Representation of owned strings the same as non-owned.

Character represented as strings.

Forwards deserialization to the deserialize_bytes.

Identifiers represented as strings.

Unit represented in XML as a xs:element or text/CDATA content. Any content inside xs:element is ignored and skipped.

Produces unit struct from any of following inputs:

  • any <tag ...>...</tag>
  • any <tag .../>
  • any text content
  • any CDATA content
Events handling
EventXMLHandling
DeEvent::Start<tag>...</tag>Calls visitor.visit_unit(), consumes all events up to corresponding End event
DeEvent::End</tag>Emits UnexpectedEnd("tag")
DeEvent::Texttext contentCalls visitor.visit_unit(). Text content is ignored
DeEvent::CData<![CDATA[cdata content]]>Calls visitor.visit_unit(). CDATA content is ignored
DeEvent::EofEmits UnexpectedEof

Representation of the names units the same as unnamed units

Representation of tuples the same as sequences.

Representation of named tuples the same as unnamed tuples.

Always call visitor.visit_unit() because returned value ignored in any case.

This method consumes any single event except the Start event, in which case all events up to corresponding End event will be consumed.

This method returns error if current event is End or Eof

The error type that can be returned if some error occurs during deserialization.
Hint that the Deserialize type is expecting an i8 value.
Hint that the Deserialize type is expecting an i16 value.
Hint that the Deserialize type is expecting an i32 value.
Hint that the Deserialize type is expecting an i64 value.
Hint that the Deserialize type is expecting a u8 value.
Hint that the Deserialize type is expecting a u16 value.
Hint that the Deserialize type is expecting a u32 value.
Hint that the Deserialize type is expecting a u64 value.
Hint that the Deserialize type is expecting an i128 value. Read more
Hint that the Deserialize type is expecting an u128 value. Read more
Hint that the Deserialize type is expecting a f32 value.
Hint that the Deserialize type is expecting a f64 value.
Hint that the Deserialize type is expecting a bool value.
Hint that the Deserialize type is expecting a string value and does not benefit from taking ownership of buffered data owned by the Deserializer. Read more
Hint that the Deserialize type is expecting a struct with a particular name and fields.
Hint that the Deserialize type is expecting a newtype struct with a particular name.
Hint that the Deserialize type is expecting an enum value with a particular name and possible variants.
Hint that the Deserialize type is expecting a sequence of values.
Hint that the Deserialize type is expecting a map of key-value pairs.
Hint that the Deserialize type is expecting an optional value. Read more
Require the Deserializer to figure out how to drive the visitor based on what data type is in the input. Read more
Determine whether Deserialize implementations should expect to deserialize their human-readable form. Read more

An accessor to sequence elements forming a value for top-level sequence of XML elements.

Technically, multiple top-level elements violates XML rule of only one top-level element, but we consider this as several concatenated XML documents.

The error type that can be returned if some error occurs during deserialization.
This returns Ok(Some(value)) for the next value in the sequence, or Ok(None) if there are no more remaining items. Read more
This returns Ok(Some(value)) for the next value in the sequence, or Ok(None) if there are no more remaining items. Read more
Returns the number of elements remaining in the sequence, if known.

Auto Trait Implementations§

Blanket Implementations§

Gets the TypeId of self. Read more
Immutably borrows from an owned value. Read more
Mutably borrows from an owned value. Read more

Returns the argument unchanged.

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

The type returned in the event of a conversion error.
Performs the conversion.
The type returned in the event of a conversion error.
Performs the conversion.