serde icon indicating copy to clipboard operation
serde copied to clipboard

Question: Why can't `#[serde(default)]` be applied to enums?

Open ahl opened this issue 3 years ago • 0 comments

It seems like it might be viable to have #[serde(default)] for an enum that implements Default. I could imagine this being challenging for an externally tagged enum since the type (object or string?) isn't known a priori, but at least would this be viable for an internally- or adjacently-tagged enum since it they're equivalent to structs with constraints?

struct AdjacentEnum {
    tag: VariantPartOfEnum,
    contents: ContentsPartOfEnumIfItWereUntagged,
}

struct InternalEnum {
    tag: VariantPartOfEnum,
    #[serde(flatten)]
    contents: ContentsPartOfEnumIfItWereUntagged,
}

If any of this seems reasonable I would be happy to put together a PR.

ahl avatar Jul 14 '22 18:07 ahl