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

旧街凉风 提交于 2019-11-26 09:45:22

问题


I have the following code that works in a console app when referencing \"org.reactivemongo\" %% \"play2-reactivemongo\" % \"0.10.5.0.akka23\"

when I update the reference to \"org.reactivemongo\" % \"play2-reactivemongo_2.11\" % \"0.11.0.play23-M3\" I get:

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]
}

and then

val collection = db.collectionJSONCollection

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

What am I doing wrong?


回答1:


The final release of ReactiveMongo 0.11 has been published ("org.reactivemongo" %% "play2-reactivemongo" % "0.11.0.play23").

As indicated on the updated documentation, for the default BSON/JSON conversions, it's recommended to have: import play.modules.reactivemongo.json._, ImplicitBSONHandlers._.




回答2:


In my case, I was feeding ReactiveMongo (insert) with a JsValue instead than a JsObject. In order to fix it, behind adding import play.modules.reactivemongo.json._, I also had to change my implicit Writes in OWrites:

from

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

to

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



回答3:


Mine worked out after adding: import play.modules.reactivemongo.json._ import play.modules.reactivemongo.json.collection._




回答4:


For me adding this import worked.

import play.modules.reactivemongo.json._



回答5:


try to add

import reactivemongo.play.json._



来源:https://stackoverflow.com/questions/31142366/no-json-serializer-as-jsobject-found-for-type-play-api-libs-json-jsobject

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!