找不到类型为play.api.libs.json.JsObject的JsObject Json对象怎么办?

内容来源于 Stack Overflow,并遵循CC BY-SA 3.0许可协议进行翻译与使用

  • 回答 (2)
  • 关注 (0)
  • 查看 (20)

我有以下代码在引用时在控制台应用程序中工作 "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 collection = 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")。

正如更新文档所示,对于默认的BSON / JSON转换,建议具有:import play.modules.reactivemongo.json._, ImplicitBSONHandlers._

用户回答回答于

在我的情况下,我给ReactiveMongo(insert)提供了一个JsValue而不是一个JsObject。为了解决这个问题,后面加入import play.modules.reactivemongo.json._,我也不得不改变我隐WritesOWrites

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(...)

扫码关注云+社区