Haris Khan
02/03/2021, 1:27 AMfun <T : JsonDataClassHolder> findProfile(
content: String,
clazz: Class<T>
): T {
return mapper.readValue(contentRaw, clazz)
}
I also tried
fun <T : JsonDataClassHolder> findProfile(
content: String
): T {
val type = object : TypeReference<T>() {}
return mapper.readValue(contentRaw, type)
}
but got the following error:
Cannot construct instance of `JsonDataClassHolder` (no Creators, like default construct, exist): abstract types either need to be mapped to concrete types, have custom deserializer, or contain additional type information
An example of how I'd like to call this function might be:
private data class CoolJson(
val status: String
) : JsonDataClassHolder
findProfile<CoolJson>("""{'status': 'cool'""")
}
I liked how I only needed to pass the type as a type parameter versus in my working example as a function variable.
I guess the kotlin way would be to make the reified inline, but i would like to avoid that as well in this case.