My
ConvertJsonToObject function converts the json text to an object of type T and returns that object:
inline fun <reified T> ConvertJsonToObject(moshi:Moshi, jsonText:String): T?
{
val moshiAdapter: JsonAdapter<T> = moshi.adapter(T::class.java)
return moshiAdapter.fromJson(jsonText)
}
Description:
My function
ConvertJsonToObject returns an unknown type
T?.
At the beginning of the function, I specified
inline <reified T>, which means that the function is
template or
generic, i.e. it will use an unknown type
T.
Or you can do it like this:
inline fun <reified T> <T> ConvertJsonToObject(moshi:Moshi, jsonText:String): T?
At the beginning of the function, I could specify
<T>, which means that the function is also
template or
generic, i.e. it will use an unknown type
T.
Why I'm using: inline <reified T>
Answer: That's the only way I can use
T::class.java