[alibaba/fastjson]java-2.0.4版本不支持双向链表的json字符串转实体类,会有问题

2024-09-02 380 views
8
@Data
public class ApproveConfigJson implements Serializable {
    private String postId;

    private boolean hasPreNode = false;

    private ApproveConfigJson preNode;

    private boolean hasNextNode = false;

    private ApproveConfigJson nextNode;
}

json字符串 {"hasNextNode":true,"hasPreNode":false,"jump":true,"nextNode":{"hasNextNode":true,"hasPreNode":true,"jump":true,"nextNode":{"hasNextNode":false,"hasPreNode":true,"jump":true,"passType":1,"postId":"8422e7b960781225747439c620cd0919","preNode":{"$ref":".."},"sort":3},"passType":1,"postId":"1e1e159ad5f8293124208207aa4b76b1","preNode":{"$ref":".."},"sort":2},"passType":1,"postId":"b68aac95774a9682aeddb9a2d15a0c38","preNode":{"hasNextNode":false,"hasPreNode":false,"jump":false,"sort":0},"sort":1}

使用JSON.parseObject(str, ApproveConfigJson.class);方法生成的ApproveConfigJson,在链表第二级节点及之后,无法正确追踪到前一级节点,而这在1.2.78版本是可用的

1 2 78 2 0 4

回答

4

2.0.5版本,双向链表向Json序列化可用,但是序列化产生的数据反序列化节点存在问题,如下 `@Data @NoArgsConstructor @AllArgsConstructor public class ApproveConfigJson implements Serializable { private String postId;

private boolean hasPreNode = false;

private ApproveConfigJson preNode;

private boolean hasNextNode = false;

private ApproveConfigJson nextNode;

private int sort;

public ApproveConfigJson(String postId, int sort) {
    this.postId = postId;
    this.sort = sort;
}

} public static void main(String[] args) { List approveConfigJsons = new ArrayList<>(); approveConfigJsons.add(new ApproveConfigJson("1",1)); approveConfigJsons.add(new ApproveConfigJson("2",2)); approveConfigJsons.add(new ApproveConfigJson("3",3)); String s = list2LinkedJson2(approveConfigJsons); ApproveConfigJson approveConfigJson = JSON.parseObject(s, ApproveConfigJson.class); System.err.println(approveConfigJson.getNextNode().getPreNode().getPostId()); }

public static String list2LinkedJson2(List<ApproveConfigJson> approveConfigJsons) {
    approveConfigJsons = approveConfigJsons.stream().sorted(Comparator.comparing(ApproveConfigJson::getSort)).collect(Collectors.toList());
    ApproveConfigJson approveUtil = null;
    Iterator<ApproveConfigJson> iterator = approveConfigJsons.iterator();
    ApproveConfigJson local = new ApproveConfigJson();
    while (iterator.hasNext()) {
        ApproveConfigJson next = iterator.next();
        if (approveUtil == null) {
            approveUtil = next;
            local = next;
        } else {
            approveUtil.setHasNextNode(true);
            approveUtil.setNextNode(next);
            next.setHasPreNode(true);
            next.setPreNode(local);
            local = next;
            approveUtil = approveUtil.getNextNode();
            approveUtil.setNextNode(null);
        }
    }

    ApproveConfigJson finalNode = approveUtil;
    while (finalNode.isHasPreNode()) {
        finalNode = finalNode.getPreNode();
    }
    return JSON.toJSONString(finalNode);
}`

按照sort排序,链表首节点为postId = 1,序列化的json串正常,但是反序列化后链表的首节点为postId = 2,且获取下级节点后再获取上级节点会出现java.lang.NullPointerException 在 ApproveConfigJson approveConfigJson = JSON.parseObject(s, ApproveConfigJson.class);方法中