serde_tagged/
lib.rs

1//! # Tagging values for de-/serialization
2//!
3//! `serde` does not provide a way to store type-information during
4//! serialization, thus de-/serializing trait-objects requires a considerable
5//! amount of boilerplate code to work. This library aims to help with that by
6//! providing multiple ways to store information, i.e. a tag, associated with
7//! a value and later retreive that information during deserialization.
8//! The retreived tag information can then be used to select a type-specific
9//! deserializer.
10//!
11//! A tag can be almost any type, its requirements mainly depend on what you
12//! want to use it for. It should implement `Serialize` and `Deserialize`
13//! (otherwise it would of course be impossible to de-/serialize it). It
14//! should also provide means for comparison, which is required to access the
15//! specific deserializer associated with that tag (unless the tag serves
16//! another purpose). Further restrictions on the tag type may be imposed by
17//! the data- and tag-format you choose (e.g. JSON only allows strings as keys
18//! in JSON-objects).
19//!
20//! This library provides multiple formats to store (and retreive) tags, that
21//! are somewhat similar to the way enums can be tagged in `serde`. The
22//! (currently) supported formats are:
23//!
24//! - [externally tagged](::ser::external), as in `{ <tag> => <value> }`
25//! - [internally tagged](::ser::internal) (i.e. embedded in the value)
26//! - [adjacently tagged using tuples](::ser::adj::tuple), as in
27//!   `( <tag>, <value> )`
28//! - [adjacently tagged using maps](::ser::adj::map), as in
29//!   `{ <tag-key> => <tag>, <value-key> => <value> }`
30//! - [adjacently tagged using structs](::ser::adj::struc), as in
31//!   `{ <tag-key>: <tag>, <value-key>: <value> }`
32//!
33//! ## A quick overview
34//!
35//! This crate is separated into two main modules: [`ser`](::ser) for
36//! serialization and [`de`](::de) for deserialization. Both modules contain
37//! further submodules, each representing a separate tagging-format.
38//!
39//! ### Serialization
40//!
41//! For each tagging-format both, a `serialize` function and a `Serializer`
42//! are provided, which allow for values to be serialized with a pre-existing
43//! serializer defining the data format, a tag, and possibly further
44//! format-specific parameters. You should always prefer the `serialize`
45//! function to the `Serializer` as it, in most cases, allows for a better
46//! performance.
47//!
48//! __Note:__
49//! Tagged serialization requires access to a `Serializer`, however some data
50//! formats do not provide direct access to the serializer. In such a case you
51//! could create a wrapper-type with a custom `Serialize` implementation.
52//!
53//! ### Deserialization
54//!
55//! For deserialization, the `deserialize` function (and in some cases also
56//! variants of it) are provided in the respective format-modules. Have a look
57//! the respective function documentation for more details.
58//!
59//! ## Examples
60//!
61//! For some examples have a look at the examples directory in the repository.
62
63#![allow(clippy::redundant_field_names)]
64
65#[macro_use]
66extern crate serde;
67
68#[cfg(feature = "erased")]
69extern crate erased_serde;
70
71
72pub mod de;
73pub mod ser;
74pub mod util;