没有找到类型为play.api.libs.json.JsValue的JsObject的Json序列化程序

时间:2015-09-23 12:23:57

标签: scala playframework playframework-2.0 reactivemongo play-reactivemongo

我正在尝试将项目从使用play2-reactivemongo版本0.10.5.0.akka23迁移到使用版本0.11.7.play23。我已经添加了以下导入来修复this question中解决的问题:

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]

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

1 个答案:

答案 0 :(得分:0)

也是这样。诀窍是删除

import play.modules.reactivemongo.json._

而是使用

import reactivemongo.play.json._

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