Module serde_tagged::ser::external
[−]
[src]
Serialization of externally tagged values.
Tagging values externally will apply the tag via a mapping from tag to value, thus serializing a tagged value with the functionality provided in this module will always result in a map-object with a single entry.
This format is similar to the externally-tagged enum format provided by
serde, however allows for various tag types (not only str
and u32
).
Examples serializing to JSON
A primitive
Serializing a value
let foo: i32 = 42; let mut serializer = serde_json::Serializer::new(std::io::stdout()); serde_tagged::ser::external::serialize(&mut serializer, "bar", &foo).unwrap();
with a tag value of "bar"
will produce
{ "bar": 42 }
A Simple struct
Serializing a value foo
with
#[derive(Serialize)] struct Foo { bar: &'static str, } let foo = Foo { bar: "baz" }; let mut serializer = serde_json::Serializer::new(std::io::stdout()); serde_tagged::ser::external::serialize(&mut serializer, "my-tag", &foo).unwrap();
with a tag-value of "my-tag"
will produce the following JSON output:
{ "my-tag": { "bar": "baz" } }
Structs
Serializer |
A serializer that applies a tag externally to the provided value and serializes them as key-value pair. |
Functions
serialize |
Applies a tag externally to the specified value and serializes them using the provided serializer. |