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

5

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?

Nationalism answered 30/6, 2015 at 15:24 Comment(4)
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.Predesignate
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.Predesignate
^^ above is wrong went through source and it only requires WritesPredesignate
Thanks for taking a look. Apparently import play.modules.reactivemongo.json._ was requiredNationalism
S
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._.

Straightway answered 30/6, 2015 at 18:18 Comment(6)
import play.modules.reactivemongo.json._ did the trick! Thanks!Nationalism
Note that the module version for Play 2.4 is 0.11.0.play24.Straightway
with 0.11.2.play23, i've to do import play.modules.reactivemongo.json.ImplicitBSONHandlers._Ankara
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 github.com/sgodbillon/reactivemongo-demo-appDiscounter
@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.Jujube
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.Illogicality
Q
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(...)
Qualified answered 15/7, 2015 at 20:45 Comment(2)
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.Straightway
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.Bouilli
M
6

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

Malarkey answered 13/8, 2015 at 22:7 Comment(0)
P
1

try to add

import reactivemongo.play.json._

Palindrome answered 21/5, 2019 at 18:10 Comment(0)
K
0

For me adding this import worked.

import play.modules.reactivemongo.json._
Ketone answered 20/7, 2018 at 14:27 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.