summaryrefslogtreecommitdiff
path: root/runtime/common/src/testWithNative/kotlinx/serialization/json/serializers/JsonArraySerializerTest.kt
blob: 5b92ccafba3a39b6fb788eec9548b21d86ce7ac7 (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
/*
 * Copyright 2017-2019 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license.
 */

package kotlinx.serialization.json.serializers

import kotlinx.serialization.json.*
import kotlinx.serialization.test.*
import kotlin.test.*

class JsonArraySerializerTest : JsonTestBase() {

    private val expected = "{\"array\":[1,null,[\"nested literal\"],[],{\"key\":\"value\"}]}"
    private val expectedTopLevel = "[1,null,[\"nested literal\"],[],{\"key\":\"value\"}]"

    @Test
    fun testJsonArray() = parametrizedTest(strict) {
        assertStringFormAndRestored(expected, JsonArrayWrapper(prebuiltJson()), JsonArrayWrapper.serializer())
    }

    @Test
    fun testJsonArrayAsElement() = parametrizedTest(strict) {
        assertStringFormAndRestored(expected.replace("array", "element"), JsonElementWrapper(prebuiltJson()), JsonElementWrapper.serializer())
    }

    @Test
    fun testTopLevelJsonObject() = parametrizedTest(strict) {
        assertStringFormAndRestored(expectedTopLevel, prebuiltJson(), JsonArraySerializer)
    }

    @Test
    fun testTopLevelJsonObjectAsElement() = parametrizedTest(strict) {
        assertStringFormAndRestored(expectedTopLevel, prebuiltJson(), JsonElementSerializer)
    }

    @Test
    fun testJsonArrayToString() {
        val prebuiltJson = prebuiltJson()
        val string = nonStrict.stringify(JsonArraySerializer, prebuiltJson)
        assertEquals(string, prebuiltJson.toString())
    }

    private fun prebuiltJson(): JsonArray {
        return jsonArray {
            +JsonLiteral(1)
            +JsonNull
            +jsonArray {
                +JsonLiteral("nested literal")
            }
            +jsonArray { }
            +json {
                "key" to "value"
            }
        }
    }
}