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
ephemient
03/03/2022, 7:43 PM
Copy code
val json = Json { ... }
val serializer = json.serializersModule.serializer(type.createType()) // may fail
json.decodeFromJsonElement(serializer, element)
m
marcinmoskala
03/08/2022, 1:33 PM
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
ephemient
03/08/2022, 9:05 PM
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
jean
03/10/2022, 9:25 AM
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