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?