SimplePropertyPreFilter是fastjson下的过滤器
可以使用SimplePropertyPreFilter进行JSON的属性过滤 或者特定字段提取
SimplePropertyPreFilter的使用
public static void main(String[] args) {
TestBean bean = new TestBean("111", "222", "333", 1);
//使用List<String>也可以
ImmutableSet<String> build1 = ImmutableSet.<String>builder()
.add("key1")
.add("string1")
.add("integer1")
.build();
SimplePropertyPreFilter simpFilter = new SimplePropertyPreFilter();
//.getIncludes()--包括指定的字段
simpFilter.getIncludes().addAll(build1);
//.getExcludes()--不包括指定的字段
//simpFilter.getExcludes().addAll(build1);
String jsonStr = JSON.toJSONString(bean, simpFilter, SerializerFeature.WriteMapNullValue);
System.out.println(jsonStr); //{"integer1":1,"string1":"111"}
}
@Data
public static class TestBean {
private String string1;
private String string2;
private String string3;
private Integer integer1;
public TestBean(String string1, String string2, String string3, Integer integer1) {
this.string1 = string1;
this.string2 = string2;
this.string3 = string3;
this.integer1 = integer1;
}
}