19

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?

Greg R
  • 1,670
  • 4
  • 26
  • 46
  • So I cannot fully recreate but comparing those two versions most of the dependencies remained the same but reactive mongo updated their internal libs. For what its worth that message your receiving is from ImplicitNotFound on OWrites. Its trying to take an instance of your type/class and create a JsObject. When you say console app - do you have app created or is this in the repl? I didn't see package names up above but perhaps they are just snippets. – Barry Jun 30 '15 at 18:15
  • It does make me wonder if something changed in the library where it wants a JsObject and the Format above on your case class is only creating reads/writes. – Barry Jun 30 '15 at 18:17
  • ^^ above is wrong went through source and it only requires Writes – Barry Jun 30 '15 at 18:34
  • Thanks for taking a look. Apparently import play.modules.reactivemongo.json._ was required – Greg R Jun 30 '15 at 18:39

5 Answers5

22

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

Zoltán
  • 21,321
  • 14
  • 93
  • 134
cchantep
  • 9,118
  • 3
  • 30
  • 41
  • 3
    import play.modules.reactivemongo.json._ did the trick! Thanks! – Greg R Jun 30 '15 at 18:38
  • Note that the module version for Play 2.4 is `0.11.0.play24`. – cchantep Jun 30 '15 at 18:50
  • 1
    with `0.11.2.play23`, i've to do `import play.modules.reactivemongo.json.ImplicitBSONHandlers._` – k.c. sham Jul 15 '15 at 20:32
  • 1
    I'm using 0.11.4.play24 and import `import play.modules.reactivemongo.json._, ImplicitBSONHandlers._`. The problem remains the same: "No Json serializer as JsObject found for type play.api.libs.json.JsObject". I couldn't even compile https://github.com/sgodbillon/reactivemongo-demo-app – Pavel Kudinov Jul 27 '15 at 06:54
  • 9
    @pavel I stumbled over the same problem, after updating the dependencies of the reactivemongo-demo-app to Play 2.4.2 and ReactiveMongo 0.11.4.play24. I had to *remove* the import of `ImplicitBSONHandlers._` to make it work. – cbley Jul 28 '15 at 12:38
  • 1
    This may have been the solution for prior versions of reactivemongo but using `"org.reactivemongo" % "play2-reactivemongo_2.12" % "0.12.7-play26",` and above, if you're using the default json handlers, simply importing `reactivemongo.play.json.JsObjectDocumentWriter` will do the trick. – franklin May 21 '18 at 16:35
6

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(...)
ticofab
  • 7,551
  • 13
  • 49
  • 90
  • The `OWrites` is recommended when the specificity of writing an object is required. As soon as you write a `JsObject`, that's a good idea. – cchantep Jul 26 '15 at 10:35
  • 1
    If you use Play with ReactiveMongo, this is the solution you need. I changed all my `Format` to `OFormat` (you have to specify the type) and it worked. Example: `case class Person(name: String, age: Int) object Person { implicit val jsonFormat: OFormat[Person] = Json.format[Person] } ` Notice the O in OFormat. – borice Jun 04 '17 at 02:39
6

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

1

try to add

import reactivemongo.play.json._

0

For me adding this import worked.

import play.modules.reactivemongo.json._
Ashish Pushp
  • 228
  • 2
  • 9