Trait serde::de::Visitor [] [src]

pub trait Visitor: Sized {
    type Value;
    fn expecting(&self, formatter: &mut Formatter) -> Result;

    fn visit_bool<E>(self, v: bool) -> Result<Self::Value, E>
    where
        E: Error
, { ... } fn visit_i8<E>(self, v: i8) -> Result<Self::Value, E>
    where
        E: Error
, { ... } fn visit_i16<E>(self, v: i16) -> Result<Self::Value, E>
    where
        E: Error
, { ... } fn visit_i32<E>(self, v: i32) -> Result<Self::Value, E>
    where
        E: Error
, { ... } fn visit_i64<E>(self, v: i64) -> Result<Self::Value, E>
    where
        E: Error
, { ... } fn visit_u8<E>(self, v: u8) -> Result<Self::Value, E>
    where
        E: Error
, { ... } fn visit_u16<E>(self, v: u16) -> Result<Self::Value, E>
    where
        E: Error
, { ... } fn visit_u32<E>(self, v: u32) -> Result<Self::Value, E>
    where
        E: Error
, { ... } fn visit_u64<E>(self, v: u64) -> Result<Self::Value, E>
    where
        E: Error
, { ... } fn visit_f32<E>(self, v: f32) -> Result<Self::Value, E>
    where
        E: Error
, { ... } fn visit_f64<E>(self, v: f64) -> Result<Self::Value, E>
    where
        E: Error
, { ... } fn visit_char<E>(self, v: char) -> Result<Self::Value, E>
    where
        E: Error
, { ... } fn visit_str<E>(self, v: &str) -> Result<Self::Value, E>
    where
        E: Error
, { ... } fn visit_string<E>(self, v: String) -> Result<Self::Value, E>
    where
        E: Error
, { ... } fn visit_unit<E>(self) -> Result<Self::Value, E>
    where
        E: Error
, { ... } fn visit_none<E>(self) -> Result<Self::Value, E>
    where
        E: Error
, { ... } fn visit_some<D>(self, deserializer: D) -> Result<Self::Value, D::Error>
    where
        D: Deserializer
, { ... } fn visit_newtype_struct<D>(
        self,
        deserializer: D
    ) -> Result<Self::Value, D::Error>
    where
        D: Deserializer
, { ... } fn visit_seq<V>(self, visitor: V) -> Result<Self::Value, V::Error>
    where
        V: SeqVisitor
, { ... } fn visit_map<V>(self, visitor: V) -> Result<Self::Value, V::Error>
    where
        V: MapVisitor
, { ... } fn visit_enum<V>(self, visitor: V) -> Result<Self::Value, V::Error>
    where
        V: EnumVisitor
, { ... } fn visit_bytes<E>(self, v: &[u8]) -> Result<Self::Value, E>
    where
        E: Error
, { ... } fn visit_byte_buf<E>(self, v: Vec<u8>) -> Result<Self::Value, E>
    where
        E: Error
, { ... } }

This trait represents a visitor that walks through a deserializer.

/// A visitor that deserializes a long string - a string containing at least
/// some minimum number of bytes.
struct LongString {
    min: usize,
}

impl Visitor for LongString {
    type Value = String;

    fn expecting(&self, formatter: &mut fmt::Formatter) -> fmt::Result {
        write!(formatter, "a string containing at least {} bytes", self.min)
    }

    fn visit_str<E>(self, s: &str) -> Result<Self::Value, E>
        where E: Error
    {
        if s.len() >= self.min {
            Ok(s.to_owned())
        } else {
            Err(Error::invalid_value(Unexpected::Str(s), &self))
        }
    }
}

Associated Types

The value produced by this visitor.

Required Methods

Format a message stating what data this Visitor expects to receive.

This is used in error messages. The message should complete the sentence "This Visitor expects to receive ...", for example the message could be "an integer between 0 and 64". The message should not be capitalized and should not end with a period.

fn expecting(&self, formatter: &mut fmt::Formatter) -> fmt::Result {
    write!(formatter, "an integer between 0 and {}", self.max)
}

Provided Methods

Deserialize a bool into a Value.

Deserialize an i8 into a Value.

Deserialize an i16 into a Value.

Deserialize an i32 into a Value.

Deserialize an i64 into a Value.

Deserialize a u8 into a Value.

Deserialize a u16 into a Value.

Deserialize a u32 into a Value.

Deserialize a u64 into a Value.

Deserialize a f32 into a Value.

Deserialize a f64 into a Value.

Deserialize a char into a Value.

Deserialize a &str into a Value.

This method allows the Deserializer to avoid a copy by retaining ownership of any buffered data. Deserialize implementations that do not benefit from taking ownership of String data should indicate that to the deserializer by using Deserializer::deserialize_str rather than Deserializer::deserialize_string.

It is never correct to implement visit_string without implementing visit_str. Implement neither, both, or just visit_str.

Deserialize a String into a Value.

This method allows the Visitor to avoid a copy by taking ownership of a string created by the Deserializer. Deserialize implementations that benefit from taking ownership of String data should indicate that to the deserializer by using Deserializer::deserialize_string rather than Deserializer::deserialize_str, although not every deserializer will honor such a request.

It is never correct to implement visit_string without implementing visit_str. Implement neither, both, or just visit_str.

The default implementation forwards to visit_str and then drops the String.

Deserialize a () into a Value.

Deserialize an absent optional Value.

Deserialize a present optional Value.

Deserialize Value as a newtype struct.

Deserialize Value as a sequence of elements.

Deserialize Value as a key-value map.

Deserialize Value as an enum.

Deserialize a &[u8] into a Value.

This method allows the Deserializer to avoid a copy by retaining ownership of any buffered data. Deserialize implementations that do not benefit from taking ownership of Vec<u8> data should indicate that to the deserializer by using Deserializer::deserialize_bytes rather than Deserializer::deserialize_byte_buf.

It is never correct to implement visit_byte_buf without implementing visit_bytes. Implement neither, both, or just visit_bytes.

Deserialize a Vec<u8> into a Value.

This method allows the Visitor to avoid a copy by taking ownership of a byte buffer created by the Deserializer. Deserialize implementations that benefit from taking ownership of Vec<u8> data should indicate that to the deserializer by using Deserializer::deserialize_byte_buf rather than Deserializer::deserialize_bytes, although not every deserializer will honor such a request.

It is never correct to implement visit_byte_buf without implementing visit_bytes. Implement neither, both, or just visit_bytes.

The default implementation forwards to visit_bytes and then drops the Vec<u8>.

Implementors