Skip to content

Fallback value for unknown enum values #3057

@Nohus

Description

@Nohus

When deserializing enum values, it would be useful to specify a fallback value, in case a remote API introduces a new value for the enum.

Example:

enum class Color {
    Red,
    Green,
    @FallbackEnumValue
    Unknown,
}

data class Scarf(val colors: List<Color>)

Then, the following JSON:

{ "colors": ["Green", "Blue", "Red"] }

Would result in:

Scarf(listOf(Green, Unknown, Red))

Another use case would be an API returning an enum with many possible values, where the client only cares about a subset of them, and would like to consider all other as a single "Other" value.

This is supported in Jackson as @JsonEnumDefaultValue.

There was a similar issue #90, but it asked for the ability to use the default value as declared on the use site of the enum, which is different and not applicable to the above example.

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions