Paul Woitaschek
08/05/2022, 8:28 AMSerializer
annotation?hfhbd
08/05/2022, 10:42 AMSerializer(forClass)
? To generate a kotlinx serializer by the compiler for a given Kotlin class. I think, the only use case are classes from 3rd party libsPaul Woitaschek
08/05/2022, 10:43 AMhfhbd
08/05/2022, 10:44 AMPaul Woitaschek
08/05/2022, 10:47 AMhfhbd
08/05/2022, 10:53 AMSerializer
annotation, you don't need to implement the object by yourself: https://github.com/Kotlin/kotlinx.serialization/blob/master/docs/serializers.md#deriving-external-serializer-for-another-kotlin-class-experimental
Or you remove the annotation and implement the serializer by yourself.
And yeah, you need to pass the serializer explicitly (or use @file
)Paul Woitaschek
08/05/2022, 10:53 AMhfhbd
08/05/2022, 10:55 AM// external
data class Dog(val name: String)
// your code
@Serializer(forClass = Dog::class)
object DogSerializer
@Serializable
data class Person(@Serializable(with = DogSerializer::class) val dog: Dog)
Paul Woitaschek
08/05/2022, 10:56 AMIf that was only auto discoverable 🤔Yeah I know, but with your example the Serializer annotation is now pointless
hfhbd
08/05/2022, 10:56 AMhfhbd
08/05/2022, 10:56 AMPaul Woitaschek
08/05/2022, 10:57 AMhfhbd
08/05/2022, 10:58 AMPaul Woitaschek
08/05/2022, 10:59 AMPaul Woitaschek
08/05/2022, 11:00 AMhfhbd
08/05/2022, 11:00 AMPaul Woitaschek
08/05/2022, 11:02 AMserialization {
serializer("some.platform.FancyClass", "my.company.FancyClassSerializer")
}
hfhbd
08/05/2022, 11:03 AMPaul Woitaschek
08/05/2022, 11:04 AMPaul Woitaschek
08/05/2022, 11:06 AMserialization {
contextualSerialization("android.net.Uri", "java.net.URL")
}