Is there a way to configure gson to ignore an unknown enum value? For example
in my client code there is an enum DeviceType has 2 enum value : PC, MOBILE
But when a server is sent another type for example MAC as another DeviceType. How can I configure my gson deserilalizer to set the DeviceType = null instead of throwing an exception?
Thanks,
Sean
Now we can use nullable enum type, for example:
data class SomeDTO (
val type : DeviceType?,
//other values
)
In that case, there won't be any exception in parsing.
You can easily write a custom JsonDeserializer
that ignores unknown values. Read more here.
For a starting point, have a look at the source gson source code, class com.google.gson.DefaultTypeAdapters.EnumTypeAdapter
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With