Json format with Play 2.4 / Scala



我知道这个问题已经被问了上千次了,但是我不明白为什么这个代码不能正常工作。

case class Partner
(_id: Option[BSONObjectID], name: String, beacon: Option[Beacon])
class PartnerFormatter @Inject() (val beaconDao: BeaconDao){
  implicit val partnerReads: Reads[Partner] = (
      (__  "_id").readNullable[String]and
      (__  "name").read[String] and
      (__  "beacon").read[String]
    )((_id, name, beaconID) => Partner(_id.map(BSONObjectID(_)), name, Await.result(beaconDao.findById(beaconID), 1 second))))
  implicit val partnerWrites: Writes[Partner] = (
        (JsPath  "_id").writeNullable[String].contramap((id: Option[BSONObjectID]) => Some(id.get.stringify)) and
        (JsPath  "name").write[String] and
        (JsPath  "beacon").writeNullable[String].contramap((beacon: Option[Beacon]) => Some(beacon.get._id.get.stringify))
      )(unlift(Partner.unapply))
}

我面对着

No Json deserializer found for type models.Partner. Try to implement an implicit Reads or Format for this type

No Json deserializer found for type models.Partner. Try to implement an implicit Writes or Format for this type

您需要在Partner 对象中定义阅读器和写入器。

这是一个例子,https://github.com/luongbalinh/play-mongo/blob/master/app/models/User.scala

最新更新