Module serde_tagged::ser::adj::struc

source ·
Expand description

Serialization of adjacently tagged values using structs.

Tagging a value adjacently using this strategy will create a struct with two fields, where the first field will be the tag and the second field the value.

The representation of this tagging format depends largely on the underlying data format, e.g. in JSON, this is equivalent to the map-based adjacent tagging format, whereas with msgpack, it would be similar to the tuple-based format.

§Warning

If the deserialization-process depends on the tag (i.e. with deserialize and/or Visitor), deserialization of struct-based adjacently tagged values is only supported for self-describing formats.

§Examples serializing to JSON

Serializing a value

let foo: i32 = 42;

let mut serializer = serde_json::Serializer::new(std::io::stdout());
serde_tagged::ser::adj::struc::serialize(
    &mut serializer,
    "Tagged",
    "t", "bar",
    "c", &foo,
).unwrap();

with a struct-name of "Tagged", a tag-key of "t", a tag value of "bar", and a value-key of "c" will produce

{
    "t": "bar",
    "c": 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::adj::struc::serialize(
    &mut serializer,
    "Tagged",
    "t", "my-tag",
    "c", &foo,
).unwrap();

with a struct-name of "Tagged", a tag-key of "t", a tag value of "my-tag", and a value-key of "c" will produce

{
    "t": "my-tag",
    "c": { "bar": "baz" }
}

Structs§

  • A serializer that Serializes the specified tag-key, tag, value-key and value as struct.

Functions§

  • Serializes the specified tag-key, tag, value-key and value as struct.