所以我基本上有以下代码:
case class MyObj( value:String, unit: String)
case class ObjSeries( id: String, myObjs: Map[String, MyObj] )
然而,创建像:
这样的作家implicit val myObjWrites = Json.writes[MyObj]
implicit val objSeriesWrites: Writes[ObjSeries] = (
(JsPath \ "id").write[String] and
(JsPath \ "myObjs").write[Map[String, MyObj]]
)(unlift(ObjSeries.unapply))
失败,“重载方法值适用于替代方案 - 无法应用于......”。
我错过了什么?
以上当然是一个简单的例子;我正在研究的实际数据结构有点复杂。实际的例子是一个类:
case class ObservationSeries(
sourceId: Option[String],
geometry: Option[Point],
levels: Option[Seq[Level]],
referenceTime: Option[String],
observations: Option[Map[String, Observation]]
)
错误信息如下:
overloaded method value apply with alternatives:
[error] [B](f: B => (Option[String], Option[no.met.geometry.Point], Option[Seq[no.met.geometry.Level]], Option[String], Option[scala.collection.immutable.Map[String,models.Observation]]))(implicit fu: play.api.libs.functional.ContravariantFunctor[play.api.libs.json.OWrites])play.api.libs.json.OWrites[B] <and>
[error] [B](f: (Option[String], Option[no.met.geometry.Point], Option[Seq[no.met.geometry.Level]], Option[String], Option[scala.collection.immutable.Map[String,models.Observation]]) => B)(implicit fu: play.api.libs.functional.Functor[play.api.libs.json.OWrites])play.api.libs.json.OWrites[B]
[error] cannot be applied to (models.ObservationSeries => (Option[String], Option[no.met.geometry.Point], Option[Seq[no.met.geometry.Level]], Option[String], Option[scala.collection.Map[String,models.Observation]]))
[error] (JsPath \ "referenceTime").writeNullable[String] and
答案 0 :(得分:0)
不确定是否是复制错误,但第一个类缺少第二个字段的名称:
# ↓↓↓
case class ObjSeries(id: String, Map[String, MyObj] )
除此之外,你应该有所作为。你确定这段代码失败了吗?