Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Optional content for enum variants? #1553

Closed
vadimcn opened this issue Jun 21, 2019 · 0 comments · Fixed by #1706
Closed

Optional content for enum variants? #1553

vadimcn opened this issue Jun 21, 2019 · 0 comments · Fixed by #1706

Comments

@vadimcn
Copy link

vadimcn commented Jun 21, 2019

Hi!
I need to de/serialize JSON messages such as the following:

{ ...,  "command": "cmd1", "arguments": { <command-dependent> } }

So far so good, this fits the adjacently tagged enum representation.
The wrinkle is that for some commands the arguments field may be optional. I tried modelling this as

#[serde(tag = "command", content = "arguments")]
enum Command {
    cmd1(Option<Cmd1Arguments>)
    ...
}

but Serde is not liking it and complains about "missing field `arguments`".

Any suggestions? (besides custom serialization)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
1 participant