Skip to content

Use without macros #754

Answered by jonasbb
jbirnick asked this question in Q&A
Jun 5, 2024 · 1 comments · 2 replies
Discussion options

You must be logged in to vote

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.

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 As::<Seq<(Same, Same)>> right you have to perform all the steps of the ser…

Replies: 1 comment 2 replies

Comment options

You must be logged in to vote
2 replies
@jbirnick
Comment options

@jonasbb
Comment options

Answer selected by jonasbb
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
2 participants