LastExceed
03/21/2020, 3:21 PMsuspend fun main() {
val x = getItems1()
val y = getItems2()
}
suspend fun getItems1() = httpClient.get<PayloadContainer<Items>>("<https://api.warframe.market/v1/items>").payload
suspend fun getItems2() = httpClient.getUnwrapped<Items>("<https://api.warframe.market/v1/items>")
suspend inline fun <reified PayloadType> HttpClient.getUnwrapped(url: String) =
get<PayloadContainer<PayloadType>>(url).payload
//for some reason this API wraps all it's payloads in another payload so we need to unwrap 1 layer
data class PayloadContainer<PayloadType>(val payload: PayloadType)
getItems1()
works perfectly fine, getItems2()
gives me the following error:
class java.util.LinkedHashMap cannot be cast to class payload.Items (java.util.LinkedHashMap is in module java.base of loader 'bootstrap'; payload.Items is in unnamed module of loader 'app')
can someone explain the problem? It seems to have to do with type erasure, but shouldn't reified
prevent that ?Jakub Pi
03/21/2020, 7:08 PMget<PayloadContainer<PayloadType>>(url).payload
It looks like you are trying to replicate the functionality in Ktor - which allows you to pass a type parameter to the (already reified) HttpClient.get function specifying the destination class of the deserialisation. I can't explain why it's not working but the specific technique they use is here: https://github.com/ktorio/ktor/blob/master/ktor-client/ktor-client-core/jvm/src/io/ktor/client/call/TypeInfoJvm.kt
You might have better chances asking in the ktor channel