如何将 DefaultMutableTreeNode (Java) 序列化为 JSON?

How to serialize DefaultMutableTreeNode (Java) to JSON?

如何将树(在 Java 中使用 DefaultMutableTreeNode class 实现)序列化为 JSON(通过 RESTful 方法传输到iOS 客户端)?

我试过了:

String jsonString = (new Gson()).toJson(topNode);
// topNode is DefaultMutableTreeNode at the root

它因 WhosebugError 而崩溃。

Swing的DefaultMutableTreeNodeclass是一个tree-like数据结构 其中包含与 childrenparent 相同类型的实例。 这就是为什么Gson的默认序列化器运行变成了无限递归 因此扔了 WhosebugError.

要解决此问题,您需要使用更智能的 JsonSerializer 自定义 Gson 专为将 DefaultMutableTreeNode 转换为 JSON 而设计。 作为奖励,您可能还想提供 JsonDeserializer 将 JSON 转换回 DefaultMutableTreeNode.

为此,不仅要通过 new Gson() 创建您的 Gson 实例,还要通过

Gson gson = new GsonBuilder()
        .registerTypeAdapter(DefaultMutableTreeNode.class, new DefaultMutableTreeNodeSerializer())
        .registerTypeAdapter(DefaultMutableTreeNode.class, new DefaultMutableTreeNodeDeserializer())
        .setPrettyPrinting()
        .create();

下面的DefaultMutableTreeNodeSerializer负责 用于将 DefaultMutableTreeNode 转换为 JSON。 它将其属性 allowsChildrenuserObjectchildren 转换为 JSON。 请注意,它不会将 parent 属性 转换为 JSON, 因为这样做会再次产生无限递归。

public class DefaultMutableTreeNodeSerializer implements JsonSerializer<DefaultMutableTreeNode> {

    @Override
    public JsonElement serialize(DefaultMutableTreeNode src, Type typeOfSrc, JsonSerializationContext context) {
        JsonObject jsonObject = new JsonObject();
        jsonObject.addProperty("allowsChildren", src.getAllowsChildren());
        jsonObject.add("userObject", context.serialize(src.getUserObject()));
        if (src.getChildCount() > 0) {
            jsonObject.add("children", context.serialize(Collections.list(src.children())));
        }
        return jsonObject;
    }
}

为了测试,我们将样本 JTree 的根节点序列化为 JSON, 然后再次反序列化。

JTree tree = new JTree();  // create a sample tree
Object topNode = tree.getModel().getRoot();  // a DefaultMutableTreeNode
String jsonString = gson.toJson(topNode);
System.out.println(jsonString);
DefaultMutableTreeNode topNode2 = gson.fromJson(jsonString, 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-like,因此不是 与 Gson 一起工作。 因此它使用 well-behaving POJO 助手 class (具有属性 allowsChildrenuserObjectchildren) 并让 Gson 将 JSON 内容反序列化为 class。 然后 POJO object(及其 POJO children)被转换为 DefaultMutableTreeNode object(含 DefaultMutableTreeNode children)。

public class DefaultMutableTreeNodeDeserializer implements JsonDeserializer<DefaultMutableTreeNode> {

    @Override
    public DefaultMutableTreeNode deserialize(JsonElement json, Type type, JsonDeserializationContext context) {
        return context.<POJO>deserialize(json, 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 Gson'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;
        }
    }
}

这是对我的旧答案的改进替代方案,该答案使用 JsonSerializer and JsonDeserializer 实现 DefaultMutableTreeNode。 这两个接口的 API 文档说:

New applications should prefer TypeAdapter, whose streaming API is more efficient than this interface's tree API.

因此,让我们使用这个首选方法并实现一个 TypeAdapter 对于 DefaultMutableTreeNode.

要使用它,您可以像这样创建 Gson 实例 (而不是仅仅使用 new Gson()):

Gson gson = new GsonBuilder()
        .registerTypeAdapterFactory(DefaultMutableTreeNodeTypeAdapter.FACTORY)
        .setPrettyPrinting()
        .create();

下面的DefaultMutableTreeNodeTypeAdapter负责 将 DefaultMutableTreeNode 与 JSON 相互转换。 它 writes/reads 其属性 allowsChildrenuserObjectchildren。 没有必要写parent属性,因为父子 关系已经编码在 JSON 输出的嵌套结构中。

public class DefaultMutableTreeNodeTypeAdapter extends TypeAdapter<DefaultMutableTreeNode> {

    public static final TypeAdapterFactory FACTORY = new TypeAdapterFactory() {

        @Override
        @SuppressWarnings("unchecked") // we use a runtime check to make sure the 'T's equal
        public <T> TypeAdapter<T> create(Gson gson, TypeToken<T> type) {
            if (type.getRawType() == DefaultMutableTreeNode.class) {
                return (TypeAdapter<T>) new DefaultMutableTreeNodeTypeAdapter(gson);
            }
            return null;
        }
    };

    private final Gson gson;

    private DefaultMutableTreeNodeTypeAdapter(Gson gson) {
        this.gson = gson;
    }

    @Override
    public void write(JsonWriter out, DefaultMutableTreeNode node) throws IOException {
        out.beginObject();
        out.name("allowsChildren");
        out.value(node.getAllowsChildren());
        out.name("userObject");
        gson.toJson(node.getUserObject(), Object.class, out);
        if (node.getChildCount() > 0) {
            out.name("children");
            gson.toJson(Collections.list(node.children()), List.class, out); // recursion!
        }
        // No need to write node.getParent(), it would lead to infinite recursion.
        out.endObject();
    }

    @Override
    public DefaultMutableTreeNode read(JsonReader in) throws IOException {
        in.beginObject();
        DefaultMutableTreeNode node = new DefaultMutableTreeNode();
        while (in.hasNext()) {
            switch (in.nextName()) {
            case "allowsChildren":
                node.setAllowsChildren(in.nextBoolean());
                break;
            case "userObject":
                node.setUserObject(gson.fromJson(in, Object.class));
                break;
            case "children":
                in.beginArray();
                while (in.hasNext()) {
                    node.add(read(in)); // recursion!
                    // this did also set the parent of the child-node
                }
                in.endArray();
                break;
            default:
                in.skipValue();
                break;
            }
        }
        in.endObject();
        return node;
    }
}