Razvan
11/12/2021, 5:04 PM{ total: "10" }
But I know it is an Int and want to map it in my object as a int
data class Response(val total: Int?)
Can’t figure out how to write the converter to do that ?
I was thinking about something like this but couldn’t make it work.
JResponse : JAny<Response>() {
private val total by str???
override fun JsonNodeObject.deserializeOrThrow(): Response = Response(total = (+total)?.toIntOrNull())
}
TODO: add example of class with private constructor and custom serializer/deserializer
object JIntAsString : JStringRepresentable<Int>() {
override val cons: (String) -> Int = {s: String -> s.toIntOrNull() ?: 0}
override val render: (Int) -> String = Int::toString
}
object JResponse : JAny<Response>() {
private val total by JFieldMaybe(Response::total, JIntAsString)
override fun JsonNodeObject.deserializeOrThrow(): Response = Response(total = +total)
}
Was there a better way ?Uberto Barbini
11/12/2021, 10:09 PMRazvan
11/13/2021, 1:11 PM