Bade
Bade

Reputation: 709

Handling non-default nested Json Reads

So I have following JSON:

{
  "senderEmail" : "[email protected]",
  "recipientEmails" : ["[email protected]", "[email protected]"]
}

and would like to map it to case class:

case class Payload (senderEmail: String, recipientEmails: Seq[String])

using Play's Json Reads with email validator.

While it's trivial for a senderEmail, I'm having trouble with recipientEmails since it's both Seq and email so this will not work:

implicit val payloadRead: Reads[Payload] = (
    (JsPath \ "senderEmail").read[String](Reads.email) and
    (JsPath \ "recipientEmails").read[Seq[String]](Reads.seq))(Payload.apply _)

I'm getting overloaded method value read with alternatives.

So how can I combine both Reads.seq and Reads.email?

Upvotes: 0

Views: 35

Answers (1)

cchantep
cchantep

Reputation: 9168

Just keep it simple ...

scala> import play.api.libs.json._

scala> import play.api.libs.functional.syntax._

scala> case class Payload (senderEmail: String, recipientEmails: Seq[String])
defined class Payload

scala> implicit val reads: Reads[Payload] = (
     |   (JsPath \ "senderEmail").read(Reads.email) and
     |   (JsPath \ "recipientEmails").read(Reads.seq(Reads.email))
     | )(Payload.apply _)
reads: play.api.libs.json.Reads[Payload] = play.api.libs.json.Reads$$...

scala> Json.parse("""{
     |   "senderEmail" : "[email protected]",
     |   "recipientEmails" : ["[email protected]", "[email protected]"]
     | }""").validate[Payload]
res0: play.api.libs.json.JsResult[Payload] = JsSuccess(Payload([email protected],Vector([email protected], [email protected])),)

Upvotes: 4

Related Questions