-
Hey, let 's say I have a hashmap : HashMap<i8,u32>, and I want to serialize this as a sequence of tuples (i8,u32). How can I do that without creating an extra struct and using an attribute macro like serde_as? I just want to do that sort of in-place on the hashmap variable, I don't want the hashmap to be contained in a "parent struct". I think that would be possible if I could e.g. construct a Any help is very much appreciated! |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 2 replies
-
Yes, you can do it. You do not construct a use serde_with::*;
use std::collections::HashMap;
fn main() {
let hashmap: HashMap<i8, u32> = HashMap::from([(1, 2), (3, 4)]);
let mut data = Vec::new();
{
let mut ser = serde_json::Serializer::new(&mut data);
As::<Seq<(Same, Same)>>::serialize(&hashmap, &mut ser).unwrap();
}
let json = String::from_utf8(data).unwrap();
println!("{json}"); // prints [[1,2],[3,4]]
} To get this part |
Beta Was this translation helpful? Give feedback.
Yes, you can do it. You do not construct a
Seq
. Most of the types in this crate are meant to be constructed and carried around. Often they do not even have any data, but just behavior implemented.To get this part
As::<Seq<(Same, Same)>>
right you have to perform all the steps of theser…