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

No Json serializer as JsObject found for type play.api.libs.json.JsObject

引用 "org.reactivemongo" %% "play2-reactivemongo" % "0.10.5.0.akka23"

时,我有以下代码可在控制台应用程序中运行

当我更新对 "org.reactivemongo" % "play2-reactivemongo_2.11" % "0.11.0.play23-M3" 的引用时,我得到:

No Json serializer as JsObject found for type play.api.libs.json.JsObject. Try to implement an implicit OWrites or OFormat for this type.

import org.joda.time.DateTime
import reactivemongo.bson.BSONObjectID
import play.modules.reactivemongo.json.BSONFormats._

case class GoogleToken
(
  id: Option[BSONObjectID],
  name: String,
  emailAddress: String,
  refreshToken: String,
  expires: DateTime
  )

object GoogleToken {

  import play.api.libs.json.Json

  // Generates Writes and Reads
  implicit val googleTokenFormat = Json.format[GoogleToken]
}

然后

val 集合 = db.collectionJSONCollection

val query = Json.obj()
val cursor = collection.find(query).
  cursor[GoogleToken](ReadPreference.nearest).
  collect[List]()

我做错了什么?

ReactiveMongo 0.11 的最终版本已经发布 ("org.reactivemongo" %% "play2-reactivemongo" % "0.11.0.play23")。

updated documentation 所示,对于默认 BSON/JSON 转换,建议具有:import play.modules.reactivemongo.json._, ImplicitBSONHandlers._.

在我的例子中,我用 JsValue 而不是 JsObject 来喂 ReactiveMongo (insert)。为了修复它,在添加 import play.modules.reactivemongo.json._ 之后,我还必须更改 OWrites 中的隐式 Writes

来自

implicit val myWrites: Writes[A] = new Writes[A] {
  def writes(a: A) = Json.obj(...)

implicit val myWrites: OWrites[A] = new OWrites[A] {  <-- NOTE THE 'O' before 'Writes'
  def writes(a: A) = Json.obj(...)

我的添加后成功了: 导入 play.modules.reactivemongo.json._ 导入 play.modules.reactivemongo.json.collection._

对我来说,添加这个导入是有效的。

import play.modules.reactivemongo.json._

尝试添加

进口reactivemongo.play.json._