Mikael Ståldal
05/30/2023, 7:00 AMMikael Ståldal
05/30/2023, 7:09 AMval prettyGson = GsonBuilder().setPrettyPrinting().create()
val prettyJsonBody = Body.string(ContentType.APPLICATION_JSON, "pretty JSON").map(
{
prettyGson.fromJson(it, JsonElement::class.java)
},
{
prettyGson.toJson(it)
}
).toLens()
Mikael Ståldal
05/30/2023, 7:14 AMpretty
thing here: https://github.com/http4k/http4k/blob/master/http4k-format/core/src/main/kotlin/org/http4k/format/Json.kt#L67
but I am not sure how it is supposed to be used 🤔s4nchez
05/30/2023, 7:34 AMConfigurableGson
and using the auto
from it. Here's a full example:
import CustomGson.auto
import com.google.gson.GsonBuilder
import org.http4k.core.Body
import org.http4k.core.Response
import org.http4k.core.Status.Companion.OK
import org.http4k.core.with
import org.http4k.format.ConfigurableGson
import org.http4k.format.asConfigurable
import org.http4k.format.withStandardMappings
data class MyDataClass(val foo: String, val bar: String)
fun main() {
val lens = Body.auto<MyDataClass>().toLens()
val response = Response(OK).with(lens of MyDataClass("foo", "bar"))
println(response)
}
object CustomGson : ConfigurableGson(
GsonBuilder()
.setPrettyPrinting()
.serializeNulls()
.asConfigurable()
.withStandardMappings()
.done()
)
s4nchez
05/30/2023, 7:34 AMHTTP/1.1 200 OK
content-type: application/json; charset=utf-8
{
"foo": "foo",
"bar": "bar"
}
Mikael Ståldal
05/30/2023, 8:05 AMs4nchez
05/30/2023, 8:06 AMimport CustomGson.auto as prettyAuto
(then use Body.prettyAuto
to create the special lens)s4nchez
05/30/2023, 8:07 AMdave
05/30/2023, 8:27 AMAndrew O'Hara
05/30/2023, 3:36 PM