Function serde_any::ser::to_file[][src]

pub fn to_file<T, P>(path: P, value: &T) -> Result<(), Error> where
    T: Serialize,
    P: AsRef<Path>, 

Serialize to a file

Errors

If the serialization format cannot be inferred from the file name, Error::UnsupportedFileExtension is returned.

If opening the file for writing fails, Error::Io is returned.

If serialization fails, the format-specific Error variant is returned, with the underlying error as its cause.

Example

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

use serde_any::Format;
use failure::Error;

use std::fs::File;

#[derive(Serialize, Debug)]
struct Person {
    name: String,
    knowledge: u32,
}

fn main() -> Result<(), Error> {
    let bran = Person {
        name: "Brandon Stark".to_string(),
        knowledge: 100,
    };
    serde_any::to_file("bran.yaml", &bran)?;
    Ok(())
}