Function serde_any::de::from_reader [−][src]
pub fn from_reader<T, R>(reader: R, format: Format) -> Result<T, Error> where
T: DeserializeOwned,
R: Read,
Deserialize from an IO stream 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 std::fs::File; use std::path::Path; use serde_any::Format; #[derive(Deserialize, Debug)] struct User { fingerprint: String, location: String, } fn read_user_from_file<P: AsRef<Path>>(path: P, format: Format) -> Result<User, Error> { // Open the file in read-only mode. let file = File::open(path)?; // Read the contents of the file as an instance of `User`. let u = serde_any::from_reader(file, format)?; // Return the `User`. Ok(u) } fn main() { match read_user_from_file("test.json", Format::Json) { Ok(u) => println!("{:#?}", u), Err(e) => println!("Error deserializing user: {}", e), }; }