Uberto Barbini
04/09/2021, 8:21 AMRazvan
04/09/2021, 9:54 AMJson<JsonNode>
With manually mapping is pretty straightforward:
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 inspirationUberto Barbini
04/09/2021, 11:46 AM.mapToLens(JStatus)
which work with any converterRazvan
04/09/2021, 12:34 PMfun <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.Uberto Barbini
04/09/2021, 12:53 PMRazvan
04/09/2021, 1:15 PMUberto Barbini
04/09/2021, 1:15 PM// 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)
Razvan
04/09/2021, 3:51 PMUberto Barbini
04/09/2021, 4:33 PMRazvan
04/09/2021, 6:00 PMUberto Barbini
04/09/2021, 8:50 PM