serde-rs / serde

Serialization framework for Rust
https://serde.rs/
Apache License 2.0
9.05k stars 767 forks source link

New enum representation combining externally and adjacently tagged #2013

Closed moogstuart closed 2 years ago

moogstuart commented 3 years ago

It would be great to have support for the following enum representation which is kind of a combination of externally and adjacently tagged e.g. externally_with_adjacent (terrible name but I can't think of a good one at the moment).

#[derive(Serialize, Deserialize)]
#[serde(externally_with_adjacent="message")]
enum Message {
    Request { id: String, method: String, params: Params },
    Response { id: String, result: Value },
}

Would be represented as:-

{
"Message: "Request",
"Request": {"id": "...", "method": "...", "params": {...}}
}

Although the Request part is duplicated in the adjacent tag and the first part of the nested keys this allows for receiving json with just the message type and no extra fields (for this example that might not make sense but for other it could be useful.

{
"Message: "Request",
}
l4l commented 3 years ago

Stumbled into the same issue. @dtolnay, may I ask you for the help with this issue? I'm interested in implementing it.

dtolnay commented 2 years ago

I'd prefer not to build this into serde_derive, but anyone should feel free to write a Serialize/Deserialize derive macro that implements this representation. That shouldn't require changes in the serde crate.