1 回答
TA贡献1798条经验 获得超3个赞
您可以ObjectMapper使用 a 自定义 Jackson'sJsonSerializer并JsonDeserializer专门用于将 a 转换DefaultMutableTreeNode为 JSON,反之亦然。
ObjectMapper objectMapper = new ObjectMapper()
.registerModule(new SimpleModule()
.addSerializer(DefaultMutableTreeNode.class, new DefaultMutableTreeNodeSerializer())
.addDeserializer(DefaultMutableTreeNode.class, new DefaultMutableTreeNodeDeserializer()))
.enable(SerializationFeature.INDENT_OUTPUT);
下面DefaultMutableTreeNodeSerializer负责将 a 转换DefaultMutableTreeNode为 JSON。它将allowsChildren,userObject和children of写入DefaultMutableTreeNodeJSON。它不写它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();
}
}
为了测试,您可以序列化 sample 的根节点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,因此不能与杰克逊一起很好地工作。因此,我创建了一个表现良好的POJO帮助器类(具有属性 和) allowsChildren,并让 Jackson 将 JSON 内容反序列化到该类中。然后我将对象(及其 子对象)转换为对象(带有子对象)。userObjectchildrenPOJOPOJODefaultMutableTreeNodeDefaultMutableTreeNode
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;
}
}
}
添加回答
举报