杰克逊系列化/树形结构的反序列化
问题描述:
我有以下POJO类:杰克逊系列化/树形结构的反序列化
@XmlAccessorType(XmlAccessType.FIELD)
@XmlType(name = "Folder", propOrder = {
"id",
"parent"
})
@XmlSeeAlso({
FolderTree.class
})
public class Folder
{
protected int id;
protected int parent;
getters, setters...
}
和
@XmlAccessorType(XmlAccessType.FIELD)
@XmlType(name = "FolderTree", propOrder = {
"subFolders"
})
@XmlRootElement(name = "folderTree")
public class FolderTree extends Folder
{
@XmlElement(name = "subFolder", type = Folder.class)
protected List<Folder> subFolders;
getter...
}
和包装:
@XmlAccessorType(XmlAccessType.FIELD)
@XmlType(name = "Folders", propOrder = {
"folders"
})
@XmlRootElement(name = "folders")
public class Folders
{
@XmlElement(name = "folder", type = Folder.class)
protected List<Folder> folders;
getter...
}
我需要序列化和反序列化他们在XML和JSON中。 XML工作正常,但在JSON以下结构串行化:
{
"folder": [
{
"subFolder": [
{
"subFolder": [],
"id": 2,
"parent": 1
},
{
"subFolder": [],
"id": 3,
"parent": 1
}
],
"id": 1,
"parent": null
}
]
}
解串器不能识别该子文件夹对象(“子文件夹”:[])内的第二场的子文件夹。有没有办法让它只在父文件夹中包含子文件夹字段?或者可以做些什么来使杰克逊反序列化这个对象?
我无法更改POJO类,因为它们是由jaxb创建的,并且所有工作都可以正确使用XML。
答
解决方案。
为了在没有孩子的情况下摆脱空的subFiolder数组,我禁用了序列化功能中的WRITE_EMPTY_JSON_ARRAYS。我使用Spring和bean看起来是这样的:
<bean id="objectMapper" class="org.springframework.http.converter.json.Jackson2ObjectMapperFactoryBean">
<property name="annotationIntrospector" ref="jaxbAnnotationIntrospector" />
<property name="featuresToDisable">
<list>
<value type="com.fasterxml.jackson.databind.SerializationFeature">WRITE_EMPTY_JSON_ARRAYS</value>
</list>
</property>
</bean>
反序列化,因为我无法修改POJO类,我添加了一个混入了文件夹类:
@JsonTypeInfo(
use = JsonTypeInfo.Id.NAME,
include = JsonTypeInfo.As.PROPERTY,
property = "folder",
defaultImpl = FolderTree.class)
public class FolderMixin {
}
最后,我伸出ObjectMapper设置MixInAnnotations:
public class CustomObjectMapper extends ObjectMapper
{
public CustomObjectMapper()
{
super();
customize();
}
protected void customize()
{
registerModule(new SimpleModule() {
private static final long serialVersionUID = 1L;
public void setupModule(SetupContext context) {
super.setupModule(context);
context.setMixInAnnotations(Folder.class, FolderMixin.class);
}
});
}
}
我增加了对反序列化都AnnotationIntrospectors:
<bean id="customObjectMapper" class="path.to.my.CustomObjectMapper">
<property name="annotationIntrospector" ref="introspectors" />
</bean>
<!-- introspectors -->
<bean id="xmlAnnotation" class="com.fasterxml.jackson.module.jaxb.JaxbAnnotationIntrospector">
<constructor-arg value="#{customObjectMapper.getTypeFactory()}" />
</bean>
<bean id="jsonAnnotation" class="com.fasterxml.jackson.databind.introspect.JacksonAnnotationIntrospector" />
<bean id="introspectors" class="com.fasterxml.jackson.databind.introspect.AnnotationIntrospectorPair">
<constructor-arg ref="xmlAnnotation" />
<constructor-arg ref="jsonAnnotation" />
</bean>
<!-- end of introspectors -->
<bean id="jsonConverter" class="org.springframework.http.converter.json.MappingJackson2HttpMessageConverter">
<property name="objectMapper" ref="customObjectMapper"/>
</bean>
我使用杰克逊2.4.0。