Skip to content

Instantly share code, notes, and snippets.

@denen99
Last active December 19, 2015 06:59
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save denen99/5915266 to your computer and use it in GitHub Desktop.
Save denen99/5915266 to your computer and use it in GitHub Desktop.
controller
-------------
package com.example.app
class MyController extends ScalatraServlet with JacksonJsonSupport {
implicit val jsonFormats = DefaultFormats + new UserSerializer
before() {
contentType = formats("json")
}
get("/") {
User.find
}
}
Model
----------
package com.example.app
case class User (id: Int, email: String)
object User {
def find = { new User(1,"Adam")}
}
Serializer
-------------------
package com.example.app
class UserSerializer extends CustomSerializer[User](format => ({
case JObject(
("id", JString(id)) ::
("email", JString(email)) :: Nil) =>
new User(2,"Adam2")
}, {
case user: User =>
JObject.apply(
"email" -> JString(user.email),
"id" -> JInt(user.id) )
}))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment