I have a `class SimpleObject(val id: String, val a...
# http4k
e
I have a
Copy code
class SimpleObject(val id: String, val attrs: Map<String, Any?>,
                        val created: ZonedDateTime,
                        val updated: ZonedDateTime
)
If I use
with( Header.zonedDateTime().required("last-modified") of obj.created)
, I end up with multiple attributes like
"created":{"dateTime":{"date":{"year":2018,"month":3,...
). I think it should be a single String attribute, based on my understanding of
this.map(ZonedDateTime::parse, DateTimeFormatter.ISO_ZONED_DATE_TIME::format)
in file lensSpec.kt
What am I doing wrong?
s
@elifarley the lenSpec mapping you're referring to is used in other lenses (query/header etc). The Body.auto relies on the underlying
Json
implementation
e
Sorry, I pasted the wrong part of the code
I'll edit my post
s
Which http4k json library are you using?
e
Pronto, editei lรก.
๐Ÿ‡ง๐Ÿ‡ท 1
๐Ÿ™‚ 1
Gson
I'm using Header.zonedDateTime
s
I've tried
Copy code
import org.http4k.core.Method
import org.http4k.core.Request
import org.http4k.core.with
import org.http4k.lens.Header
import org.http4k.lens.zonedDateTime
import java.time.ZonedDateTime

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 request = Request(Method.GET, "/")
        .with(Header.zonedDateTime().required("last-modified") of obj.created)
    println(request)
}
and got
Copy code
request = GET / HTTP/1.1
last-modified: 2018-03-06T21:56:51.882Z[Europe/London]
e
Here's my function:
Copy code
customerService[id]?.let { obj ->

                Response(OK).with(
                        bodySimpleObject of obj
                ).with(headerLastModified of obj.updated)

            } ?: Response(Status.NOT_FOUND)
val headerLastModified = Header.zonedDateTime().required("last-modified")
s
That generates a message like
Copy code
HTTP/1.1 200 OK
content-type: application/json; charset=utf-8
last-modified: 2018-03-06T22:05:09.644Z[Europe/London]

{"id":"foo","attrs":{},"created":{"dateTime":{"date":{"year":2018,"month":3,"day":6},"time":{"hour":22,"minute":5,"second":9,"nano":644000000}},"offset":{"totalSeconds":0},"zone":{"id":"Europe/London"}},"updated":{"dateTime":{"date":{"year":2018,"month":3,"day":6},"time":{"hour":22,"minute":5,"second":9,"nano":717000000}},"offset":{"totalSeconds":0},"zone":{"id":"Europe/London"}}}
e
indeed. How can I get
2018-03-06T21:56:51.882Z[Europe/London]
?
s
The json serialisation is dependent on how the json library is configure to handle
ZonedDateTimes
. We're using a very vanilla config
e
but why your first example worked ?
It looks as if it works if you use it in a Request intance, while it won't work if used in a Response instance.
s
It works for headers because we configure the serialisation in the lensSpec
It won't necessarily work for json because it depends on how the json library is configured
And the behaviour is the same for both request and response in this case.
e
Hmm.. But I think I'm using it on the header, not with json, so it should work... ?
oh...I see
Duh! the header is working just fine!
I was looking at the bloody BODY but didn't notice it, sorry ! ๐Ÿ˜
thanks for your quick replies ๐Ÿ™‚
s
Regarding the body serialisation, if you want to generate the dates in JSON in the same way as the header, you need to customise the Gson adapter. I've put together an example here: https://gist.github.com/s4nchez/9a7e65630c8a71cf6a72b0691fd80a0f
๐Ÿ‘ 1
e
Hey, that's really great, thank you!!
s
You're welcome ๐Ÿ™‚
e
Hmmm.. how do I link it so that it'll be called by Body.auto ?
import myapp.MyCustomGson.auto ?
๐Ÿ‘ 2
good
It worked beautifully, thanks ๐Ÿ™‚
๐Ÿ˜€ 1