Answer coming from :

But more convenient directly here on StackOverFlow:

Sorry, I should have chimed in here sooner.

One of the long-standing problems with Rogue was that it was too easy to accidentally make a field that was not serializable as BSON, and have it fail at runtime (when you try to add that value to a DBObject) rather than at compile time.

I introduced the BSONType type class to try to address this. The upside is it catches BSON errors at compile time. The downside is you need to make a choice when it comes to case classes.

If you want to do this the "correct" way, define your case class plus a BSONType "witness" for that case class. To define a BSONType witness, you need to provide serialization from that type to a BSON type. Example:

 case class TestCC(v: Int)

 implicit object TestCCIsBSONType extends BSONType[TestCC] {
   override def asBSONObject(v: TestCC): AnyRef = {
     // Create a BSON object
     val ret = new BasicBSONObject
     // Serialize all the fields of the case class
     ret.put("v", v.v)

That said, this can be quite burdensome if you're doing it for each case class. Your second option is to define a generic witness that works for any case class, if you have a generic serialization scheme:

 implicit def CaseClassesAreBSONTypes[CC <: CaseClass]: BSONType[CC] =
new BSONType[CC] {
   override def asBSONObject(v: CC): AnyRef = {
     // your generic serialization code here, maybe involving formats

Hope this helps,

Related Query

More Query from same tag