Create an account

Very important

  • To access the important data of the forums, you must be active in each forum and especially in the leaks and database leaks section, send data and after sending the data and activity, data and important content will be opened and visible for you.
  • You will only see chat messages from people who are at or below your level.
  • More than 500,000 database leaks and millions of account leaks are waiting for you, so access and view with more activity.
  • Many important data are inactive and inaccessible for you, so open them with activity. (This will be done automatically)


Thread Rating:
  • 287 Vote(s) - 3.37 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Kotlin: objectmapper.readValue() with TypeReference<HashMap<String, String>> cannot infer parameter

#1
I would like to deserialize a json to Map<String, String> 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)
...
}




Reply

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

#3
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)
Reply

#4
You can use an [object expression](

[To see links please register here]

) to pass an anonymous implementation of `TypeReference`:

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

Notice the `object` keyword before the `TypeReference` parameter.
Reply



Forum Jump:


Users browsing this thread:
1 Guest(s)

©0Day  2016 - 2023 | All Rights Reserved.  Made with    for the community. Connected through