The issue, really, is in Jackson's API here. Here's how the readValue method is declared:
public <T> T readValue(String content, TypeReference valueTypeRef)
They are using the raw type of TypeReference for some reason, even though they could easily take a TypeReference<T> as their parameter. If they did, you code would work as is, as Kotlin could then infer the T generic type parameter of the function, and therefore know its return type.
You can work around this issue a couple different ways, however, by being explicit with your types.
Either by providing the generic type parameter for the function, and having the type of the
msgvariable inferred:val typeRef: TypeReference<Map<K, V>> = object : TypeReference<Map<K, V>>() {} val msg = objectMapper.readValue<HashMap<String, String>>(message.payload, typeRef)Or alternatively, by explicitly typing your variable, and having the function's type parameter inferred:
val msg: HashMap<String, String> = objectMapper.readValue(message.payload, typeRef)
The issue, really, is in Jackson's API here. Here's how the readValue method is declared:
public <T> T readValue(String content, TypeReference valueTypeRef)
They are using the raw type of TypeReference for some reason, even though they could easily take a TypeReference<T> as their parameter. If they did, you code would work as is, as Kotlin could then infer the T generic type parameter of the function, and therefore know its return type.
You can work around this issue a couple different ways, however, by being explicit with your types.
Either by providing the generic type parameter for the function, and having the type of the
msgvariable inferred:val typeRef: TypeReference<Map<K, V>> = object : TypeReference<Map<K, V>>() {} val msg = objectMapper.readValue<HashMap<String, String>>(message.payload, typeRef)Or alternatively, by explicitly typing your variable, and having the function's type parameter inferred:
val msg: HashMap<String, String> = objectMapper.readValue(message.payload, typeRef)
One possible way:
inline fun <reified T> ObjectMapper.readValue(s: String): T = this.readValue(s, object : TypeReference<T>() {})
val msg: Map<String,String> = objectMapper.readValue(message.payload)