All Downloads are FREE. Search and download functionalities are using the official Maven repository.

commonTest.kotlinx.serialization.json.JsonParserTest.kt Maven / Gradle / Ivy

There is a newer version: 1.7.3
Show newest version
/*
 * 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 {
            parse("""{"X": "foo", "Y"}""")
        }
    }

    @Test
    fun testIncorrectUnicodeEscape() {
        assertFailsWith {
            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 {  Json.parseToJsonElement(content) }
        val msg = e.message!!
        assertTrue(msg.contains("Unexpected trailing"))
    }

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

        assertFailsWith {
            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)
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy