fastjson SimplePropertyPreFilter改造
fastjson的序列化过滤器不支持a.b.c的格式数据,这边简单的改造一下。
针对版本1.2.60
public class SimplePropertyPreFilter implements PropertyPreFilter {
private final Class<?> clazz;
private final Set<String> includes;
private final Set<String> excludes;
private int maxLevel;
public SimplePropertyPreFilter(String... properties) {
this((Class)null, properties);
}
public SimplePropertyPreFilter(Class<?> clazz, String... properties) {
this.includes = new HashSet();
this.excludes = new HashSet();
this.maxLevel = 0;
this.clazz = clazz;
String[] var3 = properties;
int var4 = properties.length;
for(int var5 = 0; var5 < var4; ++var5) {
String item = var3[var5];
if (item != null) {
this.includes.add(item);
}
}
}
public int getMaxLevel() {
return this.maxLevel;
}
public void setMaxLevel(int maxLevel) {
this.maxLevel = maxLevel;
}
public Class<?> getClazz() {
return this.clazz;
}
public Set<String> getIncludes() {
return this.includes;
}
public Set<String> getExcludes() {
return this.excludes;
}
public boolean apply(JSONSerializer serializer, Object source, String name) {
if (source == null) {
return true;
}
if (this.clazz != null && !this.clazz.isInstance(source)) {
return true;
}
//修改name,变成...的样式
String levelName = serializer.getContext().toString();
levelName += "." + name;
levelName = levelName.replace("$.", "");
name = levelName;
if (this.excludes.contains(name)) {
return false;
}
if (this.maxLevel > 0) {
int level = 0;
for(SerialContext context = serializer.getContext(); context != null; context = context.parent) {
++level;
if (level > this.maxLevel) {
return false;
}
}
}
return this.includes.size() == 0 || this.includes.contains(name);
}
}