0

大众!我对 JSON 序列化有疑问 例如,我有两个类

public class Site {
private String eID;
private Integer sID;

public Site(String eID, Integer sID){
    this.sID = sID;
    this.eID = eID;
}

public String getEID(){
    return eID;
}

public Integer getSID(){
    return sID;
} }

和包含先前类型对象的类。例如,像

public class Address{
private BigInteger addrID;
private ICOMSSitePOJO site;

public AddressUnitPOJO(BigInteger addrID, Site site){
    this.addrID = addrID;
    this.site = new Site(site.getEID(), site.getSID());
}

还有 getter 和 setter

所以对于站点类,我使用跟随序列化类

public class SiteSerializer extends JsonSerializer<Site>  {
@Override
public void serialize(Site obj, JsonGenerator jsonGenerator,
                      SerializerProvider serializerProvider) throws IOException, JsonProcessingException
{
    jsonGenerator.writeStartObject();
    jsonGenerator.writeFieldName("sID");
    jsonGenerator.writeNumber(obj.getSID());
    jsonGenerator.writeFieldName("eID");
    jsonGenerator.writeString(obj.getEID());
    jsonGenerator.writeEndObject();
}

然后我想序列化我的地址类。为此,我可以执行以下操作:

   public class AddressUnitSerializer extends JsonSerializer<AddressUnitPOJO> {

    @Override
    public void serialize(AddressUnitPOJO obj, JsonGenerator jsonGenerator,
                          SerializerProvider serializerProvider)
                                        throws IOException, JsonProcessingException {
        jsonGenerator.writeStartObject();

        jsonGenerator.writeFieldName("addressID");
        jsonGenerator.writeNumber(obj.getAddressID());

//I don't want to do this rutine every time!!!
/*      jsonGenerator.writeFieldName("Site");

        jsonGenerator.writeStartObject();
        jsonGenerator.writeFieldName("sID");
        jsonGenerator.writeNumber(obj.getSite().getSID());
        jsonGenerator.writeFieldName("eID");
        jsonGenerator.writeNumber(obj.getSite().getEID());
        jsonGenerator.writeEndObject();*/
//!!!

        jsonGenerator.writeEndObject();

    }
}

如何重用我之前对子对象的序列化?我搜索了答案,但没有找到解决方案。谢谢指教!

编辑 1

所以我现在要做的是

  public class AddressUnitSerializer extends JsonSerializer<AddressUnitPOJO> {

@Override
public void serialize(AddressUnitPOJO obj, JsonGenerator jsonGenerator,
                      SerializerProvider serializerProvider)
                                    throws IOException, JsonProcessingException {
    jsonGenerator.writeStartObject();

    jsonGenerator.writeFieldName("addressID");
    jsonGenerator.writeNumber(obj.getAddressID());

ObjectMapper mapper = new ObjectMapper().setVisibility(JsonMethod.ALL, JsonAutoDetect.Visibility.ANY);
mapper.configure(SerializationConfig.Feature.WRITE_NULL_PROPERTIES, false);
Site site = obj.getAddressUnit().getSite();

jsonGenerator.writeFieldName("Site");
jsonGenerator.writeString(mapper.writeValueAsString(site).
                            substring(0,mapper.writeValueAsString(site).length()-1));


    jsonGenerator.writeEndObject();

}
 }

但也许有更好的方法可以用注释来转换它?

4

1 回答 1

0

生成 JSON 字符串的最简单方法是使用 method .writeObject(objName),它会自动生成字段及其值。如果您需要其他名称的值或从生成或格式字符串中排除值,则可以使用标准声明。

于 2013-06-28T09:17:13.587 回答