summaryrefslogtreecommitdiff
path: root/formats/json/commonTest/src/kotlinx/serialization/features/inline/InlineClassesTest.kt
blob: 667094d454dea6077d4ec089e89b5c8897f0d54f (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
/*
 * Copyright 2017-2021 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license.
 */

@file:Suppress("INLINE_CLASSES_NOT_SUPPORTED", "SERIALIZER_NOT_FOUND")
@file:OptIn(ExperimentalUnsignedTypes::class)

/*
 * Copyright 2017-2019 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license.
 */

package kotlinx.serialization.features.inline

import kotlinx.serialization.*
import kotlinx.serialization.builtins.*
import kotlinx.serialization.encoding.*
import kotlinx.serialization.json.*
import kotlinx.serialization.test.*
import kotlin.jvm.*
import kotlin.test.*

@Serializable
data class SimpleContainerForUInt(val i: UInt)

@Serializable(MyUIntSerializer::class)
@JvmInline
value class MyUInt(val m: Int)

@Serializer(forClass = MyUInt::class)
object MyUIntSerializer {
    override val descriptor = UInt.serializer().descriptor
    override fun serialize(encoder: Encoder, value: MyUInt) {
        encoder.encodeInline(descriptor).encodeInt(value.m)
    }

    override fun deserialize(decoder: Decoder): MyUInt {
        return MyUInt(decoder.decodeInline(descriptor).decodeInt())
    }
}

@Serializable
data class SimpleContainerForMyType(val i: MyUInt)

@Serializable
@JvmInline
value class MyList<T>(val list: List<T>)

@Serializable
data class ContainerForList<T>(val i: MyList<T>)

@Serializable
data class UnsignedInBoxedPosition(val i: List<UInt>)

@Serializable
data class MixedPositions(
    val int: Int,
    val intNullable: Int?,
    val uint: UInt,
    val uintNullable: UInt?,
    val boxedInt: List<Int>,
    val boxedUInt: List<UInt>,
    val boxedNullableInt: List<Int?>,
    val boxedNullableUInt: List<UInt?>
)

@Serializable
@JvmInline
value class ResourceId(val id: String)

@Serializable
@JvmInline
value class ResourceType(val type: String)

@Serializable
data class ResourceIdentifier(val id: ResourceId, val type: ResourceType)

class InlineClassesTest : JsonTestBase() {
    private val precedent: UInt = Int.MAX_VALUE.toUInt() + 10.toUInt()

    @Test
    fun testSimpleContainer() = noLegacyJs {
        assertJsonFormAndRestored(
            SimpleContainerForUInt.serializer(),
            SimpleContainerForUInt(precedent),
            """{"i":2147483657}""",
        )
    }

    @Test
    fun testSimpleContainerForMyTypeWithCustomSerializer() = assertJsonFormAndRestored(
        SimpleContainerForMyType.serializer(),
        SimpleContainerForMyType(MyUInt(precedent.toInt())),
        """{"i":2147483657}""",
    )

    @Test
    fun testSimpleContainerForList() = noLegacyJs {
        assertJsonFormAndRestored(
            ContainerForList.serializer(UInt.serializer()),
            ContainerForList(MyList(listOf(precedent))),
            """{"i":[2147483657]}""",
        )
    }

    @Test
    fun testInlineClassesWithStrings() = noLegacyJs {
        assertJsonFormAndRestored(
            ResourceIdentifier.serializer(),
            ResourceIdentifier(ResourceId("resId"), ResourceType("resType")),
            """{"id":"resId","type":"resType"}"""
        )
    }

    @Test
    fun testUnsignedInBoxedPosition() = assertJsonFormAndRestored(
        UnsignedInBoxedPosition.serializer(),
        UnsignedInBoxedPosition(listOf(precedent)),
        """{"i":[2147483657]}""",
    )

    @Test
    fun testMixedPositions() {
        val o = MixedPositions(
            int = precedent.toInt(),
            intNullable = precedent.toInt(),
            uint = precedent,
            uintNullable = precedent,
            boxedInt = listOf(precedent.toInt()),
            boxedUInt = listOf(precedent),
            boxedNullableInt = listOf(null, precedent.toInt(), null),
            boxedNullableUInt = listOf(null, precedent, null)
        )
        assertJsonFormAndRestored(
            MixedPositions.serializer(),
            o,
            """{"int":-2147483639,"intNullable":-2147483639,"uint":2147483657,"uintNullable":2147483657,"boxedInt":[-2147483639],"boxedUInt":[2147483657],"boxedNullableInt":[null,-2147483639,null],"boxedNullableUInt":[null,2147483657,null]}""",
        )
    }
}