JSON writer for ZonedDateTime does not write timezone

216 views
Skip to first unread message

Marco Schweighauser

unread,
Jun 15, 2015, 3:34:00 PM6/15/15
to play-fr...@googlegroups.com
I use Json.toJson(object) to generate a JSON response for an API. I noticed that all my ZoneDateTimes do not include any timezone information once the JSON is written. A look into the relevant Play code reveals that the DateTimeFormatter.ISO_LOCAL_DATE is used.

implicit val DefaultZonedDateTimeWrites = temporalWrites[ZonedDateTime, DateTimeFormatter](DateTimeFormatter.ISO_LOCAL_DATE_TIME)

In my opinion this code should use DateTimeFormatter.ISO_OFFSET_DATE_TIME instead.

Is there an elegant way how to include the time zone information? Is it somehow possible to overwrite this built-in formatter?

Marco Schweighauser

unread,
Jun 19, 2015, 12:53:51 PM6/19/15
to play-fr...@googlegroups.com
I found a simple solution. Just add the following implicit conversion to the companion object of the class which should be serialized. The resulting JSON will have all date times incl. the time zone offset.

case class Car(maxSpeed: Int, ...)

object Car {
implicit val ZonedDateTimeWrites = Writes.temporalWrites[ZonedDateTime, DateTimeFormatter](DateTimeFormatter.ISO_DATE_TIME)
implicit val carWrites: Writes[Car] = Json.writes[car]
}
Reply all
Reply to author
Forward
0 new messages