Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/397.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 使用Jackson实现嵌套类的序列化_Java_Json_Serialization_Jackson_Objectmapper - Fatal编程技术网

Java 使用Jackson实现嵌套类的序列化

Java 使用Jackson实现嵌套类的序列化,java,json,serialization,jackson,objectmapper,Java,Json,Serialization,Jackson,Objectmapper,以下是我的班级结构: public interface AInf { public String getUniqueKey(); // other getters } public interface BInf { public String getUniqueKey(); // other getters } public interface CInf { public String getUniqueKey(); // other gett

以下是我的班级结构:

public interface AInf {
    public String getUniqueKey();
    // other getters
}

public interface BInf {
    public String getUniqueKey();
    // other getters
}

public interface CInf {
    public String getUniqueKey();
    // other getters
}

class A implements AInf {
    private String name;
    private String uniqueKey;
    private CInf c;
    // with getters and setters
    // also there are more fields with nested class structure
}

class B implements BInf {
    private String name;
    private String uniqueKey;
    private AInf a;
    private CInf c;
    // with getters and setters
    // also there are more fields with nested class structure
}

class C implements CInf {
    private String name;
    private String uniqueKey;
    private String value;
    // with getters and setters
}
当使用Jackson的ObjectMapper.writeValueAsString()创建JSON时,它再次为内部嵌套类创建整个JSON。比如:

A :- 
{
 "name" : "abc",
 "c" : {
 // whole C class JSON without unique Key
}
}

B :- 
{
"name" : "xyz",
"a" : {
 // whole A class JSON without unique Key
}
 "c" : {
 // whole C class JSON without unique Key
}
}

C :- 
{
"name" : "xyz",
"value" : "pqr"
}
有没有办法避免为嵌套类重新创建JSON?


据统计,我总共有600多个类型为A、B和C的对象。使用上述方法创建的JSON大小为35MB,花费了20多分钟。

请尝试@JsonIgnore让feilds完成skip@VinodBokare对尽可能使用它。是否要避免已写入实例的重复序列化,还是要忽略字段?@teppic我要避免已写入实例的重复序列化。已使用@JsonIgnoreDo实现忽略字段。您的对象是否具有唯一键?如果需要,请使用
@JsonIdentityInfo
对其进行注释。这将允许jackson折叠对象以返回引用。