summaryrefslogtreecommitdiff
path: root/formats/json-tests/commonTest/src/kotlinx/serialization/json/JsonMapKeysTest.kt
blob: 560e51feaeaecb06d7792d50f0d915ba94855afa (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
/*
 * Copyright 2017-2019 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license.
 */

package kotlinx.serialization.json

import kotlinx.serialization.*
import kotlinx.serialization.builtins.*
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.modules.SerializersModule
import kotlinx.serialization.test.*
import kotlin.jvm.*
import kotlin.test.*

@JvmInline
@Serializable
value class ComplexCarrier(val c: IntData)

@JvmInline
@Serializable
value class PrimitiveCarrier(val c: String)

data class ContextualValue(val c: String) {
    companion object: KSerializer<ContextualValue> {
        override val descriptor: SerialDescriptor = PrimitiveSerialDescriptor("ContextualValue", PrimitiveKind.STRING)

        override fun serialize(encoder: Encoder, value: ContextualValue) {
            encoder.encodeString(value.c)
        }

        override fun deserialize(decoder: Decoder): ContextualValue {
            return ContextualValue(decoder.decodeString())
        }
    }
}

class JsonMapKeysTest : JsonTestBase() {
    @Serializable
    private data class WithMap(val map: Map<Long, Long>)

    @Serializable
    private data class WithBooleanMap(val map: Map<Boolean, Boolean>)

    @Serializable
    private data class WithValueKeyMap(val map: Map<PrimitiveCarrier, Long>)

    @Serializable
    private data class WithEnum(val map: Map<SampleEnum, Long>)

    @Serializable
    private data class WithComplexKey(val map: Map<IntData, String>)

    @Serializable
    private data class WithComplexValueKey(val map: Map<ComplexCarrier, String>)

    @Serializable
    private data class WithContextualValueKey(val map: Map<@Contextual PrimitiveCarrier, Long>)

    @Serializable
    private data class WithContextualKey(val map: Map<@Contextual ContextualValue, Long>)

    @Test
    fun testMapKeysSupportNumbers() = parametrizedTest {
        assertStringFormAndRestored(
            """{"map":{"10":10,"20":20}}""",
            WithMap(mapOf(10L to 10L, 20L to 20L)),
            WithMap.serializer(),
            default
        )
    }

    @Test
    fun testMapKeysSupportBooleans() = parametrizedTest {
        assertStringFormAndRestored(
            """{"map":{"true":false,"false":true}}""",
            WithBooleanMap(mapOf(true to false, false to true)),
            WithBooleanMap.serializer(),
            default
        )
    }

    // As a result of quoting ignorance when parsing primitives, it is possible to parse unquoted maps if Kotlin keys are non-string primitives.
    // This is not spec-compliant, but I do not see any problems with it.
    @Test
    fun testMapDeserializesUnquotedKeys() = parametrizedTest {
        assertEquals(WithMap(mapOf(10L to 10L, 20L to 20L)), default.decodeFromString("""{"map":{10:10,20:20}}"""))
        assertEquals(
            WithBooleanMap(mapOf(true to false, false to true)),
            default.decodeFromString("""{"map":{true:false,false:true}}""")
        )
        assertFailsWithSerial("JsonDecodingException") {
            default.decodeFromString(
                MapSerializer(
                    String.serializer(),
                    Boolean.serializer()
                ),"""{"map":{true:false,false:true}}"""
            )
        }
    }

    @Test
    fun testStructuredMapKeysShouldBeProhibitedByDefault() = parametrizedTest { streaming ->
        verifyProhibition(WithComplexKey(mapOf(IntData(42) to "42")), streaming)
        verifyProhibition(WithComplexValueKey(mapOf(ComplexCarrier(IntData(42)) to "42")), streaming)
    }

    private inline fun <reified T: Any> verifyProhibition(value: T, streaming: JsonTestingMode) {
        assertFailsWithSerialMessage("JsonEncodingException", "can't be used in JSON as a key in the map") {
            Json.encodeToString(value, streaming)
        }
    }

    @Test
    fun testStructuredMapKeysAllowedWithFlag() = assertJsonFormAndRestored(
        WithComplexKey.serializer(),
        WithComplexKey(mapOf(IntData(42) to "42")),
        """{"map":[{"intV":42},"42"]}""",
        Json { allowStructuredMapKeys = true }
    )

    @Test
    fun testStructuredValueMapKeysAllowedWithFlag() {
        assertJsonFormAndRestored(
            WithComplexValueKey.serializer(),
            WithComplexValueKey(mapOf(ComplexCarrier(IntData(42)) to "42")),
            """{"map":[{"intV":42},"42"]}""",
            Json { allowStructuredMapKeys = true }
        )
    }

    @Test
    fun testEnumsAreAllowedAsMapKeys() = assertJsonFormAndRestored(
        WithEnum.serializer(),
        WithEnum(mapOf(SampleEnum.OptionA to 1L, SampleEnum.OptionC to 3L)),
        """{"map":{"OptionA":1,"OptionC":3}}""",
        Json
    )

    @Test
    fun testPrimitivesAreAllowedAsValueMapKeys() {
        assertJsonFormAndRestored(
            WithValueKeyMap.serializer(),
            WithValueKeyMap(mapOf(PrimitiveCarrier("fooKey") to 1)),
            """{"map":{"fooKey":1}}""",
            Json
        )
    }

    @Test
    fun testContextualValuePrimitivesAreAllowedAsValueMapKeys() {
        assertJsonFormAndRestored(
            WithContextualValueKey.serializer(),
            WithContextualValueKey(mapOf(PrimitiveCarrier("fooKey") to 1)),
            """{"map":{"fooKey":1}}""",
            Json {
                serializersModule =
                    SerializersModule { contextual(PrimitiveCarrier::class, PrimitiveCarrier.serializer()) }
            }
        )
    }

    @Test
    fun testContextualPrimitivesAreAllowedAsValueMapKeys() {
        assertJsonFormAndRestored(
            WithContextualKey.serializer(),
            WithContextualKey(mapOf(ContextualValue("fooKey") to 1)),
            """{"map":{"fooKey":1}}""",
            Json {
                serializersModule = SerializersModule { contextual(ContextualValue::class, ContextualValue) }
            }
        )
    }
}