使用Jackson序列化嵌套类

时间:2018-04-24 06:29:02

标签: 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 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大小为35 MB,耗时超过20分钟。

0 个答案:

没有答案