summaryrefslogtreecommitdiff
path: root/formats/json-tests/commonTest/src/kotlinx/serialization/json/JsonNumericKeysTest.kt
blob: ee3e8f150ae543486ce0793467f78761c679dd44 (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
/*
 * Copyright 2017-2020 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.serializer
import kotlin.test.*

class JsonNumericKeysTest : JsonTestBase() {
    @Serializable
    data class EntryWrapper(val e: Map.Entry<Int, Int>)

    @Serializable
    data class MapWrapper(val m: Map<Int, Int>)

    @Test
    fun testIntegerKeyInTopLevelEntry() {
        assertJsonFormAndRestored(MapEntrySerializer(Int.serializer(), Int.serializer()), getEntry(), """{"1":2}""")
    }

    @Test
    fun testIntegerKeyInEntry() {
        assertJsonFormAndRestored(EntryWrapper.serializer(), EntryWrapper(getEntry()), """{"e":{"1":2}}""")
    }

    @Test
    fun testIntegerKeyInTopLevelMap() = parametrizedTest {
        assertJsonFormAndRestored(serializer(), mapOf(1 to 2), """{"1":2}""")

    }

    @Test
    fun testIntegerKeyInMap() = parametrizedTest {
        assertJsonFormAndRestored(MapWrapper.serializer(), MapWrapper(mapOf(1 to 2)), """{"m":{"1":2}}""")
    }

    // Workaround equals on JS and Native
    fun getEntry(): Map.Entry<Int, Int> {
        val e = default.decodeFromString(MapEntrySerializer(Int.serializer(), Int.serializer()), """{"1":2}""")
        assertEquals(1, e.key)
        assertEquals(2, e.value)
        return e
    }
}