jsTest.kotlinx.serialization.json.DecodeFromDynamicSpecialCasesTest.kt Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of kotlinx-serialization-json
Show all versions of kotlinx-serialization-json
Kotlin multiplatform serialization runtime library
/*
* 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 kotlin.test.*
class DecodeFromDynamicSpecialCasesTest {
@Test
fun testTopLevelInt() {
val dyn = js("42")
val parsed = Json.decodeFromDynamic(dyn)
assertEquals(42, parsed)
}
@Test
fun testTopLevelString() {
val dyn = js(""""42"""")
val parsed = Json.decodeFromDynamic(dyn)
assertEquals("42", parsed)
}
@Test
fun testTopLevelList() {
val dyn = js("[1, 2, 3]")
val parsed = Json.decodeFromDynamic>(dyn)
assertEquals(listOf(1, 2, 3), parsed)
}
@Test
fun testStringMap() = testMapWithPrimitiveKey("1", "2")
@Test
fun testByteMap() = testMapWithPrimitiveKey(1.toByte(), 2.toByte())
@Test
fun testCharMap() = testMapWithPrimitiveKey('1', '2')
@Test
fun testShortMap() = testMapWithPrimitiveKey(1.toShort(), 2.toShort())
@Test
fun testIntMap() = testMapWithPrimitiveKey(1, 2)
@Test
fun testLongMap() = testMapWithPrimitiveKey(1L, 2L)
@Test
fun testDoubleMap() = testMapWithPrimitiveKey(1.0, 2.0)
@Test
fun testFloatMap() = testMapWithPrimitiveKey(1.0f, 2.0f)
private inline fun testMapWithPrimitiveKey(k1: T, k2: T) {
val map = mapOf(k1 to 3, k2 to 4)
val dyn = js("{1:3, 2:4}")
val parsed = Json.decodeFromDynamic