diff options
author | Leonid Startsev <sandwwraith@users.noreply.github.com> | 2023-10-19 12:49:54 +0200 |
---|---|---|
committer | GitHub <noreply@github.com> | 2023-10-19 12:49:54 +0200 |
commit | cf57414d83314bf05cb28ae8a600163b96855ba0 (patch) | |
tree | 170fd431be2b497e733e6b84d0f3a2a091e26665 /formats/json-tests | |
parent | 6ac490287b08c9aec6e07cba3b503a8def67d4ba (diff) | |
download | kotlinx.serialization-cf57414d83314bf05cb28ae8a600163b96855ba0.tar.gz |
Add a flag to allow parser to accept trailing commas. (#2480)
This is one of the popular community requests and one of the main reasons people ask for Json5 support.
Implementing this flag separately will allow for alleviating large paint points quickly without waiting for full Json5 support.
Fixes #1812
Relates to: #797, #2221
Diffstat (limited to 'formats/json-tests')
4 files changed, 136 insertions, 8 deletions
diff --git a/formats/json-tests/commonTest/src/kotlinx/serialization/json/JsonErrorMessagesTest.kt b/formats/json-tests/commonTest/src/kotlinx/serialization/json/JsonErrorMessagesTest.kt index 8c16ac01..08d1eefd 100644 --- a/formats/json-tests/commonTest/src/kotlinx/serialization/json/JsonErrorMessagesTest.kt +++ b/formats/json-tests/commonTest/src/kotlinx/serialization/json/JsonErrorMessagesTest.kt @@ -6,6 +6,7 @@ package kotlinx.serialization.json import kotlinx.serialization.* +import kotlinx.serialization.test.* import kotlin.test.* @@ -155,11 +156,4 @@ class JsonErrorMessagesTest : JsonTestBase() { }) } - - private fun checkSerializationException(action: () -> Unit, assertions: SerializationException.(String) -> Unit) { - val e = assertFailsWith(SerializationException::class, action) - assertNotNull(e.message) - e.assertions(e.message!!) - } - } diff --git a/formats/json-tests/commonTest/src/kotlinx/serialization/json/JsonParserTest.kt b/formats/json-tests/commonTest/src/kotlinx/serialization/json/JsonParserTest.kt index 7f9044f9..94f7052c 100644 --- a/formats/json-tests/commonTest/src/kotlinx/serialization/json/JsonParserTest.kt +++ b/formats/json-tests/commonTest/src/kotlinx/serialization/json/JsonParserTest.kt @@ -83,7 +83,7 @@ class JsonParserTest : JsonTestBase() { } private fun testTrailingComma(content: String) { - assertFailsWithSerialMessage("JsonDecodingException", "Unexpected trailing") { Json.parseToJsonElement(content) } + assertFailsWithSerialMessage("JsonDecodingException", "Trailing comma before the end of JSON object") { Json.parseToJsonElement(content) } } @Test diff --git a/formats/json-tests/commonTest/src/kotlinx/serialization/json/TrailingCommaTest.kt b/formats/json-tests/commonTest/src/kotlinx/serialization/json/TrailingCommaTest.kt new file mode 100644 index 00000000..0916de57 --- /dev/null +++ b/formats/json-tests/commonTest/src/kotlinx/serialization/json/TrailingCommaTest.kt @@ -0,0 +1,128 @@ +/* + * Copyright 2017-2023 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.test.* +import kotlin.test.* + +class TrailingCommaTest : JsonTestBase() { + val tj = Json { allowTrailingComma = true } + + @Serializable + data class Optional(val data: String = "") + + @Serializable + data class MultipleFields(val a: String, val b: String, val c: String) + + private val multipleFields = MultipleFields("1", "2", "3") + + @Serializable + data class WithMap(val m: Map<String, String>) + + private val withMap = WithMap(mapOf("a" to "1", "b" to "2", "c" to "3")) + + @Serializable + data class WithList(val l: List<Int>) + + private val withList = WithList(listOf(1, 2, 3)) + + @Test + fun basic() = parametrizedTest { mode -> + val sd = """{"data":"str",}""" + assertEquals(Optional("str"), tj.decodeFromString<Optional>(sd, mode)) + } + + @Test + fun trailingCommaNotAllowedByDefaultForObjects() = parametrizedTest { mode -> + val sd = """{"data":"str",}""" + checkSerializationException({ + default.decodeFromString<Optional>(sd, mode) + }, { message -> + assertContains( + message, + """Unexpected JSON token at offset 13: Trailing comma before the end of JSON object""" + ) + }) + } + + @Test + fun trailingCommaNotAllowedByDefaultForLists() = parametrizedTest { mode -> + val sd = """{"l":[1,]}""" + checkSerializationException({ + default.decodeFromString<WithList>(sd, mode) + }, { message -> + assertContains( + message, + """Unexpected JSON token at offset 7: Trailing comma before the end of JSON array""" + ) + }) + } + + @Test + fun trailingCommaNotAllowedByDefaultForMaps() = parametrizedTest { mode -> + val sd = """{"m":{"a": "b",}}""" + checkSerializationException({ + default.decodeFromString<WithMap>(sd, mode) + }, { message -> + assertContains( + message, + """Unexpected JSON token at offset 14: Trailing comma before the end of JSON object""" + ) + }) + } + + @Test + fun emptyObjectNotAllowed() = parametrizedTest { mode -> + assertFailsWithMessage<SerializationException>("Unexpected leading comma") { + tj.decodeFromString<Optional>("""{,}""", mode) + } + } + + @Test + fun emptyListNotAllowed() = parametrizedTest { mode -> + assertFailsWithMessage<SerializationException>("Unexpected leading comma") { + tj.decodeFromString<WithList>("""{"l":[,]}""", mode) + } + } + + @Test + fun emptyMapNotAllowed() = parametrizedTest { mode -> + assertFailsWithMessage<SerializationException>("Unexpected leading comma") { + tj.decodeFromString<WithMap>("""{"m":{,}}""", mode) + } + } + + @Test + fun testMultipleFields() = parametrizedTest { mode -> + val input = """{"a":"1","b":"2","c":"3", }""" + assertEquals(multipleFields, tj.decodeFromString(input, mode)) + } + + @Test + fun testWithMap() = parametrizedTest { mode -> + val input = """{"m":{"a":"1","b":"2","c":"3", }}""" + + assertEquals(withMap, tj.decodeFromString(input, mode)) + } + + @Test + fun testWithList() = parametrizedTest { mode -> + val input = """{"l":[1, 2, 3, ]}""" + assertEquals(withList, tj.decodeFromString(input, mode)) + } + + @Serializable + data class Mixed(val mf: MultipleFields, val wm: WithMap, val wl: WithList) + + @Test + fun testMixed() = parametrizedTest { mode -> + //language=JSON5 + val input = """{"mf":{"a":"1","b":"2","c":"3",}, + "wm":{"m":{"a":"1","b":"2","c":"3",},}, + "wl":{"l":[1, 2, 3,],},}""" + assertEquals(Mixed(multipleFields, withMap, withList), tj.decodeFromString(input, mode)) + } +} diff --git a/formats/json-tests/commonTest/src/kotlinx/serialization/test/TestingFramework.kt b/formats/json-tests/commonTest/src/kotlinx/serialization/test/TestingFramework.kt index e941f047..3ec07149 100644 --- a/formats/json-tests/commonTest/src/kotlinx/serialization/test/TestingFramework.kt +++ b/formats/json-tests/commonTest/src/kotlinx/serialization/test/TestingFramework.kt @@ -92,3 +92,9 @@ inline fun <reified T : Throwable> assertFailsWithMessage( "expected:<$message> but was:<${exception.message}>" ) } + +inline fun checkSerializationException(action: () -> Unit, assertions: SerializationException.(String) -> Unit) { + val e = assertFailsWith(SerializationException::class, action) + assertNotNull(e.message) + e.assertions(e.message!!) +} |