Function serde_any::de::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 the specified format is not supported, this function returns Error::UnsupportedFormat.

If the conversion itself fails, the format-specific variant of Error will be returned, with the underlying error as its cause.

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(())
}