Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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?

like image 850
Greg R Avatar asked Jun 30 '15 15:06

Greg R


4 Answers

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._.

like image 87
cchantep Avatar answered Sep 23 '22 00:09

cchantep


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(...)
like image 24
ticofab Avatar answered Sep 19 '22 00:09

ticofab


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

like image 6
user2904891 Avatar answered Sep 22 '22 00:09

user2904891


try to add

import reactivemongo.play.json._

like image 1
Abdelwahed Houbouby Avatar answered Sep 20 '22 00:09

Abdelwahed Houbouby