【问题标题】:No Json deserializer found for type Option[reactivemongo.bson.BSONObjectID]找不到类型 Option[reactivemongo.bson.BSONObjectID] 的 Json 反序列化器
【发布时间】:2013-05-29 16:41:47
【问题描述】:

我得到一个:

No Json deserializer found for type Option[reactivemongo.bson.BSONObjectID]. Try to implement an implicit Reads or Format for this type.

尝试反序列化我的评论对象时。

评论:

case class Review(override var id: Option[BSONObjectID] = None,
                  override var createdAt: Option[DateTime] = None,
                  override var updatedAt: Option[DateTime] = None,
                  grade: Int,
                  text: String,
                  originIPAddress: Option[String],
                  status: ReviewStatus,
                  origin: ReviewOrigin,
                  rId: Option[Long],
                  userId: Option[Long]
                  ) extends TemporalModel

object Review {

  import mongo_models.enums.EnumFormat._
  implicit val reviewStatusReads = enumReads(ReviewStatus)
  implicit val reviewOriginReads = enumReads(ReviewOrigin)

  implicit val reviewReads: Reads[Review] = (
    (__ \ "id").read[Option[BSONObjectID]] and
      (__ \ "createdAt").read[Option[DateTime]] and
      (__ \ "updatedAt").read[Option[DateTime]] and
      (__ \ "grade").read[Int] and
      (__ \ "text").read[String] and
      (__ \ "originIPAddress").read[Option[String]] and
      (__ \ "status").read[ReviewStatus] and
      (__ \ "origin").read[ReviewOrigin] and
      (__ \ "rId").read[Option[Long]] and
      (__ \ "userId").read[Option[Long]]
    )(Review.apply _)

  implicit val reviewWrites: Writes[Review] = (
    (__ \ "id").write[Option[BSONObjectID]] and
      (__ \ "createdAt").write[Option[DateTime]] and
      (__ \ "updatedAt").write[Option[DateTime]] and
      (__ \ "grade").write[Int] and
      (__ \ "text").write[String] and
      (__ \ "originIPAddress").write[Option[String]] and
      (__ \ "status").write[ReviewStatus] and
      (__ \ "origin").write[ReviewOrigin] and
      (__ \ "rId").write[Option[Long]] and
      (__ \ "userId").write[Option[Long]]
    )(unlift(Review.unapply))



  val form = Form(
    mapping(
      "id" -> optional(of[String] verifying pattern(
        """[a-fA-F0-9]{24}""".r,
        "constraint.objectId",
        "error.objectId")),
      "creationDate" -> optional(of[Long]),
      "updateDate" -> optional(of[Long]),
       "grade" -> number,
       "text" -> text(minLength = 30, maxLength = 5000),
      "originIPAddress" -> optional(of[String]),
      "status" -> text,
      "origin" -> text,
      "rId" -> optional(of[Long]),
      "userId" -> optional(of[Long])
    ) {
      (id, createdAt, updatedAt, grade, text, originIPAddress, status, origin, rId, userId) =>
        Review(
          id.map(new BSONObjectID(_)),
          createdAt.map(new DateTime(_)),
          updatedAt.map(new DateTime(_)),
          grade,
          text,
          originIPAddress,
          ReviewStatus.withName(status),
          ReviewOrigin.withName(origin),
          rId,
          userId
        )
    } {
      review => {
        Some(
          (review.id.map(_.stringify)),
          review.createdAt.map(_.getMillis),
          review.updatedAt.map(_.getMillis),
          review.grade,
          review.text,
          review.originIPAddress,
          review.status.toString,
          review.origin.toString,
          review.rId,
          review.userId
        )
      }
    }
  )
}

【问题讨论】:

    标签: mongodb scala playframework playframework-2.1 reactivemongo


    【解决方案1】:

    奇怪! 我的 Intellij IDEA 12 无法识别导入以及优化导入时

    import play.modules.reactivemongo.json.BSONFormats._
    

    已删除导致错误。

    也可以创建一个自定义格式对象来将 BSONObjectID 转换为 json。

    implicit object BSONObjectIDFormat extends Format[BSONObjectID] {
        def writes(objectId: BSONObjectID): JsValue = JsString(objectId.toString())
        def reads(json: JsValue): JsResult[BSONObjectID] = json match {
          case JsString(x) => {
            val maybeOID: Try[BSONObjectID] = BSONObjectID.parse(x)
            if(maybeOID.isSuccess) JsSuccess(maybeOID.get) else {
              JsError("Expected BSONObjectID as JsString")
            }
          }
          case _ => JsError("Expected BSONObjectID as JsString")
        }
      }
    

    但是在这种情况下导入就足够了。

    【讨论】:

    • 嘿 @jakob 我正在尝试使用 BSONObjectID 作为案例类中的 id 类型。但在阅读中我得到了一个错误:not enough arguments for method writeNullable: (implicit w: play.api.libs.json.Writes[reactivemongo.bson.BSONObjectID])play.api.libs.json.OWrites[Option[reactivemongo.bson.BSONObjectID]]. Unspecified value parameter w.。仍然没有得到任何有效的解决方案。我想念什么?
    【解决方案2】:

    按照documentation使用这个:import reactivemongo.play.json._

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2018-09-01
      • 1970-01-01
      • 2017-01-19
      • 1970-01-01
      • 1970-01-01
      • 2014-06-30
      • 2011-09-16
      相关资源
      最近更新 更多