为包含类型为Map的属性的case类定义Writed。 Json / Scala / PlayFramework

时间:2015-02-04 20:14:14

标签: json scala playframework combinators

我有一个以这种方式定义的案例类:

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

当我尝试以这种方式声明其写入时,我收到了一个错误:

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]

有人可以帮我指出我做错的地方吗?

我觉得我对此的理解读/写组合器有点不稳定。我在这里发布了相关问题(当我试图了解发生了什么时):What is this "and" in ScalaJsonCombinator (when defining a Writes)?

如果您知道实现写入和读取的最佳方式,那么&#34; Duh&#34;请分享。

提前致谢!

1 个答案:

答案 0 :(得分:2)

Writes没有预定义的Map[Int, String],只有Map[String, A]。这是因为所有JSON密钥必须是String,而Int密钥实际上没有任何意义。您可以通过定义将Int密钥转换为String s。

的密钥来解决此问题
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$2@50973f47