你好scala和playFramework大师,
我不明白为什么隐式Json.format[Comment]
的使用在我的代码中不起作用。从文档说明中,它应该与Format[Comment]
一样,但看起来并非如此。
以下是我的两个案例类及其随播广告的代码
case class ServiceTask(id: Option[String],
name: String,
description: String,
requiredInfo: String,
status: String,
approved: Boolean,
comments: Option[Seq[Comment]])
object ServiceTask {
implicit val serviceTaskFormat: Format[ServiceTask] = (
(JsPath \ "id").formatNullable[String] and
(JsPath \ "name").format[String] and
(JsPath \ "description").format[String] and
(JsPath \ "requiredInfo").format[String] and
(JsPath \ "status").format[String] and
(JsPath \ "approved").format[Boolean] and
(JsPath \ "comments").formatNullable[Seq[Comment]]
)(ServiceTask.apply _, unlift(ServiceTask.unapply))
}
和
case class Comment(id: Option[String],
authorID: BSONObjectID,
updatedAt: Option[DateTime] = None,
body: String)
object Comment {
implicit val commentFormat = Json.format[Comment]
}
答案 0 :(得分:3)
您似乎没有在commentFormat
中导入object serviceTaskFormat
。试试这个:
object ServiceTask {
import Comment._
implicit val serviceTaskFormat: Format[ServiceTask] = (
(JsPath \ "id").formatNullable[String] and
(JsPath \ "name").format[String] and
(JsPath \ "description").format[String] and
(JsPath \ "requiredInfo").format[String] and
(JsPath \ "status").format[String] and
(JsPath \ "approved").format[Boolean] and
(JsPath \ "comments").formatNullable[Seq[Comment]]
)(ServiceTask.apply _, unlift(ServiceTask.unapply))
}