-
Notifications
You must be signed in to change notification settings - Fork 620
/
example-poly-16.kt
38 lines (30 loc) · 997 Bytes
/
example-poly-16.kt
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
// 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))
}