-
Notifications
You must be signed in to change notification settings - Fork 0
/
Anonymous.kt
106 lines (97 loc) · 2.57 KB
/
Anonymous.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
105
106
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 AnonList(
val foo: List<FooItem>
) {
@Serializable
data class FooItem(
val bar: String,
val baz: Baz
) {
@Serializable
enum class Baz(
val serialName: String
) {
@SerialName("one") ONE("one"),
@SerialName("two") TWO("two");
}
}
}
@Serializable
data class NumberUnion(
val foo: Foo
) {
@Serializable(with = Foo.Companion::class)
enum class Foo(
val value: Int
) {
_1(1),
_2(2);
companion object : KSerializer<Foo> {
override val descriptor: SerialDescriptor = PrimitiveSerialDescriptor("net.sarazan.martok.NumberUnion.Foo", PrimitiveKind.INT)
override fun deserialize(decoder: Decoder) = when (val value = decoder.decodeInt()) {
1 -> _1
2 -> _2
else -> throw IllegalArgumentException("Foo could not parse: $value")
}
override fun serialize(encoder: Encoder, value: Foo) {
return encoder.encodeInt(value.value)
}
}
}
}
@Serializable
data class StringUnion(
val foo: Foo
) {
@Serializable
enum class Foo(
val serialName: String
) {
@SerialName("barBar") BAR_BAR("barBar"),
@SerialName("bazBaz") BAZ_BAZ("bazBaz");
}
}
@Serializable
data class NumberStringUnion(
val foo: Foo
) {
@Serializable
enum class Foo(
val serialName: String
) {
@SerialName("1.1") _1_1("1.1"),
@SerialName("2") _2("2");
}
}
@Serializable
data class Nested(
val foo: Foo
) {
@Serializable
data class Foo(
val bar: String,
val baz: Baz
) {
@Serializable
enum class Baz(
val serialName: String
) {
@SerialName("one") ONE("one"),
@SerialName("two") TWO("two");
}
}
}