为包含类型为 Map 的 属性 的案例 class 定义写入。 Json / Scala / PlayFramework

Defining Writes for case class that contains property whose type is a Map . Json / Scala / PlayFramework

我有一个案例 class 是这样定义的:

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]

谁能帮我指出哪里做错了?

我觉得我对这个 reads/writes 组合子的理解有点不稳定。我在这里发布了相关问题(当我试图了解发生了什么时):

如果您知道为 "Duh" 实现写入和读取的最佳方法,请分享。

提前致谢!

Map[Int, String] 没有预定义的 Writes,只有 Map[String, A]。这是因为所有 JSON 键必须是 String,到 Int 键没有任何意义。您可以通过定义一个将 Int 键转换为 Strings.

来解决此问题
type MapIntString = Map[Int, String]

implicit val mapIntWrites = new Writes[MapIntString] {
    def writes(m: MapIntString): JsValue = 
        Json.toJson(m.map { case (k, v) => (k.toString, v)} )
}

scala> implicit val duhWrites = Json.writes[Duh]
duhWrites: play.api.libs.json.OWrites[Duh] = play.api.libs.json.OWrites$$anon@50973f47