Skip to content

Parsing JSON with unquoted string literal to JsonElement is not prohibited in non-lenient mode #2511

Open
@laskovenko1

Description

@laskovenko1

Describe the bug

In accordance with the JSON specification, it is required that any string value be enclosed in quotes. However, when I attempt to parse inputs containing unquoted string values into a JsonElement, I observe that the resulting JSON tree contains malformed JsonLiteral objects (with content=str and isString=false).

Additionally, an intriguing observation prompted me to raise this issue: when I encode this JsonElement back to a string, the values are quoted, introducing what appears to be inconsistent behavior.

To Reproduce

    val jsonStr1 = """{"a": 1a}"""
    val jsonTree = Json { isLenient = false }.parseToJsonElement(jsonStr1)
    val jsonStr2 = Json { isLenient = false }.encodeToString(jsonTree)
    if (jsonStr1 != jsonStr2) throw IllegalStateException()

Expected behavior

SerializationException is thrown when malformed input parsed or jsonStr1 == jsonStr2

Environment

  • Kotlin version: 1.9.0
  • Library version: 1.6.1
  • Kotlin platforms: JVM

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions