All Downloads are FREE. Search and download functionalities are using the official Maven repository.

commonTest.kotlinx.serialization.json.JsonNumericKeysTest.kt Maven / Gradle / Ivy

There is a newer version: 1.7.3
Show newest version
/*
 * 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)

    @Serializable
    data class MapWrapper(val m: Map)

    @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 {
        val e = default.decodeFromString(MapEntrySerializer(Int.serializer(), Int.serializer()), """{"1":2}""")
        assertEquals(1, e.key)
        assertEquals(2, e.value)
        return e
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy