ObjectId getting rendered as empty list

7 views
Skip to first unread message

Mrityunjay Kant

unread,
Dec 23, 2011, 5:33:25 AM12/23/11
to Bowler Users
Hi,

I have the following case class for storage in MongoDb:

case class Item(_id : ObjectId, name : String, value : String)

When I return this case object back to client I need to include value
for _id field. However, the _id column is being returned as an empty
list in Json output instead of its actual value.

{"_id":{},"name":"Id Test","value":"id test"}

Any pointers on how this can be fixed will be greatly appreciated.

Mrityunjay Kant

unread,
Dec 23, 2011, 9:26:15 AM12/23/11
to Bowler Users
Update: I tried using custom serializer for it but for some reason it
doesn't get called!

class ObjectIdSerializer extends Serializer[ObjectId] {
private val Class = classOf[ObjectId]

def deserialize(implicit format: Formats) = {
case (TypeInfo(Class, _), json) => json match {
case JObject(JField("_id", JString(s)) :: Nil) => new
ObjectId(s)
case x => throw new MappingException("Can't convert " + x + "
to ObjectId")
}
}

def serialize(implicit format: Formats) = {
case x: ObjectId => { println("\t @@@@@@@@Custom Serializer was
called!"); JObject(JField("_id", JString(x.toString)) :: Nil)}
}
}

implicit val formats = DefaultFormats + new ObjectIdSerializer

Wille Faler

unread,
Dec 23, 2011, 2:24:50 PM12/23/11
to bowler...@googlegroups.com
Hi,
I suspect this is because the Serializer doesn't get  set in the RenderStrategy: https://github.com/wfaler/Bowler/blob/master/core/src/main/scala/org/bowlerframework/view/DefaultRenderStrategy.scala

I think the fix for this is to make sure the JSONRenderStrategy in the same package can take an arbitrary number of serializers in it's constructor, then allowing this to be set via the RenderStrategy, and set a RenderStrategy with your configurations in the BowlerConfigurator.

If you want to fix it (should be relatively simple), I'd be happy to accept a pull request that sorts this out (the above mentioned are the three main places I think that you might want to look at).
Otherwise, please raise a ticket and I'll fix it next week, but at the moment I am indisposed due to Christmas for a few days. 

..also tempting you to fix it, because I'd love to see some more contributions. ;)
Reply all
Reply to author
Forward
0 new messages