How do i attach type to serde_json string?

Let's say i have the struct:

pub struct MyStruct {
    one: u32,
    two: bool,
}

I would like to serialize this struct into JSON where the end result would include a "type" field as follows:

{'type': 'MyStruct', 'one': 1, 'two': true}

Would that be possible with serde_json?

#[derive(Serialize)]
#[serde(tag = "type")]
pub struct MyStruct {
    one: u32,
    two: bool,
}
2 Likes

This topic was automatically closed 90 days after the last reply. We invite you to open a new topic if you have further questions or comments.