How can one write a custom serializer for a `Any` ...
# serialization
c
How can one write a custom serializer for a
Any
type? I’m building a data class that has a field that can either come as a
String
or
List<String>
, and I don’t have any control over that. But when I need to serialize it, it will break because of typing issues. Has anyone ever faced this issue and if yes, what’s a possible solution? (edited)
r
I haven't tried it but there's a sample in the docs now for how to handle this exact scenario with json https://github.com/Kotlin/kotlinx.serialization/blob/master/docs/json_transformations.md#transformation-examples-list-mainupulation
👍 1
c
Thank you, will look into that!
@russhwolf I was trying that but forgot one thing. I’,m in multiplatform, so I can’t get
JsonTransformingSerializer
any idea?
r
I haven't dug through all the new 0.20.0 stuff yet so not really sure, but I thought everything in there would be multiplatform. Make sure you have all the right gradle imports
c
Yeah, my bad. Just noticed I’m on an older version.. 😅
r
ah yeah that would do it