summaryrefslogtreecommitdiff
path: root/formats/json-tests/jvmTest/src/kotlinx/serialization/features/SerializerByTypeTest.kt
blob: a600b9d71948ab9732328d7c88d1500872960976 (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
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
/*
 * Copyright 2017-2021 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license.
 */

package kotlinx.serialization.features

import kotlinx.serialization.*
import kotlinx.serialization.builtins.*
import kotlinx.serialization.descriptors.*
import kotlinx.serialization.encoding.*
import kotlinx.serialization.json.*
import kotlinx.serialization.modules.*
import kotlinx.serialization.test.*
import org.junit.Test
import java.lang.reflect.*
import kotlin.reflect.*
import kotlin.test.*
import kotlin.time.*

class SerializerByTypeTest {

    private val json = Json

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

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

    @Serializable(WithCustomDefault.Companion::class)
    data class WithCustomDefault(val n: Int) {

        companion object: KSerializer<WithCustomDefault> {
            override val descriptor: SerialDescriptor = PrimitiveSerialDescriptor("WithCustomDefault", PrimitiveKind.INT)
            override fun serialize(encoder: Encoder, value: WithCustomDefault) = encoder.encodeInt(value.n)
            override fun deserialize(decoder: Decoder) = WithCustomDefault(decoder.decodeInt())
        }
    }

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

    @Serializable
    object SerializableObject

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

    @Test
    fun testGenericParameter() {
        val b = Box(42)
        assertSerializedWithType(IntBoxToken, """{"a":42}""", b)
    }

    @Test
    fun testNestedGenericParameter() {
        val b = Box(Box(239))
        assertSerializedWithType(typeTokenOf<Box<Box<Int>>>(), """{"a":{"a":239}}""", b)
    }

    @Test
    fun testArray() {
        val myArr = arrayOf("a", "b", "c")
        val token = myArr::class.java
        assertSerializedWithType(token, """["a","b","c"]""", myArr)
    }

    @Test
    fun testList() {
        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)
        }
        assertSerializedWithType(token, """["a","b","c"]""", myArr)
    }

    @Test
    fun testListAsCollection() {
        val myArr: Collection<String> = listOf("a", "b", "c")
        val token = object : ParameterizedType {
            override fun getRawType(): Type = Collection::class.java
            override fun getOwnerType(): Type? = null
            override fun getActualTypeArguments(): Array<Type> = arrayOf(String::class.java)
        }
        assertSerializedWithType(token, """["a","b","c"]""", myArr)
    }


    @Test
    fun testReifiedArrayResolving() {
        val myArr = arrayOf("a", "b", "c")
        val token = typeTokenOf<Array<String>>()
        assertSerializedWithType(token, """["a","b","c"]""", myArr)
    }

    @Test
    fun testPrimitiveArrayResolving() {
        val myArr = intArrayOf(1, 2, 3)
        val token = IntArray::class.java
        val name = serializer(token).descriptor.serialName
        assertTrue(name.contains("IntArray"))
        assertSerializedWithType(token, """[1,2,3]""", myArr)
    }

    @Test
    fun testReifiedListResolving() {
        val myList = listOf("a", "b", "c")
        val token = typeTokenOf<List<String>>()
        assertSerializedWithType(token, """["a","b","c"]""", myList)
    }

    @Test
    fun testReifiedSetResolving() {
        val mySet = setOf("a", "b", "c", "c")
        val token = typeTokenOf<Set<String>>()
        assertSerializedWithType(token, """["a","b","c"]""", mySet)
    }

    @Test
    fun testReifiedMapResolving() {
        val myMap = mapOf("a" to Data(listOf("c"), Box(6)))
        val token = typeTokenOf<Map<String, Data>>()
        assertSerializedWithType(token, """{"a":{"l":["c"],"b":{"a":6}}}""",myMap)
    }

    @Test
    fun testNestedListResolving() {
        val myList = listOf(listOf(listOf(1, 2, 3)), listOf())
        val token = typeTokenOf<List<List<List<Int>>>>()
        assertSerializedWithType(token, "[[[1,2,3]],[]]", myList)
    }

    @Test
    fun testNestedArrayResolving() {
        val myList = arrayOf(arrayOf(arrayOf(1, 2, 3)), arrayOf())
        val token = typeTokenOf<Array<Array<Array<Int>>>>()
        assertSerializedWithType(token, "[[[1,2,3]],[]]", myList)
    }

    @Test
    fun testNestedMixedResolving() {
        val myList = arrayOf(listOf(arrayOf(1, 2, 3)), listOf())
        val token = typeTokenOf<Array<List<Array<Int>>>>()
        assertSerializedWithType(token, "[[[1,2,3]],[]]", myList)
    }

    @Test
    fun testPair() {
        val myPair = "42" to 42
        val token = typeTokenOf<Pair<String, Int>>()
        assertSerializedWithType(token, """{"first":"42","second":42}""", myPair)
    }

    @Test
    fun testTriple() {
        val myTriple = Triple("1", 2, Box(42))
        val token = typeTokenOf<Triple<String, Int, Box<Int>>>()
        assertSerializedWithType(token, """{"first":"1","second":2,"third":{"a":42}}""", myTriple)
    }

    @Test
    fun testGenericInHolder() {
        val b = Data(listOf("a", "b", "c"), Box(42))
        assertSerializedWithType(Data::class.java,"""{"l":["a","b","c"],"b":{"a":42}}""", b )
    }

    @Test
    fun testOverriddenSerializer() {
        val foo = json.decodeFromString<WithCustomDefault>("9")
        assertEquals(9, foo.n)
    }

    @Test
    fun testNamedCompanion() {
        val namedCompanion = WithNamedCompanion(1)
        assertSerializedWithType(WithNamedCompanion::class.java, """{"a":1}""", namedCompanion)
    }

    @Test
    fun testPrimitive() {
        val token = typeTokenOf<Int>()
        val serial = serializer(token)
        assertSame(Int.serializer() as KSerializer<*>, serial)
    }

    @Test
    fun testObject() {
        val token = typeTokenOf<SerializableObject>()
        val serial = serializer(token)
        assertEquals(SerializableObject.serializer().descriptor, serial.descriptor)
    }

    @Suppress("UNCHECKED_CAST")
    private fun <T> assertSerializedWithType(
        token: Type,
        expected: String,
        value: T,
    ) {
        val serial = serializer(token) as KSerializer<T>
        assertEquals(expected, json.encodeToString(serial, value))
        val serial2 = requireNotNull(serializerOrNull(token)) { "Expected serializer to be found" }
        assertEquals(expected, json.encodeToString(serial2 as KSerializer<T>, value))
    }

    class IntBox(val i: Int)

    object CustomIntSerializer : KSerializer<IntBox> {
        override val descriptor: SerialDescriptor = PrimitiveSerialDescriptor("CIS", PrimitiveKind.INT)

        override fun serialize(encoder: Encoder, value: IntBox) {
            encoder.encodeInt(42)
        }

        override fun deserialize(decoder: Decoder): IntBox {
            TODO()
        }
    }

    @Test
    fun testContextualLookup() {
        val module = SerializersModule { contextual(CustomIntSerializer) }
        val serializer = module.serializer(typeTokenOf<List<List<IntBox>>>())
        assertEquals("[[42]]", Json.encodeToString(serializer, listOf(listOf(IntBox(1)))))
    }

    @Test
    fun testCompiledWinsOverContextual() {
        val contextual = object : KSerializer<Int> {
            override val descriptor: SerialDescriptor = Int.serializer().descriptor

            override fun serialize(encoder: Encoder, value: Int) {
                fail()
            }

            override fun deserialize(decoder: Decoder): Int {
                fail()
            }
        }
        val module = SerializersModule { contextual(contextual) }
        val serializer = module.serializer(typeTokenOf<List<List<Int>>>())
        assertEquals("[[1]]", Json.encodeToString(serializer, listOf(listOf<Int>(1))))
        assertEquals("42", Json.encodeToString(module.serializer(typeTokenOf<Int>()), 42))
    }

    class NonSerializable

    class NonSerializableBox<T>(val boxed: T)

    @Test
    fun testLookupFail() {
        assertNull(serializerOrNull(typeTokenOf<NonSerializable>()))
        assertNull(serializerOrNull(typeTokenOf<NonSerializableBox<String>>()))
        assertNull(serializerOrNull(typeTokenOf<Box<NonSerializable>>()))

        assertFailsWithMessage<SerializationException>("for class 'NonSerializable'") {
            serializer(typeTokenOf<NonSerializable>())
        }

        assertFailsWithMessage<SerializationException>("for class 'NonSerializableBox'") {
            serializer(typeTokenOf<NonSerializableBox<String>>())
        }

        assertFailsWithMessage<SerializationException>("for class 'NonSerializable'") {
            serializer(typeTokenOf<kotlinx.serialization.Box<NonSerializable>>())
        }

        assertFailsWithMessage<SerializationException>("for class 'NonSerializable'") {
            serializer(typeTokenOf<Array<NonSerializable>>())
        }
    }

    @OptIn(ExperimentalTime::class)
    @Test
    fun testSerializersAreIntrinsified() {
        val direct = measureTime {
            Json.encodeToString(IntData.serializer(), IntData(10))
        }
        val directMs = direct.inWholeMicroseconds
        val indirect = measureTime {
            Json.encodeToString(IntData(10))
        }
        val indirectMs = indirect.inWholeMicroseconds
        if (indirectMs > directMs + (directMs / 4)) error("Direct ($directMs) and indirect ($indirectMs) times are too far apart")
    }
}