fastjson 相互引用类示例进行parseObject转换时对象引用缺失

0qx6xfy6  于 2021-11-27  发布在  Java
关注(0)|答案(0)|浏览(227)

建了3个互相引用的类:Father、Mother、Son
public class Father {
private String name;
private Son son;
private Mother wife;

public static Father init() {
    Father father = new Father();
    father.name = "This is father";
    return father;
}

public String getName() {
    return name;
}

public void setName(String name) {
    this.name = name;
}

public Son getSon() {
    return son;
}

public void setSon(Son son) {
    this.son = son;
}

public Mother getWife() {
    return wife;
}

public void setWife(Mother wife) {
    this.wife = wife;
}

}

public class Mother {
private String name;
private Son son;
private Father husband;

public static Mother init() {
    Mother mother = new Mother();
    mother.name = "This is mother";
    return mother;
}

public String getName() {
    return name;
}

public void setName(String name) {
    this.name = name;
}

public Son getSon() {
    return son;
}

public void setSon(Son son) {
    this.son = son;
}

public Father getHusband() {
    return husband;
}

public void setHusband(Father husband) {
    this.husband = husband;
}

}

public class Son {
private String name;
private Mother mother;
private Father father;

public static Son init() {
    Son son = new Son();
    son.name = "This is son";
    return son;
}

public String getName() {
    return name;
}

public void setName(String name) {
    this.name = name;
}

public Mother getMother() {
    return mother;
}

public void setMother(Mother mother) {
    this.mother = mother;
}

public Father getFather() {
    return father;
}

public void setFather(Father father) {
    this.father = father;
}

}

进行如下的测试:
Father father = Father.init();
Mother mother = Mother.init();
Son son = Son.init();
father.setWife(mother);
father.setSon(son);
mother.setHusband(father);
mother.setSon(son);
son.setFather(father);
son.setMother(mother);
String jsonStringFather = JSON.toJSONString(father);
JSONObject objectFather = JSON.parseObject(jsonStringFather);

objectFather对象内容如下:

向确认下,这里是设计如此还是缺陷。是能够正常转回对象的。

环境:1.2.62,jdk6

暂无答案!

目前还没有任何答案,快来回答吧!

相关问题