I've had a search through the history of this channel but couldn't find anything. I have the following map
mapOf("foo" to "2")
. I want to convert it into the following data class
data class Sample(val foo: Option<Number>)
. I know I can use jacksonObjectMapper to do this if I wasn't using
Option
using
fun fromMap(m: Map<String, String>) = jacksonObjectMapper().convertValue<Sample>(m)
. How can I get jacksonObjectMapper to convert any values I have into
Option
? Providing a
None
value for nulls.
b
Bob Glamm
03/26/2020, 3:17 AM
I thought maybe this was present in Arrow, but I think the best option you have currently is to pull something like this in scope:
fun <T> T?.asOption(): Option<T> = Option.fromNullable(this)
r
raulraja
03/26/2020, 9:45 AM
I guess for that case you would have to write a custom marshaller for jackson for Option but I wouldn’t recommend it since nullable types should already be supported and they are isomorphic to option and faster, as Bob said you can always go toOption when you need to.