-
-
Save s4nchez/9a7e65630c8a71cf6a72b0691fd80a0f to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package myapp | |
import com.google.gson.GsonBuilder | |
import org.http4k.core.Body | |
import org.http4k.core.Response | |
import org.http4k.core.Status | |
import org.http4k.core.with | |
import org.http4k.lens.Header | |
import org.http4k.lens.zonedDateTime | |
import java.time.ZonedDateTime | |
import java.io.IOException | |
import com.google.gson.TypeAdapter | |
import com.google.gson.stream.JsonReader | |
import com.google.gson.stream.JsonWriter | |
import org.http4k.format.ConfigurableGson | |
import myapp.MyCustomGson.auto | |
object MyCustomGson : ConfigurableGson(GsonBuilder() | |
.registerTypeAdapter(ZonedDateTime::class.java, object : TypeAdapter<ZonedDateTime>() { | |
@Throws(IOException::class) | |
override fun write(out: JsonWriter, value: ZonedDateTime) { | |
out.value(value.toString()) | |
} | |
@Throws(IOException::class) | |
override fun read(`in`: JsonReader): ZonedDateTime { | |
return ZonedDateTime.parse(`in`.nextString()) | |
} | |
}) | |
.enableComplexMapKeySerialization() | |
) | |
class SimpleObject(val id: String, val attrs: Map<String, Any?>, | |
val created: ZonedDateTime, | |
val updated: ZonedDateTime | |
) | |
fun main(args: Array<String>) { | |
val obj = SimpleObject("foo", emptyMap(), ZonedDateTime.now(), ZonedDateTime.now()) | |
val response = Response(Status.OK) | |
.with(Body.auto<SimpleObject>().toLens() of obj) | |
.with(Header.zonedDateTime().required("last-modified") of obj.created) | |
println(response) | |
GsonBuilder().serializeNulls() | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment