Skip to content
This repository has been archived by the owner on Oct 23, 2024. It is now read-only.

两个对象引用同一个对象,序列化并反序列化后第二个对象的该引用为null #415

Closed
xuehuazhang opened this issue Sep 8, 2015 · 1 comment
Milestone

Comments

@xuehuazhang
Copy link

    //一个老师,有两个学生,两个学生住在同一个地方
    //对老师序列化,老师关联的第二个学生的地址反序列化的时候为null

    Teacher t = new Teacher();

    Address addr = new Address();
    addr.setAddrDetail("中国上海南京路");

    Student s1 = new Student();
    s1.setName("张三");
    s1.setAddr(addr);

    Student s2 = new Student();
    s2.setName("李四");
    s2.setAddr(addr);

    t.setStudentList(Arrays.asList(s1, s2));


    MessageData msg = new MessageData();
    msg.setData(t);

    //序列化
    String json = JSON.toJSONString(msg,SerializerFeature.WriteClassName);
     //@1 打印序列化的时候json串

    System.out.println(json);
    //反序列化
    MessageData data = JSON.parseObject(json,MessageData.class);
    Teacher  dt = (Teacher) data.getData();
    for(Student s:dt.getStudentList()){
        //@2 打印两个学生的地址,现象第一个地址不为空,其他都为空
        System.out.println(s.getAddr());
    }

输出内容
{"@type":"demo.MessageData","data":{"@type":"demo.Teacher","studentList":[{"addr":{"addrDetail":"中国上海南京路"},"name":"张三"},{"addr":{"$ref":"$.data.studentList[0].addr"},"name":"李四"}]}}

demo.Address@49fc609f

null

wenshao added a commit that referenced this issue Apr 15, 2016
wenshao added a commit that referenced this issue Apr 15, 2016
@wenshao
Copy link
Member

wenshao commented Apr 15, 2016

bug fixed. it will include in 1.2.9 & 1.1.49.android.

@wenshao wenshao closed this as completed Apr 15, 2016
@wenshao wenshao added this to the 1.2.9 milestone Apr 15, 2016
wenshao added a commit that referenced this issue Jul 17, 2019
asa1997 pushed a commit to Be-Secure/fastjson that referenced this issue Feb 8, 2023
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants