Michal Klimczak
04/06/2022, 7:59 PMprivate suspend inline fun <reified T> fetch(
url: String,
state: ObservableValue<Collection<T>>
) {
val response1 = client.get(url).body<List<T>>(typeInfo<List<T>>())
val response2 : List<T> = client.get(url).body()
val type = typeOf<T>()
}
neither response1, nor response2 is able to properly derive the generic type, even though it's reified.
TypeInfo(type=class List, reifiedType=[object Object], kotlinType=null)
And calling typeOf fails with a weird " This marker function should never been called. Looks like compiler did not eliminate it properly. Please, report an issue if you caught this exception."
When the type is provided directly, everything works fine, obviously (kotlinType
is properly defined). Is that not possible at all, even with reified type? Can I create a KType
for List<T>
?
Kotlin 1.6.10Anton Lakotka [JB]
04/08/2022, 5:07 PMMichal Klimczak
04/08/2022, 5:11 PMMichal Klimczak
04/08/2022, 5:35 PMfun test() {
val projects = listOf(Project(1, "A"), Project(2, "B"))
val encodedProjects = Json.encodeToString(projects)
val decodedProjects : List<Project> = deserializeGeneric(encodedProjects)
println(decodedProjects)
}
private inline fun <reified T> deserializeGeneric(encodedStuff: String) =
Json.decodeFromString<T>(encodedStuff)
But similar ktor doesn't
fetch<List<Project>>()
private inline fun <reified T> fetch() {
val client = HttpClient(Js) {
install(ContentNegotiation) { json(Json { ignoreUnknownKeys = true }) }
}
AppScope.launch {
val response: T = client.get("$baseUrl/admin/project").body()
println(response)
}
}
JsonDecodingException
Expected class JsonObject as the serialized body of kotlinx.serialization.Polymorphic<List>, but had class JsonArrayMichal Klimczak
04/08/2022, 5:36 PMAnton Lakotka [JB]
04/08/2022, 5:42 PMshould I search around ktor rather than kotlin js then?I guess so. But I’ll try to reproduce anyway.
Michal Klimczak
04/08/2022, 5:44 PMAnton Lakotka [JB]
04/08/2022, 7:15 PMmain
function.
• And then run gradle task: ./gradlew jsNodeRun