FastJsonプロパティフィルタ(hibernate関連)を書き換えます.
5725 ワード
もっと読む
そのフィルタを使う
その結果:
package cn.topcheer.landTax.filter;
import java.util.HashMap;
import java.util.Map;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.serializer.JSONSerializer;
import com.alibaba.fastjson.serializer.PropertyPreFilter;
import com.alibaba.fastjson.serializer.SerializerFeature;
/**
* @author : ²º¹³
* @Comment : fastjson ( )
*/
public class ComplexPropertyPreFilter implements PropertyPreFilter {
private Map, String[]> includes = new HashMap, String[]>();
private Map, String[]> excludes = new HashMap, String[]>();
static {
JSON.DEFAULT_GENERATE_FEATURE |= SerializerFeature.DisableCircularReferenceDetect.getMask();
}
public ComplexPropertyPreFilter() {
}
public ComplexPropertyPreFilter(Map, String[]> includes) {
super();
this.includes = includes;
}
public boolean apply(JSONSerializer serializer, Object source, String name) {
// 。
if (source == null) {
return true;
}
//
Class> clazz = source.getClass();
// 、 ,
//
for (Map.Entry, String[]> item : this.excludes.entrySet()) {
// isAssignableFrom(),
if (item.getKey().isAssignableFrom(clazz)) {
String[] strs = item.getValue();
// name
if (isHave(strs, name)) {
return false;
}
}
}
//
if (this.includes.isEmpty()) {
return true;
}
//
//
for (Map.Entry, String[]> item : this.includes.entrySet()) {
// isAssignableFrom(),
if (item.getKey().isAssignableFrom(clazz)) {
String[] strs = item.getValue();
// name
if (isHave(strs, name)) {
return true;
}
}
}
return false;
}
/*
* , ,
*/
public static boolean isHave(String[] strs, String s) {
for (int i = 0; i < strs.length; i++) {
//
if (strs[i].equals(s)) {
// ,
return true;
}
}
// false
return false;
}
public Map, String[]> getIncludes() {
return includes;
}
public void setIncludes(Map, String[]> includes) {
this.includes = includes;
}
public Map, String[]> getExcludes() {
return excludes;
}
public void setExcludes(Map, String[]> excludes) {
this.excludes = excludes;
}
}
そのフィルタを使う
Map, String[]> map = new HashMap, String[]>();
map.put(Area.class, new String[]{"id","name","area"});
ComplexPropertyPreFilter ss = new ComplexPropertyPreFilter(map);
String ddd = JSONArray.toJSONString(list,ss);
その結果:
[
{
"id": "420000",
"name": " "
},
{
"area": {
"id": "420000",
"name": " "
},
"id": "420051",
"name": " "
},
{
"area": {
"id": "420000",
"name": " "
},
"id": "420100",
"name": " "
},
{
"area": {
"area": {
"id": "420000",
"name": " "
},
"id": "420100",
"name": " "
},
"id": "420101",
"name": " "
},
{
"area": {
"area": {
"id": "420000",
"name": " "
},
"id": "420100",
"name": " "
},
"id": "420102",
"name": " "
}]