Search code examples
scaladatetimeplay-json

Play-Json parsing date time string to Reads[Instant]


I'm trying to enforce a validation rule that timestamps in input Json must have a timezone specified, using format DateTimeFormatter.ISO_OFFSET_DATE_TIME. When the input is incorrect, I want to return a message indicating bad format.

This snippet works to parse data in expected format:

implicit val instantReads = Reads[Instant] {
  js => js.validate[String].map[Instant](tsString =>
     Instant.from(OffsetDateTime.parse(tsString, DateTimeFormatter.ISO_OFFSET_DATE_TIME))
  )
}

But throws a DateTimeParseException if format is wrong.

How can I fix it to return JsError("Wrong datetime format") instead of throwing an exception?


Solution

  • You can use Read.flatMap instead.

    implicit val instantReads = Reads[Instant] {
      _.validate[String].flatMap[Instant] { tsString =>
        try { // or Try [T]
          JsSuccess (Instant.from(OffsetDateTime.parse(tsString, DateTimeFormatter.ISO_OFFSET_DATE_TIME)))
        } catch {
          case cause: Throwable =>
            JsError("Wrong datetime format")
        }
      }
    }