Adding custom serializers to jsonFormats in LiftJsonRequestBody

198 views
Skip to first unread message

Vikas Hazrati

unread,
May 30, 2012, 6:04:06 AM5/30/12
to scalat...@googlegroups.com
Hi,

I have a piece of code in which looks like this

class UserServlet extends ScalatraServlet with LiftJsonRequestBody {

I expect Json request coming from the client from which the user object is extracted like this

post("/save") {
    logger.info("Entered the UserServlet")
    parsedBody match {
      case JNothing =>
        halt(400, "Invalid Json")
      case json: JObject => {
        val user = json.extract[User]
        UserDAO.save(user)
      }
      case _ =>
        halt(400, "Unknown Json")
    }
  }

The user object is 

case class User(
  @Key("_id") id: ObjectId = new ObjectId,
  firstName: String,
  lastName: String,
...
...
)

I have a problem when i use ObjectId since there is no serializer for that. In other situations I could have added a serializer like this

 implicit val formats = new net.liftweb.json.DefaultFormats {
    override def dateFormatter = new SimpleDateFormat("dd/MM/yyyy")
  } + new EnumerationSerializer(EnumList) + new ObjectIdSerializer

when i do something similar here I get ambiguous references to Formats since jsonFormats is also present in LiftJsonRequestBody. Is there a way to add custom serializers to jsonFormats in LiftJsonRequestBody? Any other way?

Regards | Vikas

Ivan Porto Carerro

unread,
May 30, 2012, 6:07:35 AM5/30/12
to scalat...@googlegroups.com

You can override the jsonformats. Or use the liftjsonwithoutformats trait. I can't link atm on a phone

Ivan Porto Carrero

unread,
May 30, 2012, 6:20:31 AM5/30/12
to scalat...@googlegroups.com

Vikas Hazrati

unread,
May 30, 2012, 7:03:20 AM5/30/12
to scalat...@googlegroups.com
Cool thanks, I believe that is a part of 2.1 correct? I would upgrade and try
Reply all
Reply to author
Forward
0 new messages