Struct serde_with::Seq

source ·
pub struct Seq<V>(_);
Expand description

De/Serialize a Map into a list of tuples

Some formats, like JSON, have limitations on the types of keys for maps. In case of JSON, keys are restricted to strings. Rust features more powerful keys, for example tuples, which can not be serialized to JSON.

This helper serializes the Map into a list of tuples, which do not have the same type restrictions.

Examples

#[serde_as]
#[derive(Deserialize, Serialize)]
struct A {
    #[serde_as(as = "Seq<(_, _)>")]
    s: BTreeMap<(String, u32), u32>,
}

// This converts the Rust type
let data = A {
    s: BTreeMap::from([
        (("Hello".to_string(), 123), 0),
        (("World".to_string(), 456), 1),
    ]),
};

// into this JSON
let value = json!({
    "s": [
        [["Hello", 123], 0],
        [["World", 456], 1]
    ]
});

assert_eq!(value, serde_json::to_value(&data).unwrap());
assert_eq!(data, serde_json::from_value(value).unwrap());

Trait Implementations§

Deserialize this value from the given Serde deserializer.
Deserialize this value from the given Serde deserializer.
Deserialize this value from the given Serde deserializer.
Serialize this value into the given Serde serializer.
Serialize this value into the given Serde serializer.
Serialize this value into the given Serde serializer.

Auto Trait Implementations§

Blanket Implementations§

Gets the TypeId of self. Read more
Immutably borrows from an owned value. Read more
Mutably borrows from an owned value. Read more

Returns the argument unchanged.

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

The type returned in the event of a conversion error.
Performs the conversion.
The type returned in the event of a conversion error.
Performs the conversion.