Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

2.0.5版本,双向链表向Json序列化可用,但是序列化产生的数据反序列化节点存在问题 #378

Closed
javalipf opened this issue May 29, 2022 · 2 comments
Labels
bug Something isn't working
Milestone

Comments

@javalipf
Copy link

javalipf commented May 29, 2022

@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<ApproveConfigJson> 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);方法中

@javalipf javalipf added the bug Something isn't working label May 29, 2022
@wenshao wenshao added this to the 2.0.6 milestone May 29, 2022
@wenshao
Copy link
Member

wenshao commented May 29, 2022

https://oss.sonatype.org/content/repositories/snapshots/com/alibaba/fastjson2/fastjson2/2.0.6-SNAPSHOT/

感谢反馈了JSON格式支持引用的BUG,问题已修复,请用2.0.6-SNAPSHOT版本验证。2.0.6正式版本预计6月5日前发布。

@wenshao
Copy link
Member

wenshao commented Jun 4, 2022

https://github.com/alibaba/fastjson2/releases/tag/2.0.6
问题已经修复,请用新版本

@wenshao wenshao closed this as completed Jun 4, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

2 participants