hi <@U766RQY2D> I'm using Kondor with Http4k
# http4k
u
hi @Razvan I'm using Kondor with Http4k
r
Hi, I could use it in my tests by manually declaring the Body mapping but I was wandering if there was a way to add it as a http4k-format to do the do the automapping or implement
Json<JsonNode>
With manually mapping is pretty straightforward:
Copy code
val bodyMapper = Body.string(ContentType.APPLICATION_JSON).map(JStatus::fromJsonThrow, JStatus::toJson).toLens()

data class Status(val status: AppStatus, val date: String)

object JStatus : JAny<Status>() {
    val status by str(Status::status)
    val date by str(Status::date)
    override fun JsonNodeObject.deserializeOrThrow() = Status(
        status = +status,
        date = +date
    )

    fun fromJsonThrow(json: String) = fromJson(json).orThrow()
}
If you have some more advanced exemples I’ll happy try to use them as inspiration
u
We did somethink like
Copy code
.mapToLens(JStatus)
which work with any converter
it's 3 lines of code... let me find them...
r
Something like:
Copy code
fun  <T: Any, K: JAny<T>> K.mapToLens() = Body.string(ContentType.APPLICATION_JSON).map( { this.fromJson(it).orThrow() }) {
    this.toJson(it)
}.toLens()

val bodyLens = JStatus.mapToLens()
? I can se a
T.mapToLens(JAny<T>)
but it won’t be a bidirectional lens just a one way to respond Status as Json.
u
Yes similar, apart that is a bidilens
Instead if JAny use JConverter so it would work also with collections and sealed class
r
Thanks I’ll keep playing with and see if I can come around with something working. If I get lost, I’ll reach out. Thank you.
👍 1
u
I will put something in the kondor-json examples and readme, but yes, please continue to experiment
also we use these ext funs:
Copy code
// for http clients
fun <T: Any> Request.bodyAsJson(converter: JConverter<T>, value: T) =
    body(converter.toJson(value)).contentType(APPLICATION_JSON)

fun <T: Any> Response.parseJsonBody(converter: JConverter<T>): T =
    converter.fromJson(bodyString()).orThrow()

//for http servers

fun <T: Any> Request.parseJsonBody(converter: JConverter<T>): T =
    converter.fromJson(bodyString()).orThrow()

fun <T: Any> Response.bodyAsJson(converter: JConverter<T>, value: T) =
    body(converter.toJson(value)).contentType(APPLICATION_JSON)
r
Thanks, useful ones. Btw thanks for pairing with Dmitry for the videos on his youtube channel. Always nice to see how other think and are programming.
u
yeah, we should do more often!
btw me and Dmitry will present something about http4k together at JaxLondon in October
r
Nice. looking forward to… It’s awesome you take the time to spread the word about this great lib. After http4k returning to some old Spring Boot projets at works is incredible painful. Thank to all of you that help http4k to gain traction.
👍 1