Scala 从ObjectId到case类BSONObjectID的ReactiveMongo映射

Scala 从ObjectId到case类BSONObjectID的ReactiveMongo映射,scala,playframework-2.3,play-reactivemongo,Scala,Playframework 2.3,Play Reactivemongo,我是Scala新手,我正在尝试将ScalaBSONObjectID映射到mongoObjectId。我从互联网上获得了许多示例,但仍然陷入了一个编译时错误。下面是我的案例类的代码: case class UserDetail( val _id: Option[BSONObjectID], val name: String, val age: Double, var created: Option[Long] ) object UserDetail{ implicit val us

我是Scala新手,我正在尝试将Scala
BSONObjectID
映射到mongo
ObjectId
。我从互联网上获得了许多示例,但仍然陷入了一个编译时错误。下面是我的案例类的代码:

case class UserDetail(
 val _id: Option[BSONObjectID],
 val name: String,
 val age: Double,
 var created: Option[Long]
)  

object UserDetail{
 implicit val userDetailReads: Reads[UserDetail] = (
 (JsPath \ "_id").readNullable[BSONObjectID] and
 (JsPath \ "name").read[String] and
 (JsPath \ "age").read[Double] and
 (JsPath \ "created").readNullable[Long]
)(UserDetail.apply _)

implicit val userDetailWrites: Writes[UserDetail] = (
(JsPath \ "_id").writeNullable[BSONObjectID]and
(JsPath \ "name").write[String] and
(JsPath \ "age").write[Double] and
(JsPath \ "created").writeNullable[Long]
)(unlift { UserDetail.unapply })}
(JsPath\“\u id”).readNullable[BSONObjectID]
生成编译时错误,如下所示:

not enough arguments for method readNullable: (implicit r: play.api.libs.json.Reads[reactivemongo.bson.BSONObjectID])play.api.libs.json.Reads[Option[reactivemongo.bson.BSONObjectID]]. Unspecified value parameter r.
not enough arguments for method readNullable: (implicit r: play.api.libs.json.Reads[reactivemongo.bson.BSONObjectID])play.api.libs.json.Reads[Option[reactivemongo.bson.BSONObjectID]]. Unspecified value parameter r.
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")
}}
{
"_id":{"$oid":"54fd4b7084071e6a6ab13cee"},
"name" : "Akka",
"age" : 30,
"created" : 1425886070013
}
case class UserDetail(
 val _id: Option[BSONObjectID],
 val name: String,
 val age: Double,
 var created: Option[Long]
)  

object UserDetail{

 implicit val idFormatter = BSONObjectIDFormat

 implicit val userDetailReads: Reads[UserDetail] = (
 (JsPath \ "_id").readNullable[BSONObjectID] and
 (JsPath \ "name").read[String] and
 (JsPath \ "age").read[Double] and
 (JsPath \ "created").readNullable[Long]
)(UserDetail.apply _)

 implicit val userDetailWrites: Writes[UserDetail] = (
 (JsPath \ "_id").writeNullable[BSONObjectID]and
 (JsPath \ "name").write[String] and
 (JsPath \ "age").write[Double] and
 (JsPath \ "created").writeNullable[Long]
 )(unlift { UserDetail.unapply })}
(JsPath\“\u id”).writeNullable[BSONObjectID]
也会生成相同的错误

我想格式化我的json请求。因此,我使用自定义格式化程序,如下所示:

not enough arguments for method readNullable: (implicit r: play.api.libs.json.Reads[reactivemongo.bson.BSONObjectID])play.api.libs.json.Reads[Option[reactivemongo.bson.BSONObjectID]]. Unspecified value parameter r.
not enough arguments for method readNullable: (implicit r: play.api.libs.json.Reads[reactivemongo.bson.BSONObjectID])play.api.libs.json.Reads[Option[reactivemongo.bson.BSONObjectID]]. Unspecified value parameter r.
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")
}}
{
"_id":{"$oid":"54fd4b7084071e6a6ab13cee"},
"name" : "Akka",
"age" : 30,
"created" : 1425886070013
}
case class UserDetail(
 val _id: Option[BSONObjectID],
 val name: String,
 val age: Double,
 var created: Option[Long]
)  

object UserDetail{

 implicit val idFormatter = BSONObjectIDFormat

 implicit val userDetailReads: Reads[UserDetail] = (
 (JsPath \ "_id").readNullable[BSONObjectID] and
 (JsPath \ "name").read[String] and
 (JsPath \ "age").read[Double] and
 (JsPath \ "created").readNullable[Long]
)(UserDetail.apply _)

 implicit val userDetailWrites: Writes[UserDetail] = (
 (JsPath \ "_id").writeNullable[BSONObjectID]and
 (JsPath \ "name").write[String] and
 (JsPath \ "age").write[Double] and
 (JsPath \ "created").writeNullable[Long]
 )(unlift { UserDetail.unapply })}
我的Json请求如下:

not enough arguments for method readNullable: (implicit r: play.api.libs.json.Reads[reactivemongo.bson.BSONObjectID])play.api.libs.json.Reads[Option[reactivemongo.bson.BSONObjectID]]. Unspecified value parameter r.
not enough arguments for method readNullable: (implicit r: play.api.libs.json.Reads[reactivemongo.bson.BSONObjectID])play.api.libs.json.Reads[Option[reactivemongo.bson.BSONObjectID]]. Unspecified value parameter r.
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")
}}
{
"_id":{"$oid":"54fd4b7084071e6a6ab13cee"},
"name" : "Akka",
"age" : 30,
"created" : 1425886070013
}
case class UserDetail(
 val _id: Option[BSONObjectID],
 val name: String,
 val age: Double,
 var created: Option[Long]
)  

object UserDetail{

 implicit val idFormatter = BSONObjectIDFormat

 implicit val userDetailReads: Reads[UserDetail] = (
 (JsPath \ "_id").readNullable[BSONObjectID] and
 (JsPath \ "name").read[String] and
 (JsPath \ "age").read[Double] and
 (JsPath \ "created").readNullable[Long]
)(UserDetail.apply _)

 implicit val userDetailWrites: Writes[UserDetail] = (
 (JsPath \ "_id").writeNullable[BSONObjectID]and
 (JsPath \ "name").write[String] and
 (JsPath \ "age").write[Double] and
 (JsPath \ "created").writeNullable[Long]
 )(unlift { UserDetail.unapply })}
发送JSON请求时,出现以下错误:

[error] D:\play_projects\scala_play_sample\app\models\UserDetail.scala:35: No Js
on deserializer found for type reactivemongo.bson.BSONObjectID. Try to implement
an implicit Reads or Format for this type.
[error]     (JsPath \ "_id").readNullable[BSONObjectID] and
[error]                                  ^
[error] D:\play_projects\scala_play_sample\app\models\UserDetail.scala:42: No Js
on serializer found for type reactivemongo.bson.BSONObjectID. Try to implement a
n implicit Writes or Format for this type.
[error]     (JsPath \ "_id").writeNullable[BSONObjectID]and

在本例中,我们只需在
case
类中使用初始化自定义格式化程序。我不确定,为什么这部戏不自动拍摄这件事。现在代码如下所示:

not enough arguments for method readNullable: (implicit r: play.api.libs.json.Reads[reactivemongo.bson.BSONObjectID])play.api.libs.json.Reads[Option[reactivemongo.bson.BSONObjectID]]. Unspecified value parameter r.
not enough arguments for method readNullable: (implicit r: play.api.libs.json.Reads[reactivemongo.bson.BSONObjectID])play.api.libs.json.Reads[Option[reactivemongo.bson.BSONObjectID]]. Unspecified value parameter r.
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")
}}
{
"_id":{"$oid":"54fd4b7084071e6a6ab13cee"},
"name" : "Akka",
"age" : 30,
"created" : 1425886070013
}
case class UserDetail(
 val _id: Option[BSONObjectID],
 val name: String,
 val age: Double,
 var created: Option[Long]
)  

object UserDetail{

 implicit val idFormatter = BSONObjectIDFormat

 implicit val userDetailReads: Reads[UserDetail] = (
 (JsPath \ "_id").readNullable[BSONObjectID] and
 (JsPath \ "name").read[String] and
 (JsPath \ "age").read[Double] and
 (JsPath \ "created").readNullable[Long]
)(UserDetail.apply _)

 implicit val userDetailWrites: Writes[UserDetail] = (
 (JsPath \ "_id").writeNullable[BSONObjectID]and
 (JsPath \ "name").write[String] and
 (JsPath \ "age").write[Double] and
 (JsPath \ "created").writeNullable[Long]
 )(unlift { UserDetail.unapply })}

通常不建议在模型的case类中使用BSON特定类型。它将应用程序模型的定义与底层持久性库的详细实现联系起来。Hello@cchantep而不是BSON特定类型。我们需要用什么?因为当我使用字符串类型时,我无法执行诸如按id查找等查询。