Trait Visitor
trait Visitor<'de>: Sized
This trait represents a visitor that walks through a deserializer.
Lifetime
The 'de lifetime of this trait is the requirement for lifetime of data
that may be borrowed by Self::Value. See the page Understanding
deserializer lifetimes for a more detailed explanation of these lifetimes.
Example
# use serde::de::{self, Unexpected, Visitor};
# use std::fmt;
#
/// A visitor that deserializes a long string - a string containing at least
/// some minimum number of bytes.
struct LongString {
min: usize,
}
impl<'de> Visitor<'de> 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: de::Error,
{
if s.len() >= self.min {
Ok(s.to_owned())
} else {
Err(de::Error::invalid_value(Unexpected::Str(s), &self))
}
}
}
Associated Types
type ValueThe value produced by this visitor.
Required Methods
fn expecting(self: &Self, formatter: &mut Formatter<'_>) -> ResultFormat 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.
# use std::fmt; # # struct S { # max: usize, # } # # impl<'de> serde::de::Visitor<'de> for S { # type Value = (); # fn expecting(&self, formatter: &mut fmt::Formatter) -> fmt::Result { write!(formatter, "an integer between 0 and {}", self.max) } # }
Provided Methods
fn visit_bool<E>(self: Self, v: bool) -> Result<<Self as >::Value, E> where E: ErrorThe input contains a boolean.
The default implementation fails with a type error.
fn visit_i8<E>(self: Self, v: i8) -> Result<<Self as >::Value, E> where E: ErrorThe input contains an
i8.The default implementation forwards to
visit_i64.fn visit_i16<E>(self: Self, v: i16) -> Result<<Self as >::Value, E> where E: ErrorThe input contains an
i16.The default implementation forwards to
visit_i64.fn visit_i32<E>(self: Self, v: i32) -> Result<<Self as >::Value, E> where E: ErrorThe input contains an
i32.The default implementation forwards to
visit_i64.fn visit_i64<E>(self: Self, v: i64) -> Result<<Self as >::Value, E> where E: ErrorThe input contains an
i64.The default implementation fails with a type error.
fn visit_i128<E>(self: Self, v: i128) -> Result<<Self as >::Value, E> where E: ErrorThe input contains a
i128.The default implementation fails with a type error.
fn visit_u8<E>(self: Self, v: u8) -> Result<<Self as >::Value, E> where E: ErrorThe input contains a
u8.The default implementation forwards to
visit_u64.fn visit_u16<E>(self: Self, v: u16) -> Result<<Self as >::Value, E> where E: ErrorThe input contains a
u16.The default implementation forwards to
visit_u64.fn visit_u32<E>(self: Self, v: u32) -> Result<<Self as >::Value, E> where E: ErrorThe input contains a
u32.The default implementation forwards to
visit_u64.fn visit_u64<E>(self: Self, v: u64) -> Result<<Self as >::Value, E> where E: ErrorThe input contains a
u64.The default implementation fails with a type error.
fn visit_u128<E>(self: Self, v: u128) -> Result<<Self as >::Value, E> where E: ErrorThe input contains a
u128.The default implementation fails with a type error.
fn visit_f32<E>(self: Self, v: f32) -> Result<<Self as >::Value, E> where E: ErrorThe input contains an
f32.The default implementation forwards to
visit_f64.fn visit_f64<E>(self: Self, v: f64) -> Result<<Self as >::Value, E> where E: ErrorThe input contains an
f64.The default implementation fails with a type error.
fn visit_char<E>(self: Self, v: char) -> Result<<Self as >::Value, E> where E: ErrorThe input contains a
char.The default implementation forwards to
visit_stras a one-character string.fn visit_str<E>(self: Self, v: &str) -> Result<<Self as >::Value, E> where E: ErrorThe input contains a string. The lifetime of the string is ephemeral and it may be destroyed after this method returns.
This method allows the
Deserializerto avoid a copy by retaining ownership of any buffered data.Deserializeimplementations that do not benefit from taking ownership ofStringdata should indicate that to the deserializer by usingDeserializer::deserialize_strrather thanDeserializer::deserialize_string.It is never correct to implement
visit_stringwithout implementingvisit_str. Implement neither, both, or justvisit_str.fn visit_borrowed_str<E>(self: Self, v: &'de str) -> Result<<Self as >::Value, E> where E: ErrorThe input contains a string that lives at least as long as the
Deserializer.This enables zero-copy deserialization of strings in some formats. For example JSON input containing the JSON string
"borrowed"can be deserialized with zero copying into a&'a stras long as the input data outlives'a.The default implementation forwards to
visit_str.fn visit_string<E>(self: Self, v: String) -> Result<<Self as >::Value, E> where E: ErrorThe input contains a string and ownership of the string is being given to the
Visitor.This method allows the
Visitorto avoid a copy by taking ownership of a string created by theDeserializer.Deserializeimplementations that benefit from taking ownership ofStringdata should indicate that to the deserializer by usingDeserializer::deserialize_stringrather thanDeserializer::deserialize_str, although not every deserializer will honor such a request.It is never correct to implement
visit_stringwithout implementingvisit_str. Implement neither, both, or justvisit_str.The default implementation forwards to
visit_strand then drops theString.fn visit_bytes<E>(self: Self, v: &[u8]) -> Result<<Self as >::Value, E> where E: ErrorThe input contains a byte array. The lifetime of the byte array is ephemeral and it may be destroyed after this method returns.
This method allows the
Deserializerto avoid a copy by retaining ownership of any buffered data.Deserializeimplementations that do not benefit from taking ownership ofVec<u8>data should indicate that to the deserializer by usingDeserializer::deserialize_bytesrather thanDeserializer::deserialize_byte_buf.It is never correct to implement
visit_byte_bufwithout implementingvisit_bytes. Implement neither, both, or justvisit_bytes.fn visit_borrowed_bytes<E>(self: Self, v: &'de [u8]) -> Result<<Self as >::Value, E> where E: ErrorThe input contains a byte array that lives at least as long as the
Deserializer.This enables zero-copy deserialization of bytes in some formats. For example Postcard data containing bytes can be deserialized with zero copying into a
&'a [u8]as long as the input data outlives'a.The default implementation forwards to
visit_bytes.fn visit_byte_buf<E>(self: Self, v: Vec<u8>) -> Result<<Self as >::Value, E> where E: ErrorThe input contains a byte array and ownership of the byte array is being given to the
Visitor.This method allows the
Visitorto avoid a copy by taking ownership of a byte buffer created by theDeserializer.Deserializeimplementations that benefit from taking ownership ofVec<u8>data should indicate that to the deserializer by usingDeserializer::deserialize_byte_bufrather thanDeserializer::deserialize_bytes, although not every deserializer will honor such a request.It is never correct to implement
visit_byte_bufwithout implementingvisit_bytes. Implement neither, both, or justvisit_bytes.The default implementation forwards to
visit_bytesand then drops theVec<u8>.fn visit_none<E>(self: Self) -> Result<<Self as >::Value, E> where E: ErrorThe input contains an optional that is absent.
The default implementation fails with a type error.
fn visit_some<D>(self: Self, deserializer: D) -> Result<<Self as >::Value, <D as >::Error> where D: Deserializer<'de>The input contains an optional that is present.
The default implementation fails with a type error.
fn visit_unit<E>(self: Self) -> Result<<Self as >::Value, E> where E: ErrorThe input contains a unit
().The default implementation fails with a type error.
fn visit_newtype_struct<D>(self: Self, deserializer: D) -> Result<<Self as >::Value, <D as >::Error> where D: Deserializer<'de>The input contains a newtype struct.
The content of the newtype struct may be read from the given
Deserializer.The default implementation fails with a type error.
fn visit_seq<A>(self: Self, seq: A) -> Result<<Self as >::Value, <A as >::Error> where A: SeqAccess<'de>The input contains a sequence of elements.
The default implementation fails with a type error.
fn visit_map<A>(self: Self, map: A) -> Result<<Self as >::Value, <A as >::Error> where A: MapAccess<'de>The input contains a key-value map.
The default implementation fails with a type error.
fn visit_enum<A>(self: Self, data: A) -> Result<<Self as >::Value, <A as >::Error> where A: EnumAccess<'de>The input contains an enum.
The default implementation fails with a type error.
Implementors
impl<'de> Visitor for IgnoredAny