Lost In Code
Lost In Code

Reputation: 504

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?

Upvotes: 0

Views: 1320

Answers (1)

cchantep
cchantep

Reputation: 9168

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")
    }
  }
}

Upvotes: 2

Related Questions