1

我正在尝试将我的项目从使用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]

解决了这个问题,但我的代码中有很多这样的问题,我希望有一个更简单的解决方案。

4

1 回答 1

0

刚刚也被这个咬到了。诀窍是删除

import play.modules.reactivemongo.json._

而是使用

import reactivemongo.play.json._

play.modules 版本不提供身份 OWriter。

于 2016-08-11T19:22:11.737 回答