找不到类型为Seq [models.Comment]的Json格式化程序尝试为此类型实现隐式格式

时间:2015-06-04 15:52:41

标签: scala playframework playframework-2.0

你好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]
  }

1 个答案:

答案 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))
}