I’m using Jackson to serialize and deserialize objects. The problem is that sometimes I want to show a field and sometimes not.
Actually I’m using the @JsonIgnore to avoid the printing of the property when I don’t need it. When I need it I’m disabling the Property through
mapper.getSerializationConfig().disable(SerializationConfig.Feature.USE_ANNOTATIONS);
but this will disable also other annotations that I need.
How can I get the result that I need? Using Views? Any example?
A little pojo to understand what I want:
class User {
private String username;
@JsonIgnore
private String password;
// getter setter
}
writeToDB() {
mapper.getSerializationConfig().disable(SerializationConfig.Feature.USE_ANNOTATIONS);
mapper.writeValueAsString(user);
}
and through the REST API you can get the username without the password (thanks to the JsonIgnore)
I think you should handle this a different way, by creating a custom Jackson serializer that can selectively serialize/ignore the password.
Annotations like this should be considered immutable at runtime. There may be some reflection trick to extract the
JsonIgnoreand set the value, but, if so, this would be really heavy-handed.