No Json deserializer found for type Option[reactivemongo.bson.BSONObjectID]

痴心易碎 提交于 2019-12-05 02:51:00

Strange! My Intellij IDEA 12 didn't recognise the import and when I optimised the imports

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

was removed which created the error.

One could also create a custom Format object to translate the BSONObjectID to 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")
    }
  }

But the import is enough in this case.

Use this as per documentation: import reactivemongo.play.json._

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!