Answer coming from : http://grokbase.com/t/gg/rogue-users/1367nscf80/how-to-update-a-record-with-mongocaseclassfield-when-case-class-contains-a-scala-enumeration#20130612woc3x7utvaoacu7tv7lzn4sr2q
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)
ret
}
}
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,
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With