我是Scala和Play的新手,我正在尝试将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 \ "_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.
此(JsPath \ "_id").writeNullable[BSONObjectID]
也会生成相同的错误。
我想格式化我的json请求。所以我使用自定义格式化程序如下:
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")
}}
我的Json请求如下:
{
"_id":{"$oid":"54fd4b7084071e6a6ab13cee"},
"name" : "Akka",
"age" : 30,
"created" : 1425886070013
}
当我发送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
答案 0 :(得分:0)
在这种情况下,我们只需在Case
类中初始化自定义格式化程序。我不确定,为什么这个游戏不能自动拍照。现在的代码如下:
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 })}