隐式json读取和写入列表

时间:2013-04-01 13:54:24

标签: json playframework playframework-2.1 salat

我遵循以下示例https://github.com/leon/play-salat/tree/master/sample。在我的数据模型中,我有列表,我试图采用该模型。

我收到以下编译错误:

[error] /home/malte/workspace/bdt/app/models/Ballot.scala:26: No Json deserializer found        for type List[models.Position]. Try to implement an implicit Writes or Format for this type.
[error]         "positions" -> b.positions,
[error]                          ^
[error] /home/malte/workspace/bdt/app/models/Ballot.scala:33: No Json deserializer found for type List[models.Position]. Try to implement an implicit Reads or Format for this type.
[error]       (__ \ 'values).read[List[Position]]).map { l => l.getOrElse(Nil) } ~

对于“Position”类型,我有隐式读写,但如何为List [Position]创建它?我隐含的写作和阅读:

implicit val ballotJsonWrite = new Writes[Ballot] {
  def writes(b: Ballot): JsValue = {
    Json.obj(
      "positions" -> b.positions,
      "title" -> b.title)
  }
}

implicit val ballotJsonRead = (
(__ \ 'positions).readNullable(
  (__ \ 'values).read[List[Position]]).map { l => l.getOrElse(Nil) } ~
  (__ \ 'title).read[String])(Ballot.apply _)

对于我跟着Create implicit json read for List collection which might be missing from input json的阅读,但我得到了上面提到的错误。

1 个答案:

答案 0 :(得分:0)

实际上现在我发现了最新版本的salat。 Json自动用于在模型和json对象之间移动。我删除了整个部分,没有顺利运行。

https://github.com/novus/salat/wiki/JSON