解析重载方法时出现 Scala 错误
Scala error while resolving overloaded methods
我在尝试使用 Play 的 Json 库反序列化 json 字符串时遇到以下错误。我知道它无法解析为重载方法,但我不明白为什么?
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
^
以上异常是由于(我正在使用 Play 的 JSON 库 - 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)
}
我什至不确定语法的实际含义?我看到 =>
用于匿名函数,其中右侧是参数,左侧是函数表达式。但是我不确定异常中的 B 指的是什么以及如何解释 2 个方法签名?
在jsonReads
中,(Event)
需要为(Event.apply _)
。应用读取组合器的函数必须与其签名匹配,在本例中为 Event.apply _
.
Event
本身指的是伴生对象,其类型为 Event.type
。因此错误消息片段 cannot be applied to (com.model.Event.type)
.
我在尝试使用 Play 的 Json 库反序列化 json 字符串时遇到以下错误。我知道它无法解析为重载方法,但我不明白为什么?
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
^
以上异常是由于(我正在使用 Play 的 JSON 库 - 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)
}
我什至不确定语法的实际含义?我看到 =>
用于匿名函数,其中右侧是参数,左侧是函数表达式。但是我不确定异常中的 B 指的是什么以及如何解释 2 个方法签名?
在jsonReads
中,(Event)
需要为(Event.apply _)
。应用读取组合器的函数必须与其签名匹配,在本例中为 Event.apply _
.
Event
本身指的是伴生对象,其类型为 Event.type
。因此错误消息片段 cannot be applied to (com.model.Event.type)
.