SalomonBrys / Kotson

Kotlin bindings for JSON manipulation via Gson
MIT License
709 stars 37 forks source link

Volley integration issues #30

Closed tmm1 closed 7 years ago

tmm1 commented 7 years ago

I'm trying to write the GsonRequest example from https://developer.android.com/training/volley/request-custom.html in kotlin using kotson, but cannot make it work.

I'm getting the following error

Error: Cannot use 'T' as reified type parameter. Use a class instead.

Is this a limitation of kotlin?

class GsonRequest<T : Any>(method: Int, url: String?, val listener: Response.Listener<T>, errorListener: Response.ErrorListener) : Request<T>(method, url, errorListener) {
    val gson = Gson()

    override fun parseNetworkResponse(response: NetworkResponse?): Response<T>? {
        val json = String(response!!.data)
        val obj = gson.fromJson<T>(json, typeToken<T>())
        return Response.success(obj, HttpHeaderParser.parseCacheHeaders(response))
    }

    override fun deliverResponse(response: T) {
        listener.onResponse(response)
    }
}
SalomonBrys commented 7 years ago

Yep, it's Kotlin, not Kotson.

As the fromJson & typeToken functions use reified types, you cannot use them in regular generic function where T is not reified. In such cases, you must use the regular GSon methods and pass a Class.