Lekhnath
Lekhnath

Reputation: 4625

class com.google.gson.internal.LinkedTreeMap cannot be cast to class Partner

import com.google.gson.Gson
import com.google.gson.reflect.TypeToken

fun main() {

    val jsonString: String = """{
        "jsonrpc": "2.0",
        "id": null,
        "result": [
            {
                "id": 1,
                "name": "Lekhnath Rijal"
            },
            {
                "id": 2,
                "name": "Administrator"
            }
        ]
    }"""

    val body1 = Gson().fromJson<RpcResult<List<Partner>>>(jsonString, object: TypeToken<RpcResult<List<Partner>>>(){}.type)

    println(body1.result[0].name) // prints Lekhnath Rijal // - As expected

    val body2 = fromJson<RpcResult<List<Partner>>>(jsonString)
    println(body2.result[0].name) // throws Exception as stated below after this code snippet
}

fun <T> fromJson(json: String?): T {
    return Gson().fromJson<T>(json, object: TypeToken<T>(){}.type)
}

data class RpcResult<T>(
    val jsonrpc: String,
    val id: Int?,
    val result: T
)

data class Partner(
    val id: Int,
    val name: String
)

Exception: java.lang.ClassCastException: class com.google.gson.internal.LinkedTreeMap cannot be cast to class RpcResult

while converting json string to data class object without using function it works as expected but executing same code from helper function does not work and instead throws an exception mentioned above. What am I missing here?

Upvotes: 5

Views: 6696

Answers (1)

Farid
Farid

Reputation: 2562

It is due to type erasure in runtime. In Kotlin you can solve this issue by making your function inline with reified type:

Change your function from:

fun <T> fromJson(json: String?): T {
    return Gson().fromJson<T>(json, object: TypeToken<T>(){}.type)
}

To:

inline fun <reified T> fromJson(json: String?): T {
    return Gson().fromJson<T>(json, object: TypeToken<T>(){}.type)
}

For further reading check this out: https://kotlinlang.org/docs/reference/inline-functions.html

Upvotes: 18

Related Questions