1

one of my models includes a list of BSONObjectIDs:

case class User(
  _id: BSONObjectID = BSONObjectID.generate,
  email: String,
  favorite_ids: List[BSONObjectID] = List(),
  home_folder_id: Option[BSONObjectID] = None
)

unfortunately, the compiler complains with the following message:

No implicit format for List[reactivemongo.bson.BSONObjectID] available.

it complains in the last line of the following snippet.

  import play.api.libs.json._
  import reactivemongo.bson._
  import play.modules.reactivemongo.json.BSONFormats._
  import play.modules.reactivemongo.json._, ImplicitBSONHandlers._
  import play.modules.reactivemongo.json.collection._

  implicit val userFormat = Json.format[User]

Funny observation: the Option[BSONObjectID] is working when i comment the List[] line out.

Anyone know how to include a format for lists? I figured that should be available implicitly.

thanks

magegu
  • 530
  • 4
  • 18
  • Please look at the [documentation](http://reactivemongo.org/releases/0.11/documentation/tutorial/play2.html), it indicates the recommanded import for BSON/JSON conversions. – cchantep Jul 09 '15 at 17:42
  • i rechecked but could not find any import that i'm not using. Any specific import you found that i'm NOT using in the given example above? – magegu Jul 09 '15 at 19:37
  • If you read the indicated documentation, you would see "The following imports are recommanded to make sure JSON/BSON convertions are available. `import play.modules.reactivemongo.json._, ImplicitBSONHandlers._`". – cchantep Jul 09 '15 at 20:46
  • @cchantep thanks, but it does not fix the issue. I updated the snipped to make clear which imports i'm using now. – magegu Jul 10 '15 at 09:27

1 Answers1

2

You can try with snapshot "org.reactivemongo" %% "play2-reactivemongo" % "0.11.2.play24-SNAPSHOT".

scala> import play.modules.reactivemongo.json._
import play.modules.reactivemongo.json._

scala> import reactivemongo.bson._
import reactivemongo.bson._

scala> import play.api.libs.json._
import play.api.libs.json._

scala> implicitly[Reads[BSONObjectID]]
res0: play.api.libs.json.Reads[reactivemongo.bson.BSONObjectID] = play.modules.reactivemongo.json.BSONFormats$BSONObjectIDFormat$@4d27019c

scala> implicitly[Writes[BSONObjectID]]
res1: play.api.libs.json.Writes[reactivemongo.bson.BSONObjectID] = play.modules.reactivemongo.json.BSONFormats$BSONObjectIDFormat$@4d27019c

scala> implicitly[Format[BSONObjectID]]
res2: play.api.libs.json.Format[reactivemongo.bson.BSONObjectID] = play.modules.reactivemongo.json.BSONFormats$BSONObjectIDFormat$@4d27019c

scala> implicitly[Format[List[BSONObjectID]]]
res3: play.api.libs.json.Format[List[reactivemongo.bson.BSONObjectID]] = play.api.libs.json.DefaultFormat$$anon$4@43b5fbbd

scala> implicitly[Reads[JsObject]]
res4: play.api.libs.json.Reads[play.api.libs.json.JsObject] = play.api.libs.json.DefaultReads$JsObjectReads$@78a1f869

scala> implicitly[OWrites[BSONDocument]]
res5: play.api.libs.json.OWrites[reactivemongo.bson.BSONDocument] = play.modules.reactivemongo.json.ImplicitBSONHandlers$BSONDocumentWrites$@1763c4c3

The implicits are all provided by the unified import play.modules.reactivemongo.json._

cchantep
  • 9,118
  • 3
  • 30
  • 41