简体   繁体   中英

@JsonInclude filtering by class' attribute

I am working on serializing classes to XML. I've found @JsonInclude to be immensely helpful. However, I am having issues on filtering a class by its attribute. In the xml, if it's a US address I need to have one set of annotations for the fields using @JsonProperty . Because of this, I'm trying to use the Include annotation to only show the relevant field based on the country.

The US address needs to have USAddress as the wrapper element name. Foreign Addresses need to have ForeignAddress as the wrapper element name as well as different element names for state/zip.

Is there a way to access the class and it's attributes in the filter? I've tried using the super class for both types of addresses, but with Bazel, I run into circular dependency issues when doing that. Im super new to bazel :P

public class Thing {
  @JsonInclude(value = Include.CUSTOM, valueFilter = CountryFilter.class)
  private final USAddress usAddress = new USAddress({line1: "123 MockingBird Ln", country: "US"});
  @JsonInclude(value = Include.CUSTOM, valueFilter = CountryFilter.class)
  private final ForeignAddress foreignAddress = new ForeignAddress({line1: "123 MockingBird Ln", country: "DE"});
}

public class CountryFilter {
  @Override
  public boolean equals(Object obj) {
    return ...; // This is where I'm having issues. Would like it to do something like obj.getCountry().equals("US").
  }
}

我最终使用了@JsonInclude(Include.NON_NULL),然后在构造函数中使用了一个条件来选择性地设置地址。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM