Last active
May 18, 2016 03:51
-
-
Save crocker/ddd122d4c00ef8a1109e6a42dede4518 to your computer and use it in GitHub Desktop.
@JSONVIEW with Scala Object Mapper
This file contains 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
// define your json views | |
object MyViews { | |
class ViewA {} | |
class ViewB {} | |
class ViewC {} | |
} | |
// configure your object mapper | |
val mapper = new ObjectMapper with ScalaObjectMapper | |
mapper.registerModule(DefaultScalaModule) | |
mapper.setConfig(mapper.getDeserializationConfig.withView(classOf[ViewA])) | |
mapper.disable(MapperFeature.DEFAULT_VIEW_INCLUSION) | |
// test it out | |
class User( | |
id: Int, | |
@JsonView(Array(classOf[ViewA])) name: String | |
) | |
val user = new User(1, "Test") | |
val json = mapper.writeValueAsString(user) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment