pub struct Seq<V>(/* private fields */);
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§
Source§impl<'de, K, KAs, V, VAs> DeserializeAs<'de, BTreeMap<K, V>> for Seq<(KAs, VAs)>
impl<'de, K, KAs, V, VAs> DeserializeAs<'de, BTreeMap<K, V>> for Seq<(KAs, VAs)>
Source§fn deserialize_as<D>(deserializer: D) -> Result<BTreeMap<K, V>, D::Error>where
D: Deserializer<'de>,
fn deserialize_as<D>(deserializer: D) -> Result<BTreeMap<K, V>, D::Error>where
D: Deserializer<'de>,
Deserialize this value from the given Serde deserializer.
Source§impl<'de, K, KAs, V, VAs> DeserializeAs<'de, HashMap<K, V>> for Seq<(KAs, VAs)>
impl<'de, K, KAs, V, VAs> DeserializeAs<'de, HashMap<K, V>> for Seq<(KAs, VAs)>
Source§fn deserialize_as<D>(deserializer: D) -> Result<HashMap<K, V>, D::Error>where
D: Deserializer<'de>,
fn deserialize_as<D>(deserializer: D) -> Result<HashMap<K, V>, D::Error>where
D: Deserializer<'de>,
Deserialize this value from the given Serde deserializer.
Source§impl<K, KAs, V, VAs> SerializeAs<BTreeMap<K, V>> for Seq<(KAs, VAs)>where
KAs: SerializeAs<K>,
VAs: SerializeAs<V>,
impl<K, KAs, V, VAs> SerializeAs<BTreeMap<K, V>> for Seq<(KAs, VAs)>where
KAs: SerializeAs<K>,
VAs: SerializeAs<V>,
Source§fn serialize_as<S>(
source: &BTreeMap<K, V>,
serializer: S,
) -> Result<S::Ok, S::Error>where
S: Serializer,
fn serialize_as<S>(
source: &BTreeMap<K, V>,
serializer: S,
) -> Result<S::Ok, S::Error>where
S: Serializer,
Serialize this value into the given Serde serializer.
Source§impl<K, KAs, V, VAs, H> SerializeAs<HashMap<K, V, H>> for Seq<(KAs, VAs)>
impl<K, KAs, V, VAs, H> SerializeAs<HashMap<K, V, H>> for Seq<(KAs, VAs)>
Source§fn serialize_as<S>(
source: &HashMap<K, V, H>,
serializer: S,
) -> Result<S::Ok, S::Error>where
S: Serializer,
fn serialize_as<S>(
source: &HashMap<K, V, H>,
serializer: S,
) -> Result<S::Ok, S::Error>where
S: Serializer,
Serialize this value into the given Serde serializer.
Auto Trait Implementations§
impl<V> Freeze for Seq<V>
impl<V> RefUnwindSafe for Seq<V>where
V: RefUnwindSafe,
impl<V> Send for Seq<V>where
V: Send,
impl<V> Sync for Seq<V>where
V: Sync,
impl<V> Unpin for Seq<V>where
V: Unpin,
impl<V> UnwindSafe for Seq<V>where
V: UnwindSafe,
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more