|
| 1 | +/* |
| 2 | + * Copyright 2017-2024 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license. |
| 3 | + */ |
| 4 | + |
| 5 | +package kotlinx.serialization.features |
| 6 | + |
| 7 | +import kotlinx.serialization.* |
| 8 | +import kotlinx.serialization.json.* |
| 9 | +import kotlin.test.* |
| 10 | + |
| 11 | +class JsonCommentsTest: JsonTestBase() { |
| 12 | + val json = Json(default) { |
| 13 | + allowComments = true |
| 14 | + allowTrailingComma = true |
| 15 | + } |
| 16 | + |
| 17 | + val withLenient = Json(json) { |
| 18 | + isLenient = true |
| 19 | + ignoreUnknownKeys = true |
| 20 | + } |
| 21 | + |
| 22 | + @Test |
| 23 | + fun testBasic() = parametrizedTest { mode -> |
| 24 | + val inputBlock = """{"data": "b" /*value b*/ }""" |
| 25 | + val inputLine = "{\"data\": \"b\" // value b \n }" |
| 26 | + assertEquals(StringData("b"), json.decodeFromString(inputBlock, mode)) |
| 27 | + assertEquals(StringData("b"), json.decodeFromString(inputLine, mode)) |
| 28 | + } |
| 29 | + |
| 30 | + @Serializable |
| 31 | + data class Target(val key: String, val key2: List<Int>, val key3: NestedTarget, val key4: String) |
| 32 | + |
| 33 | + @Serializable |
| 34 | + data class NestedTarget(val nestedKey: String) |
| 35 | + |
| 36 | + private fun target(key4: String): Target = Target("value", listOf(1, 2), NestedTarget("foo"), key4) |
| 37 | + |
| 38 | + @Test |
| 39 | + fun testAllBlocks() = parametrizedTest { mode -> |
| 40 | + val input = """{ /*beginning*/ |
| 41 | + /*before key*/ "key" /*after key*/ : /*after colon*/ "value" /*before comma*/, |
| 42 | + "key2": [ /*array1*/ 1, /*array2*/ 2, /*end array*/], |
| 43 | + "key3": { /*nested obj*/ "nestedKey": "foo"} /*after nested*/, |
| 44 | + "key4": "/*comment inside quotes is a part of value*/", |
| 45 | + /*before end*/ |
| 46 | + }""" |
| 47 | + assertEquals(target("/*comment inside quotes is a part of value*/"), json.decodeFromString(input, mode)) |
| 48 | + } |
| 49 | + |
| 50 | + @Test |
| 51 | + fun testAllLines() = parametrizedTest { mode -> |
| 52 | + val input = """{ //beginning |
| 53 | + //before key |
| 54 | + "key" // after key |
| 55 | + : // after colon |
| 56 | + "value" //before comma |
| 57 | + , |
| 58 | + "key2": [ //array1 |
| 59 | + 1, //array2 |
| 60 | + 2, //end array |
| 61 | + ], |
| 62 | + "key3": { //nested obj |
| 63 | + "nestedKey": "foo" |
| 64 | + } , //after nested |
| 65 | + "key4": "//comment inside quotes is a part of value", |
| 66 | + //before end |
| 67 | + }""" |
| 68 | + assertEquals(target("//comment inside quotes is a part of value"), json.decodeFromString(input, mode)) |
| 69 | + } |
| 70 | + |
| 71 | + @Test |
| 72 | + fun testMixed() = parametrizedTest { mode -> |
| 73 | + val input = """{ // begin |
| 74 | + "key": "value", // after |
| 75 | + "key2": /* array */ [1, 2], |
| 76 | + "key3": /* //this is a block comment */ { "nestedKey": // /*this is a line comment*/ "bar" |
| 77 | + "foo" }, |
| 78 | + "key4": /* nesting block comments /* not supported */ "*/" |
| 79 | + /* end */}""" |
| 80 | + assertEquals(target("*/"), json.decodeFromString(input, mode)) |
| 81 | + } |
| 82 | + |
| 83 | + @Test |
| 84 | + fun testWithLenient() = parametrizedTest { mode -> |
| 85 | + val input = """{ //beginning |
| 86 | + //before key |
| 87 | + key // after key |
| 88 | + : // after colon |
| 89 | + value //before comma |
| 90 | + , |
| 91 | + key2: [ //array1 |
| 92 | + 1, //array2 |
| 93 | + 2, //end array |
| 94 | + ], |
| 95 | + key3: { //nested obj |
| 96 | + nestedKey: "foo" |
| 97 | + } , //after nested |
| 98 | + key4: value//comment_cannot_break_value_apart, |
| 99 | + key5: //comment without quotes where new token expected is still a comment |
| 100 | + value5, |
| 101 | + //before end |
| 102 | + }""" |
| 103 | + assertEquals(target("value//comment_cannot_break_value_apart"), withLenient.decodeFromString(input, mode)) |
| 104 | + } |
| 105 | + |
| 106 | + @Test |
| 107 | + fun testUnclosedCommentsErrorMsg() = parametrizedTest { mode -> |
| 108 | + val input = """{"data": "x"} // no newline""" |
| 109 | + assertEquals(StringData("x"), json.decodeFromString<StringData>(input, mode)) |
| 110 | + val input2 = """{"data": "x"} /* no endblock""" |
| 111 | + assertFailsWith<SerializationException>("Expected end of the block comment: \"*/\", but had EOF instead at path: \$") { |
| 112 | + json.decodeFromString<StringData>(input2, mode) |
| 113 | + } |
| 114 | + } |
| 115 | + |
| 116 | + @Test |
| 117 | + fun testVeryLargeComments() = parametrizedTest { mode -> |
| 118 | + // 16 * 1024 is ReaderJsonLexer.BATCH_SIZE |
| 119 | + val strLen = 16 * 1024 * 2 + 42 |
| 120 | + val inputLine = """{"data": //a""" + "a".repeat(strLen) + "\n\"x\"}" |
| 121 | + assertEquals(StringData("x"), json.decodeFromString<StringData>(inputLine, mode)) |
| 122 | + val inputBlock = """{"data": /*a""" + "a".repeat(strLen) + "*/\"x\"}" |
| 123 | + assertEquals(StringData("x"), json.decodeFromString<StringData>(inputBlock, mode)) |
| 124 | + } |
| 125 | + |
| 126 | +} |
0 commit comments