kotlinx.serialization icon indicating copy to clipboard operation
kotlinx.serialization copied to clipboard

Polymorphic using both explicit definition and sealed implicit lookup

Open hrach opened this issue 2 years ago • 1 comments

I have a library that exposes the interface Destination and an API accepting this Destination - I need the library to be able to process all destinations with KotlinX.Serialization.

I have a user-land app that defines multiple destinations and joins them via a sealed interface.

package library

interface Destination

package app

@Serializable
sealed interface AppDestination : Destination {
   @Serializable
   object One : AppDestination

   @Serializable
   object Two : AppDestination
}

To be able to pass e.g. AppDestination.One to API accepting Destination, I must explicitly wire them together:

serializersModule = SerializersModule {
	polymorphic(Destination::class) {
		subclass(serializer<AppDestination.One>())
		subclass(serializer<AppDestination.Two>())
	}
}

I'd like to be able to utilize the sealed interface and just call the subclass for AppDestination

serializersModule = SerializersModule {
	polymorphic(Destination::class) {
		subclass(serializer<AppDestination>())
	}
}

Use case: https://github.com/kiwicom/navigation-compose-typed

hrach avatar Feb 20 '23 08:02 hrach

Related: #1865

sandwwraith avatar Feb 20 '23 14:02 sandwwraith