Skip to content

Instantly share code, notes, and snippets.

@crocker
Last active May 18, 2016 03:51
Show Gist options
  • Save crocker/ddd122d4c00ef8a1109e6a42dede4518 to your computer and use it in GitHub Desktop.
Save crocker/ddd122d4c00ef8a1109e6a42dede4518 to your computer and use it in GitHub Desktop.
@JSONVIEW with Scala Object Mapper
// 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