如果类类型是泛型类型'T',如何使用Gson进行解析?
具有泛型类的类,并且在其函数中将json字符串解析为泛型类型'T'在运行时的任何类型。但得到了
java.lang.ClassCastException: com.google.gson.internal.LinkedTreeMap cannot be cast to com.example.model.content
这不适用于泛型'T':
val dataType = object : TypeToken<T>() {}.type
val data = Gson().fromJson<T>(jsonStr, dataType)
调用此调用的方式是调用Content
的'solid'类类型为Handler<Content>
,其中通用的'T'(IHandler<T>
}是预期的
FetchRemoteDataCommand("https", “testcontent.com", "v2/content”,
params,
Handler<Content>).execute()
这是类采用泛型类型:IHandler<T>
,在运行时期望'T'应该是'solid'类型,即它应该是Content
类型的调用
class FetchRemoteDataCommand<T>(scheme: String, authority: String, path: String,
params: HashMap<String, String>,
dataReadyCb: IHandler<T>) {
val mBaseUrl = "$scheme://$authority/"
val mPath = path
val mParams = params
var mDataReadyListener = dataReadyCb
override fun dispose() {……}
override fun execute() {
getRemoteData(object : Observer<ResponseBody> {
override fun onNext(responseBody: ResponseBody) {
val dataType = object : TypeToken<T>() {}.type
val jsonStr = responseBody.string()
val data = Gson().fromJson<T>(jsonStr, dataType) ***//<=== throws***
///
mDataReadyListener(data) //<== suppose to pass back the pared data back
……
}
…………
})
}
private fun getRemoteData(observer: Observer<ResponseBody>) : Unit {
val service: IRemoteRepositoryService = ServiceFactory.createRetrofitService(
IRemoteRepositoryService::class.java, mBaseUrl)
service.getRemoteData(mPath, mParams)
.subscribe(observer)
}
}
答案 0 :(得分:0)
从亨宁那里得到了一个想法(林雨成也有一个在运行时获得它的解决方案)get-generic-type-of-class-at-runtime 所以传入类类型而不是在运行时获取类型。
class FetchRemoteDataCommand<T>(scheme: String, authority: String, path: String,
params: HashMap<String, String>,
dataReadyCb: IHandler<T>, handlerClassType: Class<T>) {
val mBaseUrl = "$scheme://$authority/"
val mPath = path
val mParams = params
var mDataReadyListener = dataReadyCb
private val clazz: Class<T> = handlerClassType //findTypeArguments(this.javaClass) <== some time return null???
private fun findTypeArguments(t: Type) : Class<T> {
return if (t is ParameterizedType) {
val typeArgs = t.actualTypeArguments
typeArgs[0] as Class<T>
} else {
val c = t as Class<*>
findTypeArguments(c.genericSuperclass)
}
}
fun getClassType(): Class<T> {
return clazz
}
override fun execute() {
getRemoteData(object : Observer<ResponseBody> {
override fun onNext(responseBody: ResponseBody) {
//val dataType = object : TypeToken<T>() {}.type
//val jsonStr = responseBody.string()
//val data = Gson().fromJson<T>(jsonStr, dataType) ***//<=== throws***
/// test generic type with the passed in class type instead of reflecting at runtime
val responseRecieved = responseBody.string()
val data = Gson().fromJson(responseRecieved, getClassType())
///
mDataReadyListener(data) //<== suppose to pass back the pared data back
……
}
…………
})
}