1

I'm trying to migrate my project from using play2-reactivemongo version 0.10.5.0.akka23 to using version 0.11.7.play23. I've already added the following import to fix the problems addressed in this question:

import play.modules.reactivemongo.json._

With the former version, the following code worked:

val updateEntity = Json.obj("_id" -> Json.obj("$oid" -> id))
val entity = Json.parse(stringJson)
collection.update(updateEntity, entity)

however, using the new version, the third line gives a compile error:

[error] No Json serializer as JsObject found for type play.api.libs.json.JsValue. Try to implement an implicit OWrites or OFormat for this type.
[error]     collection.update(updateEntity, entity)
[error]                      ^

I've tried introducing an implicit OWriter:

implicit val toJsObject: OWrites[JsValue] = OWrites.apply(_.as[JsObject])

but that gives an implicit declaration conflict:

[error] ambiguous implicit values:
[error]  both value toJsObject of type play.api.libs.json.OWrites[play.api.libs.json.JsValue]
[error]  and object JsObjectDocumentWriter in trait ImplicitBSONHandlers of type play.modules.reactivemongo.json.JsObjectDocumentWriter.type
[error]  match expected type collection.pack.Writer[play.api.libs.json.JsObject]
[error]     collection.update(updateEntity, entity)
[error]                      ^

changing the second line to

val entity = Json.parse(stringJson).as[JsObject]

fixes the problem, but I have a lot of these around my code and I was hoping for a simpler solution.

Community
  • 1
  • 1
Zoltán
  • 21,321
  • 14
  • 93
  • 134
  • 1
    Why not write a function like , `def parseJson(json: String) = { Json.parse(json).as[JsObject] }` and use this function instead. – curious Sep 23 '15 at 12:56
  • @curious I was hoping for a magical import which would add the required implicit conversions which I'm missing. Creating the function you suggested would still require me to change all occurrences of `Json.parse` in my code, and with that I don't really see much advantage to just adding `.as[JsObject]` everywhere. – Zoltán Sep 23 '15 at 13:00

1 Answers1

0

Just got bit by this too. The trick is to remove

import play.modules.reactivemongo.json._

and instead use

import reactivemongo.play.json._

The play.modules version doesn't provide the identity OWriter.

Lanny Ripple
  • 419
  • 2
  • 7