Hello <@UHGG4DEHJ>back again with another question...
# kondor-json
r
Hello @Uberto Barbiniback again with another question I can’t figure out. I got a service that reponds a String:
Copy code
{ total: "10" }
But I know it is an Int and want to map it in my object as a int
Copy code
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.
Copy code
JResponse : JAny<Response>() {
  private val total by str???

  override fun JsonNodeObject.deserializeOrThrow(): Response = Response(total = (+total)?.toIntOrNull())
}
Looks like what I’m looking for is in the todo part of the readme 😉
TODO: add example of class with private constructor and custom serializer/deserializer
I managed to come up with something that works using a custom StringRepresentable implementation
Copy code
object JIntAsString : JStringRepresentable<Int>() {
    override val cons: (String) -> Int = {s: String -> s.toIntOrNull() ?: 0}
    override val render: (Int) -> String = Int::toString
}
Copy code
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 ?
u
I think this is the best way (overriding JStringRepresentable)
You can define a str shortcut function if you have multiple uses
r
Thank you.