blob: e47f57904b81dc00351a184e189754a4f66f69b2 (
plain)
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
|
/*
* Copyright 2017-2020 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license.
*/
package kotlinx.serialization.json.polymorphic
import kotlinx.serialization.*
import kotlinx.serialization.json.*
import kotlinx.serialization.modules.*
import kotlin.test.*
class JsonPolymorphicObjectTest : JsonTestBase() {
@Serializable
data class Holder(@Polymorphic val a: Any)
@Serializable
@SerialName("MyObject")
object MyObject {
@Suppress("unused")
val unused = 42
}
val json = Json {
serializersModule = SerializersModule {
polymorphic(Any::class) {
subclass(MyObject::class, MyObject.serializer()) // JS bug workaround
}
}
}
@Test
fun testRegularPolymorphism() {
assertJsonFormAndRestored(Holder.serializer(), Holder(MyObject), """{"a":{"type":"MyObject"}}""", json)
}
@Test
fun testArrayPolymorphism() {
val json = Json(from = json) {
useArrayPolymorphism = true
}
assertJsonFormAndRestored(Holder.serializer(), Holder(MyObject), """{"a":["MyObject",{}]}""", json)
}
}
|