Function serde_any::de::from_slice_any[][src]

pub fn from_slice_any<'a, T>(s: &'a [u8]) -> Result<T, Error> where
    T: for<'de> Deserialize<'de>, 

Deserialize from a byte slice using any supported format

This function will attempt to deserialize the slice 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_any(data)?;
    println!("{:#?}", person);
    Ok(())
}