我正在尝试将带有杰克逊的 DefaultMutableTreeNode 对象序列化为 json 字符串。因此,我需要使用一个混合抽象类,它是 DefaultMutableTreeNode 类的一种代理。这可能是因为自引用字段,但我无法识别它们。
混搭班:
@JsonIgnoreProperties(ignoreUnknown = true)
public abstract class DefaultMutableTreeNodeMixIn {
@JsonCreator
public DefaultMutableTreeNodeMixIn(@JsonProperty Object userObject) {};
@JsonProperty("firstChild")
abstract TreeNode getFirstChild();
@JsonProperty("firstLeaf")
abstract DefaultMutableTreeNode getFirstLeaf();
@JsonProperty("lastChild")
abstract TreeNode getLastChild();
@JsonProperty("lastLeaf")
abstract DefaultMutableTreeNode getLastLeaf();
@JsonProperty("leafCount")
abstract int getLeafCount();
@JsonProperty("level")
abstract int getLevel();
@JsonProperty("nextLeaf")
abstract DefaultMutableTreeNode getNextLeaf();
@JsonProperty("nextNode")
abstract DefaultMutableTreeNode getNextNode();
@JsonProperty("nextSibling")
abstract DefaultMutableTreeNode getNextSibling();
@JsonProperty("parent")
abstract TreeNode getParent();
@JsonProperty("path")
abstract TreeNode[] getPath();
@JsonProperty("previousLeaf")
abstract DefaultMutableTreeNode getPreviousLeaf();
@JsonProperty("previousNode")
abstract DefaultMutableTreeNode getPreviousNode();
@JsonProperty("previousSibling")
abstract DefaultMutableTreeNode getPreviousSibling();
@JsonProperty("siblingCount")
abstract int getSiblingCount();
@JsonProperty("isLeaf")
abstract boolean isLeaf();
@JsonProperty("isRoot")
abstract boolean isRoot();
}
对象映射器:
ObjectMapper mapper = new ObjectMapper();
mapper.addMixIn(DefaultMutableTreeNode.class,DefaultMutableTreeNodeMixIn.class);
String json = mapper.writerWithDefaultPrettyPrinter().writeValueAsString(serverFileTree);
System.out.println(json);
(serverFileTree 是 DefaultMutableTreeNode 类型的对象)
桃花长相依
慕码人8056858
相关分类