summaryrefslogtreecommitdiff
path: root/formats/json/commonTest/src/kotlinx/serialization/json/JsonParserTest.kt
blob: 123214e21827bf8efd1cb91452ad60f01e97ff52 (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
/*
 * Copyright 2017-2019 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 kotlinx.serialization.builtins.*
import kotlinx.serialization.json.internal.*
import kotlinx.serialization.test.*
import kotlin.test.*

class JsonParserTest : JsonTestBase() {

    @Test
    fun testQuotedBrace() {
        val tree = parse("""{"x": "{"}""")
        assertTrue("x" in tree)
        assertEquals("{", (tree.getValue("x") as JsonLiteral).content)
    }

    private fun parse(input: String) = default.parseToJsonElement(input).jsonObject

    @Test
    fun testEmptyKey() {
        val tree = parse("""{"":"","":""}""")
        assertTrue("" in tree)
        assertEquals("", (tree.getValue("") as JsonLiteral).content)
    }

    @Test
    fun testEmptyValue() {
        assertFailsWith<JsonDecodingException> {
            parse("""{"X": "foo", "Y"}""")
        }
    }

    @Test
    fun testIncorrectUnicodeEscape() {
        assertFailsWith<JsonDecodingException> {
            parse("""{"X": "\uDD1H"}""")
        }
    }

    @Test
    fun testParseEscapedSymbols() {
        assertEquals(
            StringData("https://t.co/M1uhwigsMT"),
            default.decodeFromString(StringData.serializer(), """{"data":"https:\/\/t.co\/M1uhwigsMT"}""")
        )
        assertEquals(StringData("\"test\""), default.decodeFromString(StringData.serializer(), """{"data": "\"test\""}"""))
        assertEquals(StringData("\u00c9"), default.decodeFromString(StringData.serializer(), """{"data": "\u00c9"}"""))
        assertEquals(StringData("""\\"""), default.decodeFromString(StringData.serializer(), """{"data": "\\\\"}"""))
    }

    @Test
    fun testWorkWithNonAsciiSymbols() {
        assertStringFormAndRestored(
            """{"data":"Русские Буквы 🤔"}""",
            StringData("Русские Буквы \uD83E\uDD14"),
            StringData.serializer()
        )
    }

    @Test
    fun testUnicodeEscapes() {
        val data = buildString {
            append(1.toChar())
            append(".")
            append(0x20.toChar())
            append(".")
            append("\n")
        }

        assertJsonFormAndRestored(String.serializer(), data, "\"\\u0001. .\\n\"")
    }

    @Test
    fun testTrailingComma() {
        testTrailingComma("{\"id\":0,}")
        testTrailingComma("{\"id\":0  ,}")
        testTrailingComma("{\"id\":0  , ,}")
    }

    private fun testTrailingComma(content: String) {
        val e = assertFailsWith<JsonDecodingException> {  Json.parseToJsonElement(content) }
        val msg = e.message!!
        assertTrue(msg.contains("Unexpected trailing"))
    }

    @Test
    fun testUnclosedStringLiteral() {
        assertFailsWith<JsonDecodingException> {
            parse("\"")
        }

        assertFailsWith<JsonDecodingException> {
            parse("""{"id":"""")
        }
    }

    @Test
    fun testNullValue() {
        val obj = Json.parseToJsonElement("""{"k":null}""").jsonObject
        val value = obj["k"]!!
        assertTrue { value is JsonNull }
        assertFalse { value.jsonPrimitive.isString }
    }

    @Test
    fun testNullStringValue() {
        val obj = Json.parseToJsonElement("""{"k":"null"}""").jsonObject
        val value = obj["k"]!!
        assertFalse { value is JsonNull }
        assertTrue { value.jsonPrimitive.isString }
        assertEquals("null", obj["k"]!!.jsonPrimitive.content)
    }
}