如何获取Jackson看到的类的属性列表? [英] How to get the list of properties of a class as Jackson views it?
问题描述
我正在编写需要访问Jackson配置定义的类属性列表的代码。
例如,对于以下类:
@JsonIgnoreProperties(value = {intValue})
公共类MyDto {
@JsonProperty(name)
private String stringValue;
private int intValue;
private long longValue;
@JsonIgnore
private boolean booleanValue;
//标准制定者和获取者未显示
}
我会得到 [name,longValue]
因为这是杰克逊在序列化时实际考虑的属性。
我不知道我认为编写一整段代码来寻找getter / setter并检查Jackson注释是要走的路,因为这将重新实现Jackson。
如果我能够获得用于序列化的Jackson ObjectMapper
的句柄,有没有办法获得 Class<的属性列表。 ?>
或输入
对象?(尊重杰克逊注释和配置)
我挖了一下杰克逊的实现,找到了 POJOPropertiesCollector
,但我不确定如何从杰克逊以外的地方使用它(我们不认为要做到这一点,我相信)。
作为最后的手段,我可以创建一个我鼓励的类的实例使用ObjectMapper执行,序列化它,然后解析JSON以查找属性名称,但我不认为这也是干净的(它会带来它的整个问题:nulls可能没有序列化,在construtor中会发生什么等。)。
任何想法?
与Jackson,您可以内省任意类以获取可用的JSON属性:
< pre class =lang-java prettyprint-override>
//为您的类构造Jackson JavaType
JavaType javaType = mapper.getTypeFactory()。constructType(MyDto.class);
//内省给定类型
BeanDescription beanDescription = mapper.getSerializationConfig()。introspect(javaType);
//查找属性
列表< BeanPropertyDefinition> properties = beanDescription.findProperties();
BeanPropertyDefinition
列表应该为您提供有关的详细信息JSON属性。
@JsonIgnoreProperties
不考虑类级别注释上面提到的方法。但您可以使用 AnnotationIntrospector
在类级别上忽略属性:
//获取类级忽略的属性
Set< String> ignoredProperties = mapper.getSerializationConfig()。getAnnotationIntrospector()
.findPropertyIgnorals(beanDescription.getClassInfo())。getIgnored();
然后过滤属性
删除属性存在于 ignoredProperties
:
//删除类级别的过滤属性
List< BeanPropertyDefinition> availableProperties = properties.stream()
.filter(property - >!ignoredProperties.contains(property.getName()))
.collect(Collectors.toList());
即使你为你的班级定义了混音,这种方法仍然有效。
AnnotationIntrospector#findPropertyIgnorals(Annotated)
方法在Jackson 2.8中引入。 AnnotationIntrospector#findPropertiesToIgnore(Annotated,boolean)
方法可用于旧版本(但自Jackson 2.8以来已弃用) 。
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?
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).
这篇关于如何获取Jackson看到的类的属性列表?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!