-2

I'm trying to parse my case classes to and from JsObject but get hit with an overloaded method error that isn't clear to me.

The error message says "overloaded method value apply with alternatives".

Here is the reads code:


import play.api.libs.json._
import play.api.libs.functional.syntax._


implicit val accountCreatedReads: Reads[AccountCreated] = (
      (JsPath \ "eventId").read[String] and
      (JsPath \ "timestamp").read[OffsetDateTime] and
      (JsPath \ "accountId").read[String] and
      (JsPath \ "accountAttributes").readNullable[Attributes] and
      (JsPath \ "expirationTime").readNullable[Long]
  )(AccountCreated.apply _)

And here is the case class:

case class AccountCreated(
  eventId: String,
  timestamp: OffsetDateTime,
  accountId: String,
  accountAttributes: Option[Attributes] = None,  // This is the line the error refers to
  expirationTime: Option[Long] = None
)

Here is the class that represents the attributes:

type Attributes = JsValue

Can someone help me out here? This is the first time I've used this part of play-json, so I'm wondering what I'm doing wrong.

Chaitanya
  • 3,590
  • 14
  • 33
User
  • 168
  • 1
  • 3
  • 19

1 Answers1

1

So you want to read Attributes as Json.

 implicit val accountCreatedReads: Reads[AccountCreated] = (
      (JsPath \ "eventId").read[String] and
      (JsPath \ "timestamp").read[OffsetDateTime] and
      (JsPath \ "accountId").read[String] and
      (JsPath \ "accountAttributes").readNullable[String].map(Json.parse(_)) and //returns JsValue
      (JsPath \ "expirationTime").readNullable[Long]
  )(AccountCreated.apply _)

If it is really a JSON then

(JsPath \ "accountAttributes").readNullable[String].map(Json.parse(_))

should be able to parse it without a problem.

Dionysis Nt.
  • 955
  • 1
  • 6
  • 16