2013-04-01 43 views
1

我正在关注以下示例https://github.com/leon/play-salat/tree/master/sample。在我的数据模型中,我有列表,并试图采用该模型。隐式json读取和列表写入

我得到以下编译错误:

[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) } ~ 

对于类型“位置”我有隐含的读取和写入,但如何使其成为列表[位置]?我的隐写和读:

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读,但我得到了上述的错误。

回答

0

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

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