Here below is a case class that represents an user, and its companion object provides a BSONDocumentWriter
and BSONDocumentReader
to serialize/deserialize to/from BSON:
case class User(
id: Option[BSONObjectID],
name: String,
addresses: Option[List[BSONObjectID]]
)
object User {
implicit object UserWriter extends BSONDocumentWriter[User] {
def write(user: User) = BSONDocument(
"_id" -> user.id.getOrElse(BSONObjectID.generate),
"name" -> user.name,
"addresses" -> user.addresses
)
}
implicit object UserReader extends BSONDocumentReader[User] {
def read(doc: BSONDocument) = User(
doc.getAs[BSONObjectID]("_id"),
doc.getAs[String]("name").get,
doc.getAs[List[BSONObjectID]]("addresses").toList.flatten,
)
}
}
The code above does not compile because User.addresses
is an Option
and I always get the following error:
/home/j3d/Projects/test/app/models/User.scala:82: polymorphic expression cannot be instantiated to expected type;
[error] found : [B]List[B]
[error] required: Option[List[reactivemongo.bson.BSONObjectID]]
[error] doc.getAs[List[BSONObjectID]]("addresses").toList.flatten,
^
If the input BSON contains addresses
, how do I deserialize it into an Option[List[BSONObjectID]]
?
Why are you calling
toList
and thenflatten
? If I'm reading the docs correctly forBSONDocument
, thegetAs
method will return anOption
for typeT
where in your case, you are specifyingT
asList[BSONObjectID]
. When you calltoList
, you are basically unpacking theOption
to either an emptyList
(in the case of aNone
) or into the actualList
in the case of aSome
. And then theflatten
here doesn't seem to be doing anything relevant. Can't you just do it as: