My
ConvertJsonToList function converts the json array to a list of objects of type T and returns this list:
inline fun <reified T> ConvertJsonToList(moshi:Moshi, jsonText:String): List<T>?
{
val listType = Types.newParameterizedType(List::class.java, T::class.java)
val moshiAdapter: JsonAdapter<List<T>> = moshi.adapter(listType)
return moshiAdapter.fromJson(jsonText)
}
Description:
My
ConvertJsonToList function returns a list of 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> ConvertJsonToList(moshi:Moshi, jsonText:String): List<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