Sealed classes now do not have any special support...
# serialization
s
Sealed classes now do not have any special support, so you should use the same approach as if you were using regular polymorphic serialization (
@Polymorphic
annotation, SerializersModule, etc). We have plans to improve it in future.
p
But it doesn't work
Try running my snippet, its that it explicitly doesn't work, while the same structure with an abstract class works
@sandwwraith ^
s
@Polymorphic
annotation is applied automatically for abstract classes, but not for sealed ones
p
Can you provide an example on how to fix this?
Copy code
@Serializable
@Polymorphic
sealed class BaseSealed {

  @Serializable
  @Polymorphic
  data class Child(val a: String) : BaseSealed()
}
Doesn't work either
s
Sorry for misleading you,
@Polymorphic
should be applied on a use-site for sealed class: https://github.com/kotlin/kotlinx.serialization/blob/45aa7c7ab97f25f205408b46a7069886408bce6b/runtime/commonTest/src/kotlinx/serialization/features/SealedPolymorphismTest.kt#L26 Because special support for sealed classes would be introduced in compiler plugin, we do not apply polymorphic for sealed classes globally
If you have your sealed class on top-level (unwrapped), you can use
PolymorphicSerializer(BaseSealed::class)
as its serializer to enable polymorphism
p
But that needs reflection right?
s
nope, polymorphic serialization designed as cross-platform and therefore is reflectionless. That's why ypu have to specify all sublcasses in some module
p
Can you then please add an annotation for that? Like @Polymorphic(children= [X::class, Y::class]) I really like how I don't have to pass instances around especially in a modularized application