0

I'm writing a BSONDocumentReader and would like to create a BSONDocument from a JSON string in my test to verify whether it correctly builds the CC.

So far I haven't found an easy way to do so, can anyone provide an example?

Eddy
  • 1,662
  • 2
  • 21
  • 36
  • What have you tried? You can easily convert a Play `JsValue` to a `BSONValue` using the ReactiveMongo Play Json – cchantep Aug 12 '16 at 12:20
  • Well, that's part of the problem. I'm not using play - at least not in the part I'm working on - and I'm not very familiar with it. – Eddy Aug 12 '16 at 12:22
  • So you need to precise your question because "create a BSONDocument from a JSON string" is just that. And please also indicate what you have tried. – cchantep Aug 12 '16 at 13:17

1 Answers1

0

Eventually I found the solution on SO, see this answer

import reactivemongo.play.json._
import play.modules.reactivemongo.json.collection._

val googleJs: JsObject = Json.parse(google).as[JsObject]
val googleBson: BSONDocument = BSONFormats.toBSON(googleJs).get.asInstanceOf[BSONDocument]

val maybeAddress = AddressReader read googleBson
maybeAddress shouldBe defined
// ... and so on...

Just make sure you have the play2-reactivemongo dependency

Community
  • 1
  • 1
Eddy
  • 1,662
  • 2
  • 21
  • 36