{"action":"created","issue":{"url":"https://api.github.com/repos/serde-rs/serde/issues/760","repository_url":"https://api.github.com/repos/serde-rs/serde","labels_url":"https://api.github.com/repos/serde-rs/serde/issues/760/labels{/name}","comments_url":"https://api.github.com/repos/serde-rs/serde/issues/760/comments","events_url":"https://api.github.com/repos/serde-rs/serde/issues/760/events","html_url":"https://github.com/serde-rs/serde/issues/760","id":207051414,"node_id":"MDU6SXNzdWUyMDcwNTE0MTQ=","number":760,"title":"Serialize extra constant fields","user":{"login":"RReverser","id":557590,"node_id":"MDQ6VXNlcjU1NzU5MA==","avatar_url":"https://avatars2.githubusercontent.com/u/557590?v=4","gravatar_id":"","url":"https://api.github.com/users/RReverser","html_url":"https://github.com/RReverser","followers_url":"https://api.github.com/users/RReverser/followers","following_url":"https://api.github.com/users/RReverser/following{/other_user}","gists_url":"https://api.github.com/users/RReverser/gists{/gist_id}","starred_url":"https://api.github.com/users/RReverser/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/RReverser/subscriptions","organizations_url":"https://api.github.com/users/RReverser/orgs","repos_url":"https://api.github.com/users/RReverser/repos","events_url":"https://api.github.com/users/RReverser/events{/privacy}","received_events_url":"https://api.github.com/users/RReverser/received_events","type":"User","site_admin":false},"labels":[{"id":65683853,"node_id":"MDU6TGFiZWw2NTY4Mzg1Mw==","url":"https://api.github.com/repos/serde-rs/serde/labels/enhancement","name":"enhancement","color":"84b6eb","default":true}],"state":"open","locked":false,"assignee":null,"assignees":[],"milestone":null,"comments":16,"created_at":"2017-02-12T13:55:03Z","updated_at":"2018-08-10T01:26:21Z","closed_at":null,"author_association":"NONE","body":"Serde allows to use `tag = \"...\"` on enums, but I have a format in which there are also \"tags\" on structs - basically extra constant fields that must be present in serialized form to pass validations, but as Rust is typed, they don't matter in Rust representation.\r\n\r\nFor the sake of example:\r\n\r\n```rust\r\npub struct Program {\r\n  // type: \"Program\"\r\n  pub body: Vec<Stmt>\r\n}\r\n```\r\n\r\nThis must be serialized as `{ \"type\": \"Program\", \"body\": [...] }`.\r\n\r\nAnother example, as part of enum:\r\n\r\n```rust\r\n#[serde(tag = \"type\")]\r\npub enum Expr {\r\n  ...\r\n  #[serde(rename = \"Literal\")]\r\n  NullLiteral // must be serialized as `{ \"type\": \"Literal\", \"value\": null }` not as `{ \"type\": \"Literal\" }`\r\n}\r\n```\r\n\r\nOf course, I can add extra meaningless `type: &'static str` to `Program` and `value: ()` to `NullLiteral` or implement custom `Serialize` / `serialize_with` for each of these cases, but as it seems to be a quite common repetitive task, would it make sense to add support for constant fields in Serde itself via a separate attribute?\r\n\r\nFor deserialize implementation, those fields would be just checked for existence (like `tag` on enums), and for serialize they would be implicitly added."},"comment":{"url":"https://api.github.com/repos/serde-rs/serde/issues/comments/411947283","html_url":"https://github.com/serde-rs/serde/issues/760#issuecomment-411947283","issue_url":"https://api.github.com/repos/serde-rs/serde/issues/760","id":411947283,"node_id":"MDEyOklzc3VlQ29tbWVudDQxMTk0NzI4Mw==","user":{"login":"Ekleog","id":411447,"node_id":"MDQ6VXNlcjQxMTQ0Nw==","avatar_url":"https://avatars1.githubusercontent.com/u/411447?v=4","gravatar_id":"","url":"https://api.github.com/users/Ekleog","html_url":"https://github.com/Ekleog","followers_url":"https://api.github.com/users/Ekleog/followers","following_url":"https://api.github.com/users/Ekleog/following{/other_user}","gists_url":"https://api.github.com/users/Ekleog/gists{/gist_id}","starred_url":"https://api.github.com/users/Ekleog/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/Ekleog/subscriptions","organizations_url":"https://api.github.com/users/Ekleog/orgs","repos_url":"https://api.github.com/users/Ekleog/repos","events_url":"https://api.github.com/users/Ekleog/events{/privacy}","received_events_url":"https://api.github.com/users/Ekleog/received_events","type":"User","site_admin":false},"created_at":"2018-08-10T01:26:21Z","updated_at":"2018-08-10T01:26:21Z","author_association":"NONE","body":"@RReverser The issue with tagging is not at deserialization time (where there could be enums indeed), but at serialization time, because serialization is done with a function similar to `do_serialize<T: MyTrait>(t: T)`. Here, there is no macro that could help. :) Oh, and your solution doesn't work when trying to deserialize into a generic struct, because it's not a simple `:ident`.\r\n\r\nAnyway, this is getting pretty off-topic, so let's continue the discussion somewhere else (eg. `#serde`, I'm `ekleog` there too) :)"}}
{"actor":{"display_login":"Ekleog"}}