If I just want to parse json in an `org.http4k.cor...
# http4k
r
If I just want to parse json in an
org.http4k.core.Response.body
to kotlin maps and lists, what's the best way to do it? I don't want lenses or data classes or anything like that...
OK, I guess I need a lens... 🙂
Copy code
import org.http4k.format.Moshi.auto

val lens = Body.auto<Map<String, *>>().toLens()
val response: Response = TODO()
val json: Map<String, *> = lens(response)
d
You don't necessarily need a lens, but you'd just be calling the same code the lens does. Manually it would be
Copy code
Moshi.asA<Map<String, Any>>(Response(Status.OK).bodyString())
r
Thanks
d
You could also write your own utility method like
Copy code
inline fun <reified T: Any> HttpMessage.data(): Map<String, Any> = Body.auto<Map<String, Any>>().toLens()(this)
👍 1
or alternatively...
Copy code
response.json<Map<String, Any>>()
There are a few ways! 😂
👍 2