-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathTagged2.kt
104 lines (88 loc) · 2.85 KB
/
Tagged2.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
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
package net.sarazan.martok
import kotlinx.serialization.KSerializer
import kotlinx.serialization.SerialName
import kotlinx.serialization.Serializable
import kotlinx.serialization.descriptors.PrimitiveKind
import kotlinx.serialization.descriptors.PrimitiveSerialDescriptor
import kotlinx.serialization.descriptors.SerialDescriptor
import kotlinx.serialization.encoding.Decoder
import kotlinx.serialization.encoding.Encoder
import kotlinx.serialization.json.JsonContentPolymorphicSerializer
import kotlinx.serialization.json.JsonElement
import kotlinx.serialization.json.JsonObject
import kotlinx.serialization.json.JsonPrimitive
import kotlinx.serialization.json.jsonObject
@Serializable
data class HasId(
val id: String
)
@Serializable(with = IntersectionFirst.UnionSerializer::class)
sealed class IntersectionFirst {
abstract val id: String
abstract val type: Type
@Serializable
enum class Type(
val serialName: String
) {
@SerialName("foo") FOO("foo"),
@SerialName("bar") BAR("bar");
}
@Serializable
data class IntersectionFirstFoo(
override val id: String,
val foo: String
) : IntersectionFirst() {
override val type: Type = Type.FOO
}
@Serializable
data class IntersectionFirstBar(
override val id: String,
val bar: String
) : IntersectionFirst() {
override val type: Type = Type.BAR
}
object UnionSerializer : JsonContentPolymorphicSerializer<IntersectionFirst>(IntersectionFirst::class) {
override fun selectDeserializer(element: JsonElement) = when(
val type = element.jsonObject["type"]
) {
JsonPrimitive(Type.FOO.serialName) -> IntersectionFirstFoo.serializer()
JsonPrimitive(Type.BAR.serialName) -> IntersectionFirstBar.serializer()
else -> throw IllegalArgumentException("Unexpected type: $type")
}
}
}
@Serializable(with = UnionFirst.UnionSerializer::class)
sealed class UnionFirst {
abstract val id: String
abstract val type: Type
@Serializable
enum class Type(
val serialName: String
) {
@SerialName("foo") FOO("foo"),
@SerialName("bar") BAR("bar");
}
@Serializable
data class UnionFirstFoo(
val foo: String,
override val id: String
) : UnionFirst() {
override val type: Type = Type.FOO
}
@Serializable
data class UnionFirstBar(
val bar: Double,
override val id: String
) : UnionFirst() {
override val type: Type = Type.BAR
}
object UnionSerializer : JsonContentPolymorphicSerializer<UnionFirst>(UnionFirst::class) {
override fun selectDeserializer(element: JsonElement) = when(
val type = element.jsonObject["type"]
) {
JsonPrimitive(Type.FOO.serialName) -> UnionFirstFoo.serializer()
JsonPrimitive(Type.BAR.serialName) -> UnionFirstBar.serializer()
else -> throw IllegalArgumentException("Unexpected type: $type")
}
}
}