json icon indicating copy to clipboard operation
json copied to clipboard

How to define i32 tags in enum?

Open lz1998 opened this issue 2 years ago • 1 comments

https://serde.rs/enum-representations.html#internally-tagged

{
    "messages": [
        {"msg_type":1, "a":"aaa"},
        {"msg_type":2, "b":"bbb"},
    ]
}
pub enum Message {
    A(A), // msg_type=1
    B(B)  // msg_type=2
}
pub struct A {
    pub a: String
}
pub struct B {
    pub a: String
}

lz1998 avatar Apr 23 '23 11:04 lz1998

This feature needs to be implemented in serde, not serde-json: https://github.com/serde-rs/serde/pull/2056. We just need to pray dtolnay finds the time someday to click the merge button on that very useful PR

kangalio avatar May 23 '23 17:05 kangalio