如何使用Jackson序列化/反序列化DefaultMutableTreeNode?

时间:2019-03-06 20:39:39

标签: java json recursion serialization jackson

我们如何使用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(),...)不需要 要序列化,因为它们是从其他属性派生的。

1 个答案:

答案 0 :(得分:0)

您可以使用以下命令自定义杰克逊的ObjectMapper JsonSerializerJsonDeserializer特别是 专为将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。 它写入allowsChildrenuserObjectchildren 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的根节点, 然后再次反序列化。

tree

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助手类 (具有属性allowsChildrenuserObjectchildren) 并让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;
        }
    }
}