gpt4 book ai didi

java - 树结构的Jackson序列化/反序列化

转载 作者:行者123 更新时间:2023-11-30 11:08:53 26 4
gpt4 key购买 nike

我有以下 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
}
]
}

反序列化器无法识别 subFolder 对象(“subFolder”:[])内的第二个字段 subFolder。有没有办法让它只在父文件夹中包含子文件夹字段?或者可以做些什么来让 Jackson 按原样反序列化该对象?

我无法更改 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 类,所以我为 Folder 类添加了一个 MixIn:

@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>

我使用 Jackson 2.4.0。

关于java - 树结构的Jackson序列化/反序列化,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28354942/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com