Skip to content

Instantly share code, notes, and snippets.

@s4nchez
Last active March 9, 2018 18:09
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save s4nchez/9a7e65630c8a71cf6a72b0691fd80a0f to your computer and use it in GitHub Desktop.
Save s4nchez/9a7e65630c8a71cf6a72b0691fd80a0f to your computer and use it in GitHub Desktop.
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