I'm using Bowler framework for some REST API's (internally uses lift-json module for heavy lifting) and have the following case class:
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.
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
This is fixed. Needed to define my own RenderStrategy class in order to override the formats declaration. This post has more details on it http://blog.recursivity.com/post/5433171352/how-bowler-does-rendering-maps-requests-to-objects