option collection suggestion

35 views
Skip to first unread message

RB H

unread,
Jun 4, 2012, 10:07:49 AM6/4/12
to scala-salat
Hi,

As I understand it, Salat doesn't support collections wrapped in
Option. Any suggestion on workarounds for case class with optional
metafields?

case class TrackEvent(
consumerId: Long,
measurement: Map[String, Double],
date: Date = new Date(),
metafields: Option[Map[String, String]],
enabled: Option[Boolean])


Todd

rktoomey

unread,
Jun 4, 2012, 10:20:03 AM6/4/12
to scala-salat
An empty map?

RB H

unread,
Jun 4, 2012, 10:47:22 AM6/4/12
to scala-salat
Sorry, yes, I tried that, but I'm marshalling this case class from
incoming json and it's requiring metafields. I'm currently focusing
on it.

rktoomey

unread,
Jun 4, 2012, 10:52:15 AM6/4/12
to scala-salat
If you're marshalling, why not just define a default argument in your
case class?

case class TrackEvent(
consumerId: Long,
measurement: Map[String, Double],
date: Date = new Date(),
metafields: Map[String, String] = Map.empty,
enabled: Option[Boolean] = None)

Best,
Rose

RB H

unread,
Jun 4, 2012, 12:57:38 PM6/4/12
to scala-salat
Exactly what I've been trying, but lift-json doesn't support defaults:
https://github.com/lift/framework/issues/534. I'm using it to
marshall the incoming json into a TrackEvent. I don't think jerkson
supports it either, but not sure yet.

RB H

unread,
Jun 4, 2012, 2:17:51 PM6/4/12
to scala-salat
Because lift-json doesn't support defaults: https://github.com/lift/framework/issues/534
and that's what I'm using to construct TrackEvent.

I don't think jerkson does either.

I attempted workaround with alternative constructors, but then grater
will not work.


Todd

rktoomey

unread,
Jun 4, 2012, 4:49:35 PM6/4/12
to scala-salat
As a cheap, ugly, unsupported but entirely successful workaround,
output JSON to a string, use the mongo JSON parser to turn it into a
DBObject, and then use the grater to turn it into the model object.
This is a bit Rube Goldberg but it works. Then you can come back and
deal with this stupidity later.

I should mention that Grater already supports toJSON (as lift-json
JObject). I am currently working on finishing up Scaladoc but it
would be easy enough to add fromJSON and transform a JObject to the
model object.

As far as supporting Option/collection - honestly, with the current
setup this is not easy to do because it was never an intended use
case. Perhaps surprisingly, a lot of people seem to want it! So I
will eventually add it.

On Jun 4, 4:29 pm, RB H <rbhd...@gmail.com> wrote:
> If someone were crazy enough to try to add support for collections wrapped
> in Option in salat, where should they start?

rktoomey

unread,
Aug 7, 2012, 10:39:09 AM8/7/12
to scala-salat
Hi Diego -

Salat deserializes a collection that is not present using the default
argument supplied by you in the case class constructor.

You can combine this with a setting in Context that suppresses
serializing any field whose value is equal to the supplied default
argument. Here's a gist showing what I mean:
https://gist.github.com/3285885

Now, this doesn't only affect collections - it affects all fields with
default arguments except a field with the name or mapping "_id" (via
@Key or using context overrides).

Depending on how you use your document collection, this might or might
not be suitable for your use. But it is very effective at shrinking
document size.

Best,
Rose

On Aug 7, 10:09 am, Diego Varese <diego.var...@gmail.com> wrote:
> In my case the map will have a value in a very small number of scenarios,
> it will usually be None. Using an empty Map as the default value means that
> the mongo db gets littered with empty maps all over the place. Is there a
> way to have empty collections be omitted when serializing to Mongo, and be
> recovered as empty collections when deserializing? How does Salat
> deserialize a collection field that is not present in the mongo entry?
Reply all
Reply to author
Forward
0 new messages