What is the benefit of using the `Serializer` anno...
# serialization
p
What is the benefit of using the
Serializer
annotation?
h
You mean
Serializer(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 libs
p
Ah you mean it creates the synthetic .serializer() function?
h
Yes, it does.
p
Ah nice. If that was only auto discoverable 🤔
h
Hm, if you use the
Serializer
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
)
p
It was an example for an external dependency
h
What do you mean?
Copy code
// 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)
p
If that was only auto discoverable 🤔
Yeah I know, but with your example the Serializer annotation is now pointless
h
Yeah, you still need to add the serializer to the uri parameter.
Yeah, because you implemented the serializer by yourself.
p
I know, but from my perspective, kotlinx could already do that logic without me being forced to add file:UseSerializers everywhere
h
Hm, but you could serialize the same type with different strategies, eg handling dates from different apis...
p
Yep I know. But it could do it if there was only a single available or sth like that
Just dreaming, i know that it doesn't play well with the way how compilation works
h
I agree, I think it is annoying too, buuuut, the serializer could be (in theory) also stored in another module or dependency. 😄
p
To me, even a compiler flag would greatly simplify things. Sth like
Copy code
serialization {
  serializer("some.platform.FancyClass", "my.company.FancyClassSerializer")
}
h
Isn't this possible with the serializerModule? 🤔
p
Yeah but contextual serialization is an all or nothing thing and you still need to declare it.
I think the best solution for me would be a gradle configuration option, i.e.
Copy code
serialization {
  contextualSerialization("android.net.Uri", "java.net.URL")
}