Skip to content

Error on unquoted string gives incorrect position #2360

@sandwwraith

Description

@sandwwraith

Describe the bug

If key or value in Json is not quoted (e.g. one wanted to parse a data in lenient mode, but forgot to turn it on),
the position points to the end of such value, but message mentions the beginning:

To Reproduce

@Test
fun lenientIncorrect() {
        val input1 = """{boxed:str}"""
        val input2 = """{"boxed":str}"""
        val ser = serializer<Box<String>>()
        Json.decodeFromString(ser, input2)
}

Expected behavior

Parsing input2 gives

kotlinx.serialization.json.internal.JsonDecodingException: Unexpected JSON token at offset 11: Expected quotation mark '"', but had 'r' instead at path: $.boxed
JSON input: {"boxed":str}

although message should be 'had s instead' and position 8, to point at the beginning of the string where quotation is expected.

Parsing input1 gives

JSON input: {boxed:str}

Same here: should have offset=1 and message 'had b instead'.

Reason: consumeStringLenient here advances position and doesn't roll it back.

Environment

  • Kotlin version: 1.8.21
  • Library version: 1.5.1
  • Kotlin platforms: JVM

Metadata

Metadata

Assignees

Labels

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions