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

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


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.