我将Bowler框架用于一些REST API(内部使用lift-json模块进行繁重的提升),并具有以下case类:
case class Item(_id : ObjectId, name : String, value : String)当我将这个case对象返回给客户端时,我需要包含_id字段的值。但是,_id列在Json输出中作为空列表返回,而不是它的实际值。
{"_id":{},"name":"Id Test","value":"id test"}任何关于如何解决这个问题的建议都将不胜感激。
更新:我试着为它使用定制的序列化程序,但是由于某些原因,它没有被调用!
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发布于 2011-12-24 21:16:18
这是固定的。为了覆盖格式声明,需要定义我自己的RenderStrategy类。这篇文章有更多关于它的细节http://blog.recursivity.com/post/5433171352/how-bowler-does-rendering-maps-requests-to-objects
https://stackoverflow.com/questions/8614996
复制相似问题