Hello, currently I'm using `PolymorphicSerializer...
# serialization
p
Hello, currently I'm using
PolymorphicSerializer
for sealed class like this
Copy code
@Serializable(PolymorphicSerializer::class)
sealed class Response {

    @Serializable
    data class Success(val token: Token) : Response()
        
    // serializer defined in serial module
    object Failure : Response()
}
I found this section in doc comment of
@Polymorphic
annotation.
Copy code
Does not affect sealed classes, because they are gonna be serialized with subclasses automatically with special compiler plugin support which would be added later.
At first I thought I could use
@Polymorphic
instead of
@Serializable(PolymorphicSerializer::class)
but it does not work that way. Will this approach change in the future?
s
Does not affect...
means you can't annotate
@Polymorphic sealed class Response
, but can annotate it on use site:
@Polymorphic val resp: Response
Yes, sealed classes would be automatically polymorphic in future
p
Yes, sealed classes would be automatically polymorphic in future
That mean it will implicitly use
PolymorphicSerializer
and it will not be required to add
@Serializable(PolymorphicSerializer::class)
to the sealed class?
s
Yes. Moreover, all subclasses (because they're known at the compile time, unlike regular abstract class) would not require registration in
SerialModule
👍 1
p
For now you also need to create the serialModule for the sealed subclasses. It would be good if there were ways to inject serialModules into a format in some standard way. Perhaps in
Encoder.encodeSerializableValue()
and friends (at least overloaded methods, let's keep binary compatibility).