summaryrefslogtreecommitdiff
path: root/runtime/jvmTest/src/kotlinx/serialization/features/ResolvingTest.kt
blob: b369a0deda30740d93a3f038435b56111b599a6a (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
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
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
/*
 * Copyright 2018 JetBrains s.r.o.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package kotlinx.serialization.features

import kotlinx.serialization.*
import kotlinx.serialization.internal.IntDescriptor
import kotlinx.serialization.internal.IntSerializer
import kotlinx.serialization.json.Json
import org.junit.Test
import java.lang.reflect.ParameterizedType
import java.lang.reflect.Type
import kotlin.test.assertEquals
import kotlin.test.assertSame

class ResolvingTest {
    @Serializable
    data class Box<out T>(val a: T)

    @Serializable
    data class Data(val l: List<String>, val b: Box<Int>)

    @Serializable
    data class WithCustomDefault(val n: Int) {
        @Serializer(forClass = WithCustomDefault::class)
        companion object {
            override val descriptor: SerialDescriptor = IntDescriptor.withName("WithCustomDefault")
            override fun serialize(encoder: Encoder, obj: WithCustomDefault) = encoder.encodeInt(obj.n)
            override fun deserialize(decoder: Decoder) = WithCustomDefault(decoder.decodeInt())
        }
    }

    @Test
    fun primitiveDescriptorWithNameTest() {
        val desc = WithCustomDefault.serializer().descriptor
        assertEquals("WithCustomDefault", desc.name)
        assertSame(PrimitiveKind.INT, desc.kind)
        assertEquals(0, desc.elementsCount)
    }

    object IntBoxToken : ParameterizedType {
        override fun getRawType() = Box::class.java
        override fun getOwnerType() = null
        override fun getActualTypeArguments(): Array<Type> = arrayOf(Int::class.java)
    }

    @Test
    fun intBoxTest() {
        val b = Box(42)
        val serial = serializerByTypeToken(IntBoxToken)
        val s = Json.unquoted.stringify(serial, b)
        assertEquals("{a:42}", s)
    }

    @Test
    fun testArrayResolving() {
        val myArr = arrayOf("a", "b", "c")
        val token = myArr::class.java
        val serial = serializerByTypeToken(token)
        val s = Json.unquoted.stringify(serial, myArr)
        assertEquals("[a,b,c]", s)
    }

    @Test
    fun testListResolving() {
        val myArr = listOf("a", "b", "c")
        val token = object : ParameterizedType {
            override fun getRawType(): Type = List::class.java
            override fun getOwnerType(): Type? = null
            override fun getActualTypeArguments(): Array<Type> = arrayOf(String::class.java)
        }
        val serial = serializerByTypeToken(token)
        val s = Json.unquoted.stringify(serial, myArr)
        assertEquals("[a,b,c]", s)
    }


    @Test
    fun testReifiedArrayResolving() {
        val myArr = arrayOf("a", "b", "c")
        val token = typeTokenOf<Array<String>>()
        val serial = serializerByTypeToken(token)
        val s = Json.unquoted.stringify(serial, myArr)
        assertEquals("[a,b,c]", s)
    }

    @Test
    fun testReifiedListResolving() {
        val myList = listOf("a", "b", "c")
        val token = typeTokenOf<List<String>>()
        val serial = serializerByTypeToken(token)
        val s = Json.unquoted.stringify(serial, myList)
        assertEquals("[a,b,c]", s)
    }

    @Test
    fun testReifiedSetResolving() {
        val mySet = setOf("a", "b", "c", "c")
        val token = typeTokenOf<Set<String>>()
        val serial = serializerByTypeToken(token)
        val s = Json.unquoted.stringify(serial, mySet)
        assertEquals("[a,b,c]", s)
    }

    @Test
    fun testReifiedMapResolving() {
        val myMap = mapOf("a" to Data(listOf("c"), Box(6)))
        val token = typeTokenOf<Map<String, Data>>()
        val serial = serializerByTypeToken(token)
        val s = Json.unquoted.stringify(serial, myMap)
        assertEquals("{a:{l:[c],b:{a:6}}}", s)
    }

    @Test
    fun testNestedListResolving() {
        val myList = listOf(listOf(listOf(1, 2, 3)), listOf())
        val token = typeTokenOf<List<List<List<Int>>>>()
        val serial = serializerByTypeToken(token)
        val s = Json.unquoted.stringify(serial, myList)
        assertEquals("[[[1,2,3]],[]]", s)
    }

    @Test
    fun testNestedArrayResolving() {
        val myList = arrayOf(arrayOf(arrayOf(1, 2, 3)), arrayOf())
        val token = typeTokenOf<Array<Array<Array<Int>>>>()
        val serial = serializerByTypeToken(token)
        val s = Json.unquoted.stringify(serial, myList)
        assertEquals("[[[1,2,3]],[]]", s)
    }

    @Test
    fun testNestedMixedResolving() {
        val myList = arrayOf(listOf(arrayOf(1, 2, 3)), listOf())
        val token = typeTokenOf<Array<List<Array<Int>>>>()
        val serial = serializerByTypeToken(token)
        val s = Json.unquoted.stringify(serial, myList)
        assertEquals("[[[1,2,3]],[]]", s)
    }

    @Test
    fun objectTest() {
        val b = Data(listOf("a", "b", "c"), Box(42))
        val serial = serializerByTypeToken(Data::class.java)
        val s = Json.unquoted.stringify(serial, b)
        assertEquals("{l:[a,b,c],b:{a:42}}", s)
    }

    @Test
    fun customDefault() {
        val foo = Json.unquoted.parse<WithCustomDefault>("9")
        assertEquals(9, foo.n)
    }

    @Serializable
    data class WithNamedCompanion(val a: Int) {
        companion object Named
    }

    @Test
    fun namedCompanionTest() {
        val namedCompanion = WithNamedCompanion(1)
        val serial = serializerByTypeToken(WithNamedCompanion::class.java)
        val s = Json.unquoted.stringify(serial, namedCompanion)
        assertEquals("{a:1}", s)
    }

    @Test
    fun intResolve() {
        val token = typeTokenOf<Int>()
        val serial = serializerByTypeToken(token)
        assertSame(IntSerializer as KSerializer<*>, serial)
    }
}