Scala Play Framework Json Serializer错误

时间:2015-10-24 10:35:38

标签: scala playframework

尝试将类转换为Json。 这是我的课,它包括其他两个类:

case class GoodEdit(good: Good, data: List[(String, Option[GoodText])])
case class Good(
  id: Long,
  partnumber: Option[String] = None
)
case class GoodText(
  goodid: Long,
  languageid: Long,
  title: String,
  description: Option[String] = None)

以下是我的作家:

object GoodWriters {
  implicit val goodWrites = new Writes[Good] {
    def writes(good: Good) = Json.obj(
      "id" -> good.id,
      "partnumber" -> good.partnumber
    )
  }

  implicit val goodTextWrites = new Writes[GoodText] {
    def writes(goodText: GoodText) = Json.obj(
      "goodid" -> goodText.goodid,
      "languageid" -> goodText.languageid,
      "title" -> goodText.title,
      "description" -> goodText.description
    )
  }

  implicit val GoodEditWrites = new Writes[GoodEdit] {
    def writes(goodEdit: GoodEdit) = Json.obj(
      "good" -> Json.toJson(goodEdit.good),
      "data" -> Json.toJson(
        for ((lang, goodTextOpt) <- goodEdit.data ) yield Json.obj(lang -> goodTextOpt)
      )
    )
  }

然后,在控制器中我尝试使用这样:

  Action {
    import jwriters.GoodWriters._
    GoodEditAggregate.get(id).map{
      a => Ok(Json.toJson(a))
    }.getOrElse(Ok(Json.toJson(Json.obj("status" -> "error","message" -> "Can't find good with this id"))))
  }

编译器抱怨这部分:好的(Json.toJson(a))

  

找不到类型为GoodEdit的Json序列化程序。尝试实现一个   此类型的隐式写入或格式

无法理解错误。我已经为对象导入了作者

1 个答案:

答案 0 :(得分:0)

尝试全局导入Goodwrites