Function serde_any::from_slice [−][src]
pub fn from_slice<'a, T>(s: &'a [u8], format: Format) -> Result<T, Error> where
T: for<'de> Deserialize<'de>,
Deserialize from a byte slice using a specified format
This function will attempt to deserialize the string using each supported format, and will return the result of the first successful deserialization.
Errors
If none of the supported formats can deserialize the string successfully,
Error::NoSuccessfulParse
is returned.
Example
#[macro_use] extern crate serde; extern crate serde_any; extern crate failure; use failure::Error; use serde_any::Format; #[derive(Deserialize, Debug)] struct Person { name: String, knowledge: u32, } fn main() -> Result<(), Error> { let data = b"{ \"name\": \"Jon Snow\", \"knowledge\": 0 }"; let person: Person = serde_any::from_slice(data, Format::Json)?; println!("{:#?}", person); Ok(()) }