• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 EncodeToDynamicSpecialCasesTest {
11 
12     @Test
testTopLevelIntnull13     fun testTopLevelInt() = assertDynamicForm(42)
14 
15     @Test
16     fun testTopLevelString() = assertDynamicForm("42")
17 
18     @Test
19     fun testTopLevelList() = assertDynamicForm(listOf(1, 2, 3))
20 
21     @Test
22     fun testStringMap() = assertDynamicForm(mapOf("1" to 2, "3" to 4))
23 
24     @Test
25     fun testByteMap() = assertDynamicForm(mapOf(1.toByte() to 2, 3.toByte() to 4))
26 
27     @Test
28     fun testCharMap() = assertDynamicForm(mapOf('1' to 2, '3' to 4))
29 
30     @Test
31     fun testShortMap() = assertDynamicForm(mapOf(1.toShort() to 2, 3.toShort() to 4))
32 
33     @Test
34     fun testIntMap() = assertDynamicForm(mapOf(1 to 2, 3 to 4))
35 
36     @Test
37     fun testLongMap()  = assertDynamicForm(mapOf(1L to 2, 3L to 4))
38 
39     @Test
40     fun testDoubleMap()  = assertDynamicForm(mapOf(1.0 to 2, 3.0 to 4))
41 
42     @Test
43     fun testFloatMap()  = assertDynamicForm(mapOf(1.0f to 2, 3.0f to 4))
44 
45     @Test
46     fun testJsonPrimitive() {
47         assertDynamicForm(JsonPrimitive(42))
48         assertDynamicForm<JsonElement>(JsonPrimitive(42))
49     }
50 
51     @Test
testJsonPrimitiveDoublenull52     fun testJsonPrimitiveDouble() {
53         assertDynamicForm<JsonElement>(JsonPrimitive(42.0))
54         assertDynamicForm<JsonPrimitive>(JsonPrimitive(42.0))
55     }
56 
57     @Test
testJsonStringPrimitivenull58     fun testJsonStringPrimitive() {
59         assertDynamicForm<JsonElement>(JsonPrimitive("42"))
60         assertDynamicForm<JsonPrimitive>(JsonPrimitive("42"))
61     }
62 
63     @Test
testJsonArraynull64     fun testJsonArray() {
65         assertDynamicForm<JsonElement>(JsonArray((1..3).map(::JsonPrimitive)))
66         assertDynamicForm<JsonArray>(JsonArray((1..3).map(::JsonPrimitive)))
67     }
68 
69     @Test
testJsonObjectnull70     fun testJsonObject() {
71         assertDynamicForm<JsonElement>(
72             JsonObject(mapOf("1" to JsonPrimitive(2), "3" to JsonPrimitive(4)))
73         )
74         assertDynamicForm<JsonObject>(
75             JsonObject(mapOf("1" to JsonPrimitive(2), "3" to JsonPrimitive(4)))
76         )
77     }
78 
79 
80     @Serializable
81     data class Wrapper(val e: JsonElement, val p: JsonPrimitive, val o: JsonObject, val a: JsonArray)
82 
83     @Test
testJsonElementWrappernull84     fun testJsonElementWrapper() {
85         assertDynamicForm(Wrapper(JsonPrimitive(42), JsonPrimitive("239"), buildJsonObject { put("k", "v") }, JsonArray((1..3).map(::JsonPrimitive))))
86     }
87 }
88