【发布时间】:2014-08-21 07:59:42
【问题描述】:
我正在尝试使用 Play 2.x 将 Scala 案例类映射到 JSON。这适用于案例类的简单版本,但不适用于涉及 Seq 或对象列表时:然后我得到“没有隐式格式”和“没有找到未应用函数”错误。
我使用的代码如下:
case class Book(title: String, authors: Seq[Author])
case class Author(name: String)
我已使用Json.format 宏为此生成读取和写入:
implicit val bookFormat = Json.format[Book]
implicit val authorFormat = Json.format[Author]
但是现在当我编译我的代码时,我得到了以下错误:
Error:(25, 40) Play 2 Compiler:
/Users/erikp/Userfiles/projects/play/booksearch/app/models/user.scala:25: No implicit format for Seq[models.Author] available.
implicit val bookFormat = Json.format[Book]
^
如果没有 Seq,它可以很好地工作,但是使用 Seq,它会失败。我尝试将implicit val authorsFormat = Json.format[Seq[Author]] 添加到隐式转换器,但没有效果。
【问题讨论】:
-
以相反的顺序定义它们应该可以工作:implicit val authorFormat = Json.format[Author] implicit val bookFormat = Json.format[Book]
-
哇,我错过了。谢谢!顺便说一句,你还有一个建议不要扩大这个列表吗?我真的需要为每个案例类手动注册一个 Json.format 吗?
标签: json scala playframework