18

How do I implement a custom JodaTime's DateTime serializer/deserializer for JSON? I'm inclined to use the Play Framework's JSON library (2.1.1). There is a default DateTime serializer, but it uses dt.getMillis instead of .toString which would return an ISO compliant String.

Writing Reads[T] and Writes[T] for case classes seems fairly straightforward, but I can't figure out how to do the same for DateTime.

Ricardo
  • 3,696
  • 5
  • 36
  • 50
Dominykas Mostauskis
  • 7,797
  • 3
  • 48
  • 67

3 Answers3

25

There is a default DateTime serializer, but it uses dt.getMillis instead of .toString which would return an ISO compliant String.

If you look at the source, Reads.jodaDateReads already handles both numbers and strings using DateTimeFormatter.forPattern. If you want to handle ISO8601 string, just replace it with ISODateTimeFormat:

  implicit val jodaISODateReads: Reads[org.joda.time.DateTime] = new Reads[org.joda.time.DateTime] {
    import org.joda.time.DateTime

    val df = org.joda.time.format.ISODateTimeFormat.dateTime()

    def reads(json: JsValue): JsResult[DateTime] = json match {
      case JsNumber(d) => JsSuccess(new DateTime(d.toLong))
      case JsString(s) => parseDate(s) match {
        case Some(d) => JsSuccess(d)
        case None => JsError(Seq(JsPath() -> Seq(ValidationError("validate.error.expected.date.isoformat", "ISO8601"))))
      }
      case _ => JsError(Seq(JsPath() -> Seq(ValidationError("validate.error.expected.date"))))
    }

    private def parseDate(input: String): Option[DateTime] =
      scala.util.control.Exception.allCatch[DateTime] opt (DateTime.parse(input, df))

  }

(simplify as desired, e.g. remove number handling)

  implicit val jodaDateWrites: Writes[org.joda.time.DateTime] = new Writes[org.joda.time.DateTime] {
    def writes(d: org.joda.time.DateTime): JsValue = JsString(d.toString())
  }
Alexey Romanov
  • 167,066
  • 35
  • 309
  • 487
  • Great answer. I had to make a modification to get it working though. 'pattern' is referred to but is not in scope. I replaced it with 'df'. Was that your intention? – justinhj Jul 22 '14 at 05:44
  • @justinhj Thanks! `df` doesn't make sense there, actually. I fixed it. – Alexey Romanov Jul 22 '14 at 06:10
19

I use Play 2.3.7 and define in companion object implicit reads/writes with string pattern:

case class User(username:String, birthday:org.joda.time.DateTime)

object User {
  implicit val yourJodaDateReads = Reads.jodaDateReads("yyyy-MM-dd'T'HH:mm:ss'Z'")
  implicit val yourJodaDateWrites = Writes.jodaDateWrites("yyyy-MM-dd'T'HH:mm:ss'Z'")
  implicit val userFormat = Json.format[User]
}
Evghenii Todorov
  • 637
  • 6
  • 15
  • Hi, I've tried your code, but it doesn't work for me, I've created a new [post](http://stackoverflow.com/questions/31462673/how-to-use-joda-datetime-with-play-json) about this issue – Guillaume Jul 18 '15 at 18:55
  • In your code use `Json.fromJson[User](value)` instead of `Json.fromJson(value)`. More details see in [answer](http://stackoverflow.com/a/31516048/4081054) to your post. – Evghenii Todorov Jul 20 '15 at 13:43
  • 1
    Did you mean `"yyyy-MM-dd'T'HH:mm:ssZ"` instead of `"yyyy-MM-dd'T'HH:mm:ss'Z'"`? – Jake Greene Jun 10 '16 at 18:54
  • @JakeGreene: Well, it depends of the time string format defined in your project. In my app I used `"yyyy-MM-dd'T'HH:mm:ss'Z'"`. – Evghenii Todorov Jun 14 '16 at 11:24
  • Yeah this didn't work for me either, I had to use the code in @Guillaume's post – jakehschwartz Mar 24 '17 at 16:11
9

Another, perhaps simpler, solution would be to do a map, for example:

case class GoogleDoc(id: String, etag: String, created: LocalDateTime)

object GoogleDoc {
  import org.joda.time.LocalDateTime
  import org.joda.time.format.ISODateTimeFormat

  implicit val googleDocReads: Reads[GoogleDoc] = (
      (__ \ "id").read[String] ~
      (__ \ "etag").read[String] ~
      (__ \ "createdDate").read[String].map[LocalDateTime](x => LocalDateTime.parse(x, ISODateTimeFormat.basicdDateTime()))
  )(GoogleDoc)
}

UPDATE

If you had a recurring need for this conversion, then you could create your own implicit conversion, it is only a couple of lines of code:

import org.joda.time.LocalDateTime
import org.joda.time.format.ISODateTimeFormat

implicit val readsJodaLocalDateTime = Reads[LocalDateTime](js =>
  js.validate[String].map[LocalDateTime](dtString =>
    LocalDateTime.parse(dtString, ISODateTimeFormat.basicDateTime())
  )
)
Alex
  • 1,470
  • 1
  • 11
  • 28