summaryrefslogtreecommitdiff
path: root/formats/json/jsTest/src/kotlinx/serialization/json/EncodeToDynamicSpecialCasesTest.kt
blob: ead1896974913be70df227caeb711d7c7674270d (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
/*
 * 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.descriptors.*
import kotlinx.serialization.encoding.*
import kotlinx.serialization.*
import kotlinx.serialization.modules.*
import kotlinx.serialization.test.*
import kotlin.test.*
import kotlin.test.assertFailsWith

class EncodeToDynamicSpecialCasesTest {

    @Test
    fun testTopLevelInt() = assertDynamicForm(42)

    @Test
    fun testTopLevelString() = assertDynamicForm("42")

    @Test
    fun testTopLevelList() = assertDynamicForm(listOf(1, 2, 3))

    @Test
    fun testStringMap() = assertDynamicForm(mapOf("1" to 2, "3" to 4))

    @Test
    fun testByteMap() = assertDynamicForm(mapOf(1.toByte() to 2, 3.toByte() to 4))

    @Test
    fun testCharMap() = assertDynamicForm(mapOf('1' to 2, '3' to 4))

    @Test
    fun testShortMap() = assertDynamicForm(mapOf(1.toShort() to 2, 3.toShort() to 4))

    @Test
    fun testIntMap() = assertDynamicForm(mapOf(1 to 2, 3 to 4))

    @Test
    fun testLongMap()  = assertDynamicForm(mapOf(1L to 2, 3L to 4))

    @Test
    fun testDoubleMap()  = assertDynamicForm(mapOf(1.0 to 2, 3.0 to 4))

    @Test
    fun testFloatMap()  = assertDynamicForm(mapOf(1.0f to 2, 3.0f to 4))

    @Test
    fun testJsonPrimitive() {
        assertDynamicForm(JsonPrimitive(42))
        assertDynamicForm<JsonElement>(JsonPrimitive(42))
    }

    @Test
    fun testJsonPrimitiveDouble() {
        assertDynamicForm<JsonElement>(JsonPrimitive(42.0))
        assertDynamicForm<JsonPrimitive>(JsonPrimitive(42.0))
    }

    @Test
    fun testJsonStringPrimitive() {
        assertDynamicForm<JsonElement>(JsonPrimitive("42"))
        assertDynamicForm<JsonPrimitive>(JsonPrimitive("42"))
    }

    @Test
    fun testJsonArray() {
        assertDynamicForm<JsonElement>(JsonArray((1..3).map(::JsonPrimitive)))
        assertDynamicForm<JsonArray>(JsonArray((1..3).map(::JsonPrimitive)))
    }

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


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

    @Test
    fun testJsonElementWrapper() {
        assertDynamicForm(Wrapper(JsonPrimitive(42), JsonPrimitive("239"), buildJsonObject { put("k", "v") }, JsonArray((1..3).map(::JsonPrimitive))))
    }
}