-
Notifications
You must be signed in to change notification settings - Fork 14
Selective Persistence into CouchDB
Very often we need to have different property names in the JSON document than what is present in the Scala object. Sometimes we may also want to filter out some properties while persisting in the data store. The framework uses annotations to achieve these functionalities (much like the ones used by jcouchdb (http://code.google.com/p/jcouchdb/), the Java client of CouchDB) ..
case class Trade( @JSONProperty("Reference No") val ref: String, @JSONProperty("Instrument"){val ignoreIfNull = true} val ins: Instrument, val amount: Number )
When this class will be spitted out in JSON and stored in CouchDB, the properties will be renamed as suggested by the annotation. Also selective filtering is possible through usage of additional annotation properties as shown above.
Handling aggregate data members for JSON serialization is tricky, since erasure takes away information of the underlying types contained in the aggregates. e.g.
case class Person( lastName: String firstName: String, @JSONTypeHint(classOf[Address]) addresses: List[Address] )
Using the annotation makes it possible to get the proper types during runtime and generate the proper serialization format.