JSON Views can be used to serialize/deserialize objects.
example:
public class Views {
public static class Public {
}
}
public class User {
public int id;
@JsonView(Views.Public.class)
public String name;
}
By default - all properties not explicitly marked as being part of a view, are serialized. We are disabling that behavior with the handy DEFAULT__VIEW__INCLUSION feature.
@Test
public void whenUseJsonViewToSerialize_thenCorrect()
throws JsonProcessingException {
User user = new User(1, "John");
ObjectMapper mapper = new ObjectMapper();
mapper.disable(MapperFeature.DEFAULT_VIEW_INCLUSION);
String result = mapper
.writerWithView(Views.Public.class)
.writeValueAsString(user);
assertThat(result, containsString("John"));
assertThat(result, not(containsString("1")));
}
JsonView is kinda of filter. Some fields would not be serialized.