Facing a bit of a challenge here. The JSON seriali...
# serialization
j
Facing a bit of a challenge here. The JSON serialized format of some class, we'll call it
A
, that my application reads has changed. I used the
JsonNames
annotation to account for properties that were simply renamed, but there is one property in particular whose name didn't change that went from being (in typescript speak)
string[]
to
{ foo: string, bar: string[] }
. What's the best way to handle this property would could either be a list of strings or a specific class with two properties? Should I: • Create a custom serializer for this property which inspects the JSON structure of the property to determine which one it is? • Create an entire alternative class for
A
and use it as a surrogate after initially trying and failing to deserialize via the old format? • Some other option?
e
j
I think this is the ideal solution. The only thing I'll probably add is:
sealed class CustomProperty : List<String>
And then implement the list by delegation for the two subclasses:
Copy code
@Serializable(with = OldSerializer::class)
data class Old(override val bar: List<String>) : CustomProperty(), List<String> by bar

@Serializable
data class New(val foo: String, override val bar: List<String>) : CustomProperty(), List<String> by bar
That way I don't have to worry about refactoring all the places it was originally just used as a list.
Thanks for the playground link. Should be pretty easy to implement.