Crate serde_any[][src]

Serde Any

Dynamic serialization and deserialization with the format chosen at runtime

Reading from and writing to files

let mut m = HashMap::new();
m.insert("a", "alpha");
m.insert("b", "beta");

// Serialize to a file, the format is inferred from the file extension
serde_any::to_file("greek.toml", &m)?;

// Deserialize from a file, the format is also inferred from the file extension
let m2: HashMap<String, String> = serde_any::from_file("greek.toml")?;

Deserialization with a known format

let d = r#"{"a": "alpha", "b": "beta"}"#;
let m: HashMap<String, String> = serde_any::from_str(d, Format::Json)?;

If the deserialization format is known in advance, serde_any mirrors the API of serde_json and serde_yaml. Namely, functions from_reader, from_slice and from_str work in the same way as those in format-specific crates, except that they take an additional Format paramater specifying the deserialization format. The from_file function is provided as a convenience wrapper around from_reader for the common case of reading from a file.

Deserialization by guessing

let d = r#"{"a": "alpha", "b": "beta"}"#;
let m: HashMap<String, String> = serde_any::from_str_any(d)?;

This crate also supports deserialization where the data format is not known in advance. There are three different ways of inferring the data format:

  • with from_file, the format is deduced from the file extension. This is useful if a user can load a data file of any format.
  • with from_file_stem, each filename with the given stem and a supported extension is checked. If any such file exists, its data is deserialized and returned. This is useful for configuration files with a known set of filenames.
  • with from_slice_any and from_str_any, deserialization using each supported format is tried until one succeeds. This is useful when you receive data from an unknown source and don't know what format it is in.

Note there is no corresponding from_reader_any function, as attempting to deserialize from a reader would consume its data. In order to deserialize from a io::Read, read the data into a Vec<u8> or String and call from_slice_any or from_str_any.


let mut m = HashMap::new();
m.insert("a", "alpha");
m.insert("b", "beta");
let s = serde_any::to_string(&m, Format::Yaml)?;

For serialization, the data format must always be provided. Consistent with the format-specific crates, data may be serialized to a String with to_string, to a Vec<u8> with to_vec, or to a io::Write with to_writer.

Alternatively, when writing to a file, the format can be inferred from the file name by the to_file function. Similarly to from_file, this is most useful when saving to a user-selected file.

All serialization functions have pretty printing variants with a _pretty suffix, such as to_string_pretty. However, not all formats support pretty printing. In such cases, the output from pretty printing functions will be identical to the output from serialization functions without pretty printing.

Known limitations

  • Serialization to TOML requires that all non-table values come before any tables. See the toml::ser module documentation for details and workarounds.
  • The XML format cannot serialize sequences.


pub use error::Error;
pub use format::*;
pub use de::*;
pub use ser::*;



Deserialize data to a Rust structure


Contains the common error type


Types and functions for specifying or determining serialization formats


Serialize a Rust structure to any data format