忽略@JsonProperty annontation在fastxml ObjectMapper中基于Some Flag

时间:2016-05-23 06:24:41

标签: java json fasterxml

我想根据某些标记忽略 fastrxml @JsonProperty中的ObjectMapper年度

例如:

public class FeedData implements Serializable {

    /**
     * 
     */
    private static final long serialVersionUID = 1L;
    @JsonProperty(value="n")
    private String name;
    @JsonProperty(value="i")
    private Integer id;
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
    public Integer getId() {
        return id;
    }
    public void setId(Integer id) {
        this.id = id;
    }


}

我有一个基于输出的标志应该变化

如果标志是true
     比json应该是{"n":"Radha","i":1}
否则如果标志是false
   比json应该是{"name":"Radha","id":1}

1 个答案:

答案 0 :(得分:0)

您可以根据标志值配置对象映射器:

ObjectMapper mapper = new ObjectMapper();

if (!flag) {
  mapper.setAnnotationIntrospector( new JacksonAnnotationIntrospector() {
        @Override
        public PropertyName findNameForSerialization(Annotated a) {
            return null;
        }
    });
}
mapper.convertValue(object, FeedData.class)