我正在尝试将我的项目从使用play2-reactivemongo
版本迁移0.10.5.0.akka23
到使用版本0.11.7.play23
。我已经添加了以下导入来解决此问题中解决的问题:
import play.modules.reactivemongo.json._
对于以前的版本,以下代码有效:
val updateEntity = Json.obj("_id" -> Json.obj("$oid" -> id))
val entity = Json.parse(stringJson)
collection.update(updateEntity, entity)
但是,使用新版本,第三行给出了编译错误:
[error] No Json serializer as JsObject found for type play.api.libs.json.JsValue. Try to implement an implicit OWrites or OFormat for this type.
[error] collection.update(updateEntity, entity)
[error] ^
我尝试引入隐式OWriter
:
implicit val toJsObject: OWrites[JsValue] = OWrites.apply(_.as[JsObject])
但这会产生隐含的声明冲突:
[error] ambiguous implicit values:
[error] both value toJsObject of type play.api.libs.json.OWrites[play.api.libs.json.JsValue]
[error] and object JsObjectDocumentWriter in trait ImplicitBSONHandlers of type play.modules.reactivemongo.json.JsObjectDocumentWriter.type
[error] match expected type collection.pack.Writer[play.api.libs.json.JsObject]
[error] collection.update(updateEntity, entity)
[error] ^
将第二行更改为
val entity = Json.parse(stringJson).as[JsObject]
解决了这个问题,但我的代码中有很多这样的问题,我希望有一个更简单的解决方案。