1 /* 2 * Copyright 2017-2020 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license. 3 */ 4 5 package kotlinx.serialization.json 6 7 import kotlinx.serialization.* 8 import kotlin.test.* 9 10 class DecodeFromDynamicSpecialCasesTest { 11 12 @Test testTopLevelIntnull13 fun testTopLevelInt() { 14 val dyn = js("42") 15 val parsed = Json.decodeFromDynamic<Int>(dyn) 16 assertEquals(42, parsed) 17 } 18 19 @Test testTopLevelStringnull20 fun testTopLevelString() { 21 val dyn = js(""""42"""") 22 val parsed = Json.decodeFromDynamic<String>(dyn) 23 assertEquals("42", parsed) 24 } 25 26 @Test 27 fun testTopLevelList() { 28 val dyn = js("[1, 2, 3]") 29 val parsed = Json.decodeFromDynamic<List<Int>>(dyn) 30 assertEquals(listOf(1, 2, 3), parsed) 31 } 32 33 @Test 34 fun testStringMap() = testMapWithPrimitiveKey("1", "2") 35 36 @Test 37 fun testByteMap() = testMapWithPrimitiveKey(1.toByte(), 2.toByte()) 38 39 @Test 40 fun testCharMap() = testMapWithPrimitiveKey('1', '2') 41 42 @Test 43 fun testShortMap() = testMapWithPrimitiveKey(1.toShort(), 2.toShort()) 44 45 @Test 46 fun testIntMap() = testMapWithPrimitiveKey(1, 2) 47 48 @Test 49 fun testLongMap() = testMapWithPrimitiveKey(1L, 2L) 50 51 @Test 52 fun testDoubleMap() = testMapWithPrimitiveKey(1.0, 2.0) 53 54 @Test 55 fun testFloatMap() = testMapWithPrimitiveKey(1.0f, 2.0f) 56 57 private inline fun <reified T> testMapWithPrimitiveKey(k1: T, k2: T) { 58 val map = mapOf(k1 to 3, k2 to 4) 59 val dyn = js("{1:3, 2:4}") 60 val parsed = Json.decodeFromDynamic<Map<T, Int>>(dyn) 61 assertEquals(map, parsed) 62 } 63 64 @Test 65 fun testJsonPrimitive() { 66 testJsonElement<JsonPrimitive>(js("42"), JsonPrimitive(42)) 67 testJsonElement<JsonElement>(js("42"), JsonPrimitive(42)) 68 } 69 70 @Test 71 fun testJsonPrimitiveDouble() { 72 testJsonElement<JsonElement>(js("42.0"), JsonPrimitive(42.0)) 73 testJsonElement<JsonPrimitive>(js("42.0"), JsonPrimitive(42.0)) 74 } 75 76 @Test 77 fun testJsonStringPrimitive() { 78 testJsonElement<JsonElement>(js(""""42""""), JsonPrimitive("42")) 79 testJsonElement<JsonPrimitive>(js(""""42""""), JsonPrimitive("42")) 80 } 81 82 @Test 83 fun testJsonNull() { 84 testJsonElement<JsonElement>(js("null"), JsonNull) 85 testJsonElement<JsonElement>(js("undefined"), JsonNull) 86 } 87 88 @Test 89 fun testJsonArray() { 90 testJsonElement<JsonElement>(js("[1,2,3]"), JsonArray((1..3).map(::JsonPrimitive))) 91 testJsonElement<JsonArray>(js("[1,2,3]"), JsonArray((1..3).map(::JsonPrimitive))) 92 } 93 94 @Test 95 fun testJsonObject() { 96 testJsonElement<JsonElement>( 97 js("""{1:2,"3":4}"""), 98 JsonObject(mapOf("1" to JsonPrimitive(2), "3" to JsonPrimitive(4))) 99 ) 100 testJsonElement<JsonObject>( 101 js("""{1:2,"3":4}"""), 102 JsonObject(mapOf("1" to JsonPrimitive(2), "3" to JsonPrimitive(4))) 103 ) 104 } 105 106 private inline fun <reified T : JsonElement> testJsonElement(js: dynamic, expected: JsonElement) { 107 val parsed = Json.decodeFromDynamic<T>(js) 108 assertEquals(expected, parsed) 109 } 110 111 @Serializable 112 data class Wrapper(val e: JsonElement, val p: JsonPrimitive, val o: JsonObject, val a: JsonArray, val n: JsonNull) 113 114 @Test 115 fun testJsonElementWrapper() { 116 val js = js("""{"e":42,"p":"239", "o":{"k":"v"}, "a":[1, 2, 3], "n": null}""") 117 val parsed = Json.decodeFromDynamic<Wrapper>(js) 118 val expected = Wrapper(JsonPrimitive(42), JsonPrimitive("239"), buildJsonObject { put("k", "v") }, JsonArray((1..3).map(::JsonPrimitive)), JsonNull) 119 assertEquals(expected, parsed) 120 } 121 } 122