Expand description
Make a non-self-describing serde format (like
bincode, bitcode or
postcard) behave as like a self-describing one by transparently
serializing a schema alongside (or separately from) the
data.
The main entry point to the crate is SelfDescribed. For advanced uses SchemaBuilder
and Schema may also be of interest.
use serde::{Deserialize, Serialize};
use serde_describe::{Schema, SelfDescribed};
// Define a type that non-self-describing formats would generally struggle
// with, using skipped fields and untagged unions.
#[derive(Debug, PartialEq, Serialize, Deserialize)]
struct TopLevel {
int32: u32,
#[serde(default, skip_serializing_if = "Vec::is_empty")]
items: Vec<Item>,
}
#[derive(Debug, PartialEq, Serialize, Deserialize)]
#[serde(untagged)]
enum Item {
Int(u32),
Str(String),
}
// Serialize a `Vec<TopLevel>` using the `SelfDescribed` wrapper and bitcode, a
// particularly restrictive binary format.
let original = vec![
TopLevel {
int32: 10,
items: vec![Item::Str("hello".to_owned()), Item::Int(10)],
},
TopLevel {
int32: 20,
items: Vec::new(),
},
];
let bytes = bitcode::serialize(&SelfDescribed(&original))?;
// Then deserialize using the same wrapper to use the embedded schema.
let roundtripped =
bitcode::deserialize::<SelfDescribed<Vec<TopLevel>>>(&bytes)?;
assert_eq!(roundtripped, original);
Structs§
- Described
By - Wraps a serializable and/or deserializable type together with a reference to a
Schema. - Schema
- A saved schema that describes serialized data in a non-self-describing format.
- Schema
Builder - An in-progress schema built by successive calls to
SchemaBuilder::trace. - Self
Described - Wraps a serializable and/or deserializable type such that it gets serialized together with its schema, making non-self-describing formats effectively self-describing.
- Trace
- Represents a traced serde-serialized value. Returned by
SchemaBuilder::trace.
Enums§
- Trace
Error - Errors returned by tracing values.