Last active
December 19, 2015 06:59
-
-
Save denen99/5915266 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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