Created
December 23, 2019 15:56
-
-
Save smallufo/c47e641cae24252b610e5d581b6cff78 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
interface IRunnable { | |
fun run() | |
} | |
class Horse : IRunnable { | |
override fun run() { | |
println("horse running") | |
} | |
} | |
class Dog : IRunnable { | |
override fun run() { | |
println("dog running") | |
} | |
} | |
@Serializer(forClass = Horse::class) | |
object HorseSerializer {} | |
@Serializer(forClass = Dog::class) | |
object DogSerializer {} | |
@Serializable | |
data class Zoo(val runnable : IRunnable) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class RunnableTest { | |
private val logger = KotlinLogging.logger { } | |
@Test | |
fun testJson() { | |
val runnableModule = SerializersModule { | |
polymorphic(IRunnable::class) { | |
Horse::class with HorseSerializer | |
Dog::class with DogSerializer | |
} | |
} | |
val zoo = Zoo(Dog()) | |
Json(context = runnableModule).stringify(Zoo.serializer() , zoo).also { | |
logger.info("zoo = {}" , it) | |
} | |
} | |
} | |
// 23:56:42.407 [main] INFO RunnableTest - zoo = {"runnable":{"type":"Dog"}} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment