Crate serde_any[−][src]
Serde Any
Dynamic serialization and deserialization with the format chosen at runtime
Deserialization with a known format
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
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
andfrom_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 std::io::Read
, read the data into a Vec<u8>
or String
and
call from_slice_any
or from_str_any
.
Serialization
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 std::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.
There is no support for pretty-printing yet.
Re-exports
pub use error::Error; |
pub use format::*; |
pub use de::*; |
pub use ser::*; |
Modules
de |
Deserialize data to a Rust structure |
error |
Contains the common error type |
format |
Types and functions for specifying or determining serialization formats |
ser |
Serialize a Rust structure to any data format |