Function serde_any::de::from_file[][src]

pub fn from_file<T, P>(path: P) -> Result<T, Error> where
    T: DeserializeOwned,
    P: AsRef<Path>, 

Deserialize from a file

The format is detected using guess_format. If that fails, such as if the file extension is not recognized, the whole file is read into a buffer, and deserialization is attempted using from_slice_any.

Errors

If the file extension is recognized, but parsing fails, this function returns the error from from_reader.

If the file extension is not recognized and the file cannot be opened, it returns Error::Io with the underlying error as the cause.

If the file extension is not recognized, the file can opened but deserialization fails, this function returns the error from from_slice_any.

Example

#[macro_use]
extern crate serde;
extern crate serde_any;

#[derive(Deserialize, Debug)]
struct User {
    fingerprint: String,
    location: String,
}

fn main() {
    match serde_any::from_file::<User, _>("test.json") {
        Ok(u) => println!("{:#?}", u),
        Err(e) => println!("Error deserializing user: {}", e),
    };
}