18

I'm writing code that needs to access the list of properties of a class as defined by a Jackson configuration.

For instance, for the following class:

@JsonIgnoreProperties(value = { "intValue" })
public class MyDto {

    @JsonProperty("name")
    private String stringValue;

    private int intValue;

    private long longValue;

    @JsonIgnore
    private boolean booleanValue;

    // standard setters and getters are not shown
}

I would get [name,longValue] because that's the properties that Jackson actually considers when serializing.

I don't think writing a whole piece of code to look for getters/setters and inspect Jackson annotations is the way to go, since that would be reimplementing Jackson.

If I'm able to get a handle on the Jackson ObjectMapper used for serialization, is there a way to get a list of properties of a Class<?> or Type object? (respecting Jackson annotations and config)

I dug a bit into Jackson's implementation, and found the POJOPropertiesCollector, but I'm not sure about how I can use it from outside Jackson (we're not supposed to do this I believe).

As a last resort, I could create an instance of the class I'm inspecting, serialize it with the ObjectMapper, and then parse the JSON to find property names, but I don't think this is clean either (and it would bring its whole set of probelms: nulls might not be serialized, what happens in the construtor etc.).

Any ideas?

1 Answer 1

32

With Jackson, you can introspect an arbitrary class to get the available JSON properties:

// 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();

The BeanPropertyDefinition list should give you the details you need regarding the JSON properties.


The @JsonIgnoreProperties class level annotation is not taken into account with the above mentioned approach. But you can use an AnnotationIntrospector to get the properties ignored on class level:

// Get class level ignored properties
Set<String> ignoredProperties = mapper.getSerializationConfig().getAnnotationIntrospector()
        .findPropertyIgnorals(beanDescription.getClassInfo()).getIgnored();

Then filter properties removing the properties which are present in ignoredProperties:

// Filter properties removing the class level ignored ones
List<BeanPropertyDefinition> availableProperties = properties.stream()
        .filter(property -> !ignoredProperties.contains(property.getName()))
        .collect(Collectors.toList());

This approach works even if you have mix-ins defined for your class.


The AnnotationIntrospector#findPropertyIgnorals(Annotated) method was introduced in Jackson 2.8. The AnnotationIntrospector#findPropertiesToIgnore(Annotated, boolean) method can be used for older versions (but it's deprecated since Jackson 2.8).

12
  • 1
    This looks very promising, I will give it a try
    – Joffrey
    Commented Aug 23, 2017 at 12:37
  • 1
    It almost does the job. It just fails to ignore properties that are listed in a @JsonIgnoreProperties class annotation. @JsonIgnore field annotation is correctly taken into account, though. I have a feeling that class annotations in general are ignored, but need more experimentation to confirm this.
    – Joffrey
    Commented Aug 23, 2017 at 16:08
  • 3
    Thanks for the update, this is actually close to what I had ended up doing after digging a bit more. Sorry not to have posted it earlier. For future reference, here is my full implementation: github.com/joffrey-bion/livedoc/blob/master/livedoc-springmvc/…
    – Joffrey
    Commented Aug 30, 2017 at 8:00
  • 2
    @BenBarden the premise of the question is that I already have access to an ObjectMapper so there is no need to explain how to get it
    – Joffrey
    Commented Apr 29, 2021 at 21:05
  • 3
    @BenBarden while I agree with you in the general case, I don't think this applies here. The ObjectMapper is sort of the entry point of the Jackson library. So if a person interacts with Jackson this is usually via an ObjectMapper. If not, the way to get the ObjectMapper will be framework specific (for instance in the context of Spring, it might be via injection of the relevant bean). So we can't really add anything here besides a generic comment saying to get an ObjectMapper from somewhere. The way to get it from a specific framework will be the object of a separate question and answer.
    – Joffrey
    Commented Apr 30, 2021 at 19:26

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.