我们如何使用Jackson将Swing的DefaultMutableTreeNode
序列化到JSON或从JSON反序列化?
有一个相关的问题 How to serialize DefaultMutableTreeNode (Java) to JSON?。 但是它要求的是Gson,而不是Jackson(仅 用于序列化,而不用于反序列化。
对于DefaultMutableTreeNode
杰克逊的默认设置
出于各种原因,序列化/序列化无法正常工作
DefaultMutableTreeNode
对象。
但是它没有规范的getter和setter方法
(例如getChildren()
和setChildren(...)
)。getParent()
,getRoot()
,
getPath()
)将导致无限递归和StackOverflow
在序列化期间。isLeaf()
,
getNextSibling()
,getLastChild()
,...)不需要
要序列化,因为它们是从其他属性派生的。答案 0 :(得分:0)
您可以使用以下命令自定义杰克逊的ObjectMapper
JsonSerializer
和JsonDeserializer
特别是
专为将DefaultMutableTreeNode
转换为JSON而设计
反之亦然。
ObjectMapper objectMapper = new ObjectMapper()
.registerModule(new SimpleModule()
.addSerializer(DefaultMutableTreeNode.class, new DefaultMutableTreeNodeSerializer())
.addDeserializer(DefaultMutableTreeNode.class, new DefaultMutableTreeNodeDeserializer()))
.enable(SerializationFeature.INDENT_OUTPUT);
下面的DefaultMutableTreeNodeSerializer
是负责任的
用于将DefaultMutableTreeNode
转换为JSON。
它写入allowsChildren
,userObject
和children
DefaultMutableTreeNode
转换为JSON。
它不会写其parent
,因为那样会
导致无限递归和StackOverflowError。
而是将父子关系编码为嵌套
JSON输出的结构。
public class DefaultMutableTreeNodeSerializer extends JsonSerializer<DefaultMutableTreeNode> {
@Override
public void serialize(DefaultMutableTreeNode node, JsonGenerator gen, SerializerProvider serializers)
throws IOException {
gen.writeStartObject();
gen.writeBooleanField("allowsChildren", node.getAllowsChildren());
gen.writeObjectField("userObject", node.getUserObject());
if (node.getChildCount() > 0)
gen.writeObjectField("children", Collections.list(node.children()));
// Don't write node.getParent(), it would lead to infinite recursion.
gen.writeEndObject();
}
}
对于测试,您可以序列化示例JTree
的根节点,
然后再次反序列化。
JTree tree = new JTree(); // a sample tree
Object root = tree.getModel().getRoot(); // a DefaultMutableTreeNode
String json = objectMapper.writeValueAsString(root);
System.out.println(json);
DefaultMutableTreeNode root2 = objectMapper.readValue(json, DefaultMutableTreeNode.class);
它将生成以下JSON输出:
{
"allowsChildren" : true,
"userObject" : "JTree",
"children" : [ {
"allowsChildren" : true,
"userObject" : "colors",
"children" : [ {
"allowsChildren" : true,
"userObject" : "blue"
}, {
"allowsChildren" : true,
"userObject" : "violet"
}, {
"allowsChildren" : true,
"userObject" : "red"
}, {
"allowsChildren" : true,
"userObject" : "yellow"
} ]
}, {
"allowsChildren" : true,
"userObject" : "sports",
"children" : [ {
"allowsChildren" : true,
"userObject" : "basketball"
}, {
"allowsChildren" : true,
"userObject" : "soccer"
}, {
"allowsChildren" : true,
"userObject" : "football"
}, {
"allowsChildren" : true,
"userObject" : "hockey"
} ]
}, {
"allowsChildren" : true,
"userObject" : "food",
"children" : [ {
"allowsChildren" : true,
"userObject" : "hot dogs"
}, {
"allowsChildren" : true,
"userObject" : "pizza"
}, {
"allowsChildren" : true,
"userObject" : "ravioli"
}, {
"allowsChildren" : true,
"userObject" : "bananas"
} ]
} ]
}
下面的DefaultMutableTreeNodeDeserializer
是
负责将JSON转换回DefaultMutableTreeNode
。
DefaultMutableTreeNode
非常不像POJO
因此无法与Jackson一起使用。
因此,我创建了行为良好的POJO
助手类
(具有属性allowsChildren
,userObject
和children
)
并让Jackson将JSON内容反序列化到此类中。
然后,我转换POJO
对象(及其POJO
子对象)到DefaultMutableTreeNode
对象
(有DefaultMutableTreeNode
个孩子)。
public class DefaultMutableTreeNodeDeserializer extends JsonDeserializer<DefaultMutableTreeNode> {
@Override
public DefaultMutableTreeNode deserialize(JsonParser parser, DeserializationContext context)
throws IOException {
return parser.readValueAs(POJO.class).toDefaultMutableTreeNode();
}
private static class POJO {
private boolean allowsChildren;
private Object userObject;
private List<POJO> children;
// no need for: POJO parent
public DefaultMutableTreeNode toDefaultMutableTreeNode() {
DefaultMutableTreeNode node = new DefaultMutableTreeNode();
node.setAllowsChildren(allowsChildren);
node.setUserObject(userObject);
if (children != null) {
for (POJO child : children) {
node.add(child.toDefaultMutableTreeNode()); // recursion
// this did also set the parent of the child-node
}
}
return node;
}
// Following setters needed by Jackson's deserialization:
public void setAllowsChildren(boolean allowsChildren) {
this.allowsChildren = allowsChildren;
}
public void setUserObject(Object userObject) {
this.userObject = userObject;
}
public void setChildren(List<POJO> children) {
this.children = children;
}
}
}