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 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 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.

Enums

Error

The common error type

Format

Serialization or deserialization formats

Functions

from_file

Deserialize from a file

from_file_stem

Deserialize from any file with a given stem

from_reader

Deserialize from an IO stream using a specified format

from_slice

Deserialize from a byte slice using a specified format

from_slice_any

Deserialize from a byte slice using any supported format

from_str

Deserialize from a string using a specified format

from_str_any

Deserialize from a string using any supported format

guess_format

Attempt to guess the serialization/deserialization format from a file name

guess_format_from_extension

Attempt to guess the serialization/deserialization format from a file extension

supported_extensions

Return a list of recognized file extensions

supported_formats

Return a list of supported formats

to_file

Serialize to a file

to_string

Serialize to a String

to_vec

Serialize to a byte vector

to_writer

Serialize to a writer