Function serde_any::de::from_str[][src]

pub fn from_str<'a, T>(s: &'a str, format: Format) -> Result<T, Error> where
    T: for<'de> Deserialize<'de>, 

Deserialize from a string using a specified format

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 = "{
\"name\": \"Jon Snow\",
\"knowledge\": 0
}";
    let person: Person = serde_any::from_str(data, Format::Json)?;
    println!("{:#?}", person);
    Ok(())
}