| // This file was automatically generated from polymorphism.md by Knit tool. Do not edit. |
| package example.examplePoly16 |
| |
| import kotlinx.serialization.* |
| import kotlinx.serialization.json.* |
| |
| import kotlinx.serialization.modules.* |
| import kotlin.reflect.KClass |
| |
| val module = SerializersModule { |
| fun PolymorphicModuleBuilder<Project>.registerProjectSubclasses() { |
| subclass(OwnedProject::class) |
| } |
| polymorphic(Any::class) { registerProjectSubclasses() } |
| polymorphic(Project::class) { registerProjectSubclasses() } |
| } |
| |
| val format = Json { serializersModule = module } |
| |
| interface Project { |
| val name: String |
| } |
| |
| @Serializable |
| @SerialName("owned") |
| class OwnedProject(override val name: String, val owner: String) : Project |
| |
| @Serializable |
| class Data( |
| val project: Project, |
| @Polymorphic val any: Any |
| ) |
| |
| fun main() { |
| val project = OwnedProject("kotlinx.coroutines", "kotlin") |
| val data = Data(project, project) |
| println(format.encodeToString(data)) |
| } |