How to get a list of class properties when Jackson examines it?

I am writing code that needs to access a list of class properties, as defined by Jackson's configuration.

For example, 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 those properties that Jackson really takes into account when serializing.

I don’t think that writing a whole piece of code to search for getters / setters and checking Jackson's annotations is the way to go because it will override Jackson.

If I can get the Jackson ObjectMapper handle used for serialization, is there a way to get the list of properties of a Class<?> Or Type object? (subject to Jackson's annotations and configs)

I dug into Jackson's implementation a bit and found a POJOPropertiesCollector , but I'm not sure how I can use it from outside Jackson (we shouldn't, I suppose).

As a last resort, I could create an instance of the class that I am checking, serialize it using ObjectMapper, and then parse JSON to find the property names, but I don't think it is clean either. will give the whole set of probes: zeros cannot be serialized, which happens in the constructor, etc.).

Any ideas?

+1
source share
1 answer

With Jackson, you can inherit from 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 provide you with information about JSON properties.


@JsonIgnoreProperties class level annotation is not taken into account with the above approach. But you can use AnnotationIntrospector to ignore class level properties:

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

Then filter properties remove properties that 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 mixed settings specific to your class.


In Jackson 2.8, the AnnotationIntrospector#findPropertyIgnorals(Annotated) method was introduced. The AnnotationIntrospector#findPropertiesToIgnore(Annotated, boolean) method can be used for older versions (but it is deprecated after Jackson 2.8).

+2
source

Source: https://habr.com/ru/post/1502876/


All Articles