Skip to content

Optional content for enum variants? #1553

@vadimcn

Description

@vadimcn

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)

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions