question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Fallback Default Value for Enum Type Field

See original GitHub issue

Is your feature request related to a problem? Please describe. jackson-databind: 2.11.4

Hi, I was trying to deserialize json to Kotlin data class object which includes enum type field. But since the value of the corresponding json field is not one of the enum values, it throws InvalidFormatException

data class A(
   val type: EnumType
   ...
) : Serializable

However, I don’t need values other than the ones that are already of the enum values. So I want to set the field of the object as the default value if the value from the json does not match with any of the enum values. I think there’s no way this can be done as far as I have searched.

Describe the solution you’d like Annotation that sets default value as fallback on a field of a serialzable object

Usage example

data class A(
   @DeserializeEnumDefault(EnumType.TypeA)
   val type: EnumType
   ...
) : Serializable

Issue Analytics

  • State:closed
  • Created a year ago
  • Comments:5 (3 by maintainers)

github_iconTop GitHub Comments

1reaction
cowtowncodercommented, Jul 23, 2022

@ko-ing I am very glad it worked out once you found the problem. Thank you for confirming!

1reaction
ko-ingcommented, Jul 23, 2022

Thanks for your gentle answer!

It turned out that it was my mistake to only implement the object mapper on the encoder of webclient. I managed to add this configuration and works fine for me!

                clientDefaultCodecsConfigurer.defaultCodecs().jackson2JsonDecoder(Jackson2JsonDecoder(objectMapper(), MediaType.APPLICATION_JSON))

I’ll close this issue Have a good day!

Read more comments on GitHub >

github_iconTop Results From Across the Web

Choosing the default value of an Enum type without having to ...
The default value for an enumeration type is 0: "By default, the first enumerator has the value 0, and the value of each...
Read more >
Jackson JSON - Using @JsonEnumDefaultValue to ... - LogicBig
JsonEnumDefaultValue annotation can be used on an enum element to define a default (fallback) value used during deserialize.
Read more >
Assigning a default value if one doesnt exist in enum - Support
I wanted to know if it was possible to set a default value for a enum class if a value doesn't exist in...
Read more >
Safety Considerations When Using Enums in Avro Schemas
Here we specify two defaults: A field default — informs a reader to use the default value if the field is not present...
Read more >
3107-derive-default-enum - The Rust RFC Book
The ability to add default values to fields of enum variants does not mean that you can suddenly #[derive(Default)] on the enum. A...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found