Ideally, it would look much like this:
List props = objectMapper.getKnownProperties(MyPojo.class);
Alas, there is no such met
It's possible to ignore all annotations by using a dummy AnnotationIntrospector:
objectMapper.setAnnotationIntrospector(new AnnotationIntrospector(){
@Override public Version version() {
return Version.unknownVersion();
}
});
With Jackson, you can introspect a class and get the available JSON properties using:
// Construct a Jackson JavaType for your class
JavaType javaType = mapper.getTypeFactory().constructType(MyDto.class);
// Introspect the given type
BeanDescription beanDescription = mapper.getSerializationConfig().introspect(javaType);
// Find properties
List<BeanPropertyDefinition> properties = beanDescription.findProperties();
If you have @JsonIgnoreProperties class level annotations, check this answer.
Depending on your exact needs, JsonFilter
could also work (f.ex see http://www.cowtowncoder.com/blog/archives/2011/09/entry_461.html).
And for more advanced cases, BeanSerializerModifier
does give you access to actual list of BeanPropertyWriter
s, which represent individual properties POJO has. From that, you could write a wrapper that enables/disables output dynamically.
Or perhaps you can even combine approaches: modifier to get list of possible property names; then FilterProvider
to dynamically add filter. Benefit of this would be that it is a very efficient way of implementing filtering.
Perhaps you could use Jackson's JSON Schema module to generate a schema for a class, then inspect the schema.