Im getting the following error while trying to use the Play `s Json library for de-serializing json string. I understand it is failing to resolve to an overloaded method but Im failing to understand why?
Error:(45, 50) overloaded method value apply with alternatives:
[B](f: B => (String, String, com.model.ESource.Value, com.model.Address, java.time.ZonedDateTime, java.time.ZonedDateTime))(implicit fu: play.api.libs.functional.ContravariantFunctor[play.api.libs.json.Reads])play.api.libs.json.Reads[B] <and>
[B](f: (String, String, com.model.ESource.Value, com.model.Address, java.time.ZonedDateTime, java.time.ZonedDateTime) => B)(implicit fu: play.api.libs.functional.Functor[play.api.libs.json.Reads])play.api.libs.json.Reads[B]
cannot be applied to (com.model.Event.type)
(JsPath \ "startTime").read[ZonedDateTime] and
^
the above exception is caused by ( Im using Play's JSON library - import play.api.libs
):
case class Event(name: String,
sourceId: String,
sourceType: ESource.ESource,
address: Address,
startTime: ZonedDateTime,
endTime: ZonedDateTime) {
val id = Array( name, startTime, endTime ).mkString("-")
def toJsonString(): String = Json.toJson(this)(Event.jsonWrites).toString()
def fromJsonString(jsonString: String): Event = {
val jv = Json.parse(jsonString)
Json.fromJson[Event](jv)(Event.jsonReads).get
}
}
object Event {
val jsonWrites: Writes[Event] = (
(JsPath \ "name").write[String] and
(JsPath \ "sourceId").write[String] and
(JsPath \ "sourceType").write[ESource.ESource] and
(JsPath \ "address").write[Address](Address.jsonWrites) and
(JsPath \ "startTime").write[ZonedDateTime] and
(JsPath \ "endTime").write[ZonedDateTime]
)(unlift(Event.unapply))
val jsonReads: Reads[Event] = (
(JsPath \ "name").read[String] and
(JsPath \ "sourceId").read[String] and
(JsPath \ "sourceType").read[ESource.ESource] and
(JsPath \ "address").read[Address](Address.jsonReads) and
(JsPath \ "startTime").read[ZonedDateTime] and
(JsPath \ "endTime").read[ZonedDateTime]
)(Event)
}
Im not even sure what the syntax actually means? I see that =>
is used for anonymous functions where the right side are arguments and left would be the function expression. But Im not sure what the B in the exception refers to and how the 2 method signatures are to be interpreted?
In jsonReads
, (Event)
needs to be (Event.apply _)
. The function that the reads combinators are being applied to must match their signature, which in this case is Event.apply _
.
Event
by itself is referring to the companion object, which has type Event.type
. Hence the error message snippet cannot be applied to (com.model.Event.type)
.