Hi,
I am trying out the new kotlinx.serialization (in kotlin.js for now) and serializing by json
How would I create a generic find function that returns a correctly typed Kotlin object?
One of my many attempts is below. Here the compiler warns that Serializable is a final type, and also doesn’t know about the serializer method. Also did not have much luck installing the JsonFeature.
Any help on how to do this in a generic way would be appreciated.
suspend inline fun <reified T : Serializable> find2(id: Long): T? {
HttpClient(Js) {
// install(JsonFeature) {
// serializer = GsonSerializer()
// serializer = defaultSerializer()
// }
}.use {
// .use automatically closes the client
val json = it.get<String>("$baseUrl/$id")
// val ob = kotlinx.serialization.json.Json.parse(Event.serializer(), json)
// ^^^ Explicit type works (without the Serializable type in the function definition)
return kotlinx.serialization.json.Json.parse((T.serializer(), json)
// ^^^ What should I use here??
}
}