• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright 2017-2019 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 kotlinx.serialization.builtins.*
9 import kotlinx.serialization.json.internal.*
10 import kotlinx.serialization.test.*
11 import kotlin.test.*
12 
13 class JsonParserTest : JsonTestBase() {
14 
15     @Test
testQuotedBracenull16     fun testQuotedBrace() {
17         val tree = parse("""{"x": "{"}""")
18         assertTrue("x" in tree)
19         assertEquals("{", (tree.getValue("x") as JsonPrimitive).content)
20     }
21 
parsenull22     private fun parse(input: String) = default.parseToJsonElement(input).jsonObject
23 
24     @Test
25     fun testEmptyKey() {
26         val tree = parse("""{"":"","":""}""")
27         assertTrue("" in tree)
28         assertEquals("", (tree.getValue("") as JsonPrimitive).content)
29     }
30 
31     @Test
testEmptyValuenull32     fun testEmptyValue() {
33         assertFailsWithSerial("JsonDecodingException") {
34             parse("""{"X": "foo", "Y"}""")
35         }
36     }
37 
38     @Test
testIncorrectUnicodeEscapenull39     fun testIncorrectUnicodeEscape() {
40         assertFailsWithSerial("JsonDecodingException") {
41             parse("""{"X": "\uDD1H"}""")
42         }
43     }
44 
45     @Test
testParseEscapedSymbolsnull46     fun testParseEscapedSymbols() {
47         assertEquals(
48             StringData("https://t.co/M1uhwigsMT"),
49             default.decodeFromString(StringData.serializer(), """{"data":"https:\/\/t.co\/M1uhwigsMT"}""")
50         )
51         assertEquals(StringData("\"test\""), default.decodeFromString(StringData.serializer(), """{"data": "\"test\""}"""))
52         assertEquals(StringData("\u00c9"), default.decodeFromString(StringData.serializer(), """{"data": "\u00c9"}"""))
53         assertEquals(StringData("""\\"""), default.decodeFromString(StringData.serializer(), """{"data": "\\\\"}"""))
54     }
55 
56     @Test
testWorkWithNonAsciiSymbolsnull57     fun testWorkWithNonAsciiSymbols() {
58         assertStringFormAndRestored(
59             """{"data":"Русские Буквы ��"}""",
60             StringData("Русские Буквы \uD83E\uDD14"),
61             StringData.serializer()
62         )
63     }
64 
65     @Test
testUnicodeEscapesnull66     fun testUnicodeEscapes() {
67         val data = buildString {
68             append(1.toChar())
69             append(".")
70             append(0x20.toChar())
71             append(".")
72             append("\n")
73         }
74 
75         assertJsonFormAndRestored(String.serializer(), data, "\"\\u0001. .\\n\"")
76     }
77 
78     @Test
testTrailingCommanull79     fun testTrailingComma() {
80         testTrailingComma("{\"id\":0,}")
81         testTrailingComma("{\"id\":0  ,}")
82         testTrailingComma("{\"id\":0  , ,}")
83     }
84 
testTrailingCommanull85     private fun testTrailingComma(content: String) {
86         assertFailsWithSerialMessage("JsonDecodingException", "Trailing comma before the end of JSON object") {  Json.parseToJsonElement(content) }
87     }
88 
89     @Test
testUnclosedStringLiteralnull90     fun testUnclosedStringLiteral() {
91         assertFailsWithSerial("JsonDecodingException") {
92             parse("\"")
93         }
94 
95         assertFailsWithSerial("JsonDecodingException") {
96             parse("""{"id":"""")
97         }
98     }
99 
100     @Test
101     fun testNullValue() {
102         val obj = Json.parseToJsonElement("""{"k":null}""").jsonObject
103         val value = obj["k"]!!
104         assertTrue { value is JsonNull }
105         assertFalse { value.jsonPrimitive.isString }
106     }
107 
108     @Test
109     fun testNullStringValue() {
110         val obj = Json.parseToJsonElement("""{"k":"null"}""").jsonObject
111         val value = obj["k"]!!
112         assertFalse { value is JsonNull }
113         assertTrue { value.jsonPrimitive.isString }
114         assertEquals("null", obj["k"]!!.jsonPrimitive.content)
115     }
116 }
117