20

I would like to deserialize a json to Map with objectmapper with the following code:

fun onMessage(topic: String, message: MqttMessage) {
   val typeRef = object : TypeReference<HashMap<String, String>>() {}
   val msg = objectMapper.readValue(message.payload, typeRef)
   ...
}

Compiler says it connot infer parameter T in fun <T : Any!> readValue (src: ByteArray!, valueTypeRef: (TypeReference<Any!>..TypeReference<*>?)): T!

Is there any solution to this problem whitout extending a HashMap with my custom class like this:

class MyHashMap : HashMap<String, String>()

...

fun onMessage(topic: String, message: MqttMessage) {
   val msg = objectMapper.readValue(message.payload, MyHashMap::class.java)
   ...
}
1
  • 1
    Why is the negative vote? Sep 8, 2018 at 18:30

3 Answers 3

17

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 msg variable 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)
    
2
  • 8
    Can you show how you declared typeRef? I can't get your example to work. Tried this val typeRef: TypeReference<List<UserInfo>> = TypeReference<List<UserInfo>> (){} but the IDE tells me 'cannot access <init>'
    – xetra11
    Oct 18, 2021 at 12:01
  • val typeRef: TypeReference<Map<K, V>> = object : TypeReference<Map<K, V>>() {}
    – Gregra
    Aug 15, 2022 at 6:06
6

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) 
2

You can use an object expression to pass an anonymous implementation of TypeReference:

objectMapper.readValue(message.payload, object : TypeReference<HashMap<String, String>>() {})

Notice the object keyword before the TypeReference parameter.

Your Answer

Reminder: Answers generated by Artificial Intelligence tools are not allowed on Stack Overflow. Learn more

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.