[−][src]Function serde_json::de::from_reader
pub fn from_reader<R, T>(rdr: R) -> Result<T> where
R: Read,
T: DeserializeOwned,
Deserialize an instance of type T
from an IO stream of JSON.
Errors
This conversion can fail if the structure of the input does not match the
structure expected by T
, for example if T
is a struct type but the input
contains something other than a JSON map. It can also fail if the structure
is correct but T
's implementation of Deserialize
decides that something
is wrong with the data, for example required struct fields are missing from
the JSON map or some number is too big to fit in the expected primitive
type.
#[macro_use] extern crate serde_derive; extern crate serde; extern crate serde_json; use std::error::Error; use std::fs::File; use std::path::Path; #[derive(Deserialize, Debug)] struct User { fingerprint: String, location: String, } fn read_user_from_file<P: AsRef<Path>>(path: P) -> Result<User, Box<Error>> { // Open the file in read-only mode. let file = File::open(path)?; // Read the JSON contents of the file as an instance of `User`. let u = serde_json::from_reader(file)?; // Return the `User`. Ok(u) } fn main() { let u = read_user_from_file("test.json").unwrap(); println!("{:#?}", u); }