Skip to content

ClassCastException on Simple Read #1049

@rmayore

Description

@rmayore

// ext function

fun <T> JsonObject.complexGetValue(key: String): T? {
    return try {
        JsonPath.read<T>(this, key)
    } catch (_: PathNotFoundException) {
        null
    }
}

// test json

 {
        "_id" : "fake-id",
        "house": {
                "room" : "kitchen"
                }
}

// read house.room
var room = object.read<String>("$.house.room")

Fails with exception
java.lang.ClassCastException : kotlinx.serialization.json.JsonLiteral cannot be cast to java.lang.String
More:

  • Java version 21
  • Jsonpath version 2.9.0
  • Kotlinx Serialization 1.8.1

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions