Serde with multiple of the same key

Does serde allow multiple of the same key? Should one have special handling for it, if so desired?

A small adaption to the "custom map" example (simply defining MyMap<K, V> as a wrapper around Vec<(K, V)>) shows that it does.

fn main() {
    let map = serde_json::from_str::<MyMap<String, i32>>(r#"
{"a": 1, "a": 2}
    "#).unwrap();

    println!("{:?}", map);
}
MyMap([("a", 1), ("a", 2)])
1 Like

nod

when would we actually want this?

I could imagine some webserver somewhere responding with JSON that it creates via string concatenation. Such a web server might occasionally reuse the same key in edge cases. You might have to use this feature to properly receive responses from that web server.

However, outside such cases where you need compatibility with other sketchy software, you would not want to do this.

...how about formats that (by spec) encode multimaps? are those "sketchy"?

(not that we know of any...)

If they do so by spec, then its not sketchy. If you wanted something like that in JSON, then I would probably format it as list with [[key1, val1], [key2, val2]]. Having multiple keys in such a list is fine.

1 Like

alright, guess we'll make an effort to support it somehow...