Module serde_with::rust::tuple_list_as_map [−][src]
This serializes a list of tuples into a map and back
Normally, you want to use a HashMap
or a BTreeMap
when deserializing a map.
However, sometimes this is not possible due to type contains, e.g., if the type implements neither Hash
nor Ord
.
Another use case is deserializing a map with duplicate keys.
The implementation is generic using the FromIterator
and IntoIterator
traits.
Therefore, all of Vec
, VecDeque
, and LinkedList
and anything which implements those are supported.
Examples
Wrapper
does not implement Hash
nor Ord
, thus prohibiting the use HashMap
or BTreeMap
.
#[derive(Debug, Deserialize, Serialize, Default)] struct S { #[serde(with = "serde_with::rust::tuple_list_as_map")] s: Vec<(Wrapper<i32>, Wrapper<String>)>, } #[derive(Clone, Debug, Serialize, Deserialize)] #[serde(transparent)] struct Wrapper<T>(T); let from = r#"{ "s": { "1": "Hi", "2": "Cake", "99": "Lie" } }"#; let mut expected = S::default(); expected.s.push((Wrapper(1), Wrapper("Hi".into()))); expected.s.push((Wrapper(2), Wrapper("Cake".into()))); expected.s.push((Wrapper(99), Wrapper("Lie".into()))); let res: S = serde_json::from_str(from).unwrap(); for ((exp_k, exp_v), (res_k, res_v)) in expected.s.iter().zip(&res.s) { assert_eq!(exp_k.0, res_k.0); assert_eq!(exp_v.0, res_v.0); } assert_eq!(from, serde_json::to_string_pretty(&expected).unwrap());
In this example, the serialized format contains duplicate keys, which is not supported with HashMap
or BTreeMap
.
#[derive(Debug, Deserialize, Serialize, PartialEq, Default)] struct S { #[serde(with = "serde_with::rust::tuple_list_as_map")] s: Vec<(i32, String)>, } let from = r#"{ "s": { "1": "Hi", "1": "Cake", "1": "Lie" } }"#; let mut expected = S::default(); expected.s.push((1, "Hi".into())); expected.s.push((1, "Cake".into())); expected.s.push((1, "Lie".into())); let res: S = serde_json::from_str(from).unwrap(); assert_eq!(3, res.s.len()); assert_eq!(expected, res); assert_eq!(from, serde_json::to_string_pretty(&expected).unwrap());
Functions
deserialize | Deserialize a map into an iterator of tuples. |
serialize | Serialize any iteration of tuples into a map. |