I'm deserializing some very dirty data that I don'...
# serialization
j
I'm deserializing some very dirty data that I don't have any control over Example:
Copy code
{
    "field1":"a field that's always there"
    "field2":"a field that's sometimes there, other times not and the content varies from Boolean to String to Dates to numbers"
}
Is it possible to have
ignoreUnknownKeys=false
and then have a list of fields that I can ignore? I want it to throw an error if they send for example JSON that now has a
field3
, but I don't want to care about
field2
and I definitely don't want it in my model. I'm already using a custom JsonContentPolymorphicSerializer so that I can switch between different types for one of the other fields
a
the first suggestion that springs to mind would be • set the type of
field2
to be
JsonElement
, • and have
field2
defined in code but document it as
@Deprecated
- perhaps even with the visibility set to ‘hidden’
another idea: • define
@Serializable(with = IgnoredFieldSerializer::class) object IgnoredField
• set
IgnoredFieldSerializer
descriptor to be a primitive, and configure it so it will always decode to object
IgnoredField
, and encode nothing, or
null
• set
val field2: IgnoredField?