summaryrefslogtreecommitdiff
path: root/formats/json-tests/jsTest/src/kotlinx/serialization/json/DecodeFromDynamicSpecialCasesTest.kt
blob: 748a2dced79432b3d3157b4772db7d2ac74501be (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
/*
 * 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<Int>(dyn)
        assertEquals(42, parsed)
    }

    @Test
    fun testTopLevelString() {
        val dyn = js(""""42"""")
        val parsed = Json.decodeFromDynamic<String>(dyn)
        assertEquals("42", parsed)
    }

    @Test
    fun testTopLevelList() {
        val dyn = js("[1, 2, 3]")
        val parsed = Json.decodeFromDynamic<List<Int>>(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 <reified T> 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<Map<T, Int>>(dyn)
        assertEquals(map, parsed)
    }

    @Test
    fun testJsonPrimitive() {
        testJsonElement<JsonPrimitive>(js("42"), JsonPrimitive(42))
        testJsonElement<JsonElement>(js("42"), JsonPrimitive(42))
    }

    @Test
    fun testJsonPrimitiveDouble() {
        testJsonElement<JsonElement>(js("42.0"), JsonPrimitive(42.0))
        testJsonElement<JsonPrimitive>(js("42.0"), JsonPrimitive(42.0))
    }

    @Test
    fun testJsonStringPrimitive() {
        testJsonElement<JsonElement>(js(""""42""""), JsonPrimitive("42"))
        testJsonElement<JsonPrimitive>(js(""""42""""), JsonPrimitive("42"))
    }

    @Test
    fun testJsonNull() {
        testJsonElement<JsonElement>(js("null"), JsonNull)
        testJsonElement<JsonElement>(js("undefined"), JsonNull)
    }

    @Test
    fun testJsonArray() {
        testJsonElement<JsonElement>(js("[1,2,3]"), JsonArray((1..3).map(::JsonPrimitive)))
        testJsonElement<JsonArray>(js("[1,2,3]"), JsonArray((1..3).map(::JsonPrimitive)))
    }

    @Test
    fun testJsonObject() {
        testJsonElement<JsonElement>(
            js("""{1:2,"3":4}"""),
            JsonObject(mapOf("1" to JsonPrimitive(2), "3" to JsonPrimitive(4)))
        )
        testJsonElement<JsonObject>(
            js("""{1:2,"3":4}"""),
            JsonObject(mapOf("1" to JsonPrimitive(2), "3" to JsonPrimitive(4)))
        )
    }

    private inline fun <reified T : JsonElement> testJsonElement(js: dynamic, expected: JsonElement) {
        val parsed = Json.decodeFromDynamic<T>(js)
        assertEquals(expected, parsed)
    }

    @Serializable
    data class Wrapper(val e: JsonElement, val p: JsonPrimitive, val o: JsonObject, val a: JsonArray, val n: JsonNull)

    @Test
    fun testJsonElementWrapper() {
        val js = js("""{"e":42,"p":"239", "o":{"k":"v"}, "a":[1, 2, 3], "n": null}""")
        val parsed = Json.decodeFromDynamic<Wrapper>(js)
        val expected = Wrapper(JsonPrimitive(42), JsonPrimitive("239"), buildJsonObject { put("k", "v") }, JsonArray((1..3).map(::JsonPrimitive)), JsonNull)
        assertEquals(expected, parsed)
    }
}