我有一个这样定义的案例类:
scala> import play.api.libs.json._
import play.api.libs.json._
scala> import play.api.libs.functional.syntax._
import play.api.libs.functional.syntax._
scala> type MapIntString = Map[Int, String]
defined type alias MapIntString
case class Duh(a: MapIntString)
defined class Duh
当我尝试以这种方式声明其 Writes 时,出现错误:
scala> implicit val duhWrites = Json.writes[Duh]
<console>:25: error: No implicit Writes for MapIntString available.
implicit val duhWrites = Json.writes[Duh]
所以我求助于这个......,仍然有一个错误:
scala> implicit val duhWrites: Writes[Duh] = (
| (JsPath \ "a").write[MapIntString]
| )(unlift(Duh.unapply _))
<console>:26: error: overloaded method value write with alternatives:
(t: MapIntString)(implicit w: play.api.libs.json.Writes[MapIntString])play.api.libs.json.OWrites[play.api.libs.json.JsValue] <and>
(implicit w: play.api.libs.json.Writes[MapIntString])play.api.libs.json.OWrites[MapIntString]
cannot be applied to (Duh => MapIntString)
(JsPath \ "a").write[MapIntString]
有人可以帮我指出我做错了什么吗?
我觉得我对这个读/写组合器的理解有点不稳定。我在这里发布了相关问题(当我试图了解发生了什么时):ScalaJsonCombinator 中的这个“和”是什么(定义写入时)?
如果您知道为“Duh”实现写入和读取的最佳方式,请分享。
提前致谢!