Is there a way of doing something like this? ```fu...
# reflect
j
Is there a way of doing something like this?
Copy code
fun test(json: JsonObject) {
    val type = String::class
    json.decode<type>() //error : Unresolved reference: type
}

private inline fun <reified T> JsonObject.decode(): T = Json.decodeFromJsonElement(this)
e
Copy code
val json = Json { ... }
val serializer = json.serializersModule.serializer(type.createType()) // may fail
json.decodeFromJsonElement(serializer, element)
m
Not directly, but what you reference here is a class reference (not a type), and most Java libs expected class references, so you might use them.
Copy code
fun readUser(gson: Gson, json: String): User {
    val clazz = User::class
    return gson.fromJson(json, clazz)
}
e
I think it's clear they're using kotlinx.serialization in the original snippet though, which is why my response was tailored for that
j
Yes I’m using kotlinx.serialization, I tried your snippet @ephemient but it didn’t work in my case and I had to work on something else