fastjson过滤属性,重点在于PropertyFilter 这个东西(应用场景,hibernate懒加载过滤不要的属性)
2016-05-28 15:10
579 查看
更多内容,可以参考这个文章:
http://www.360doc.com/content/12/0319/15/1542811_195663419.shtml
[java] view
plain copy
//实现PropertyFilter中的apply方法
[java] view
plain copy
public class ComplexPropertyPreFilter implements PropertyFilter {
private Map<Class<?>, Set<String>> includeMap = new HashMap<Class<?>, Set<String>>();
//@Override
public boolean apply(Object source, String name, Object value) {
for(Entry<Class<?>, Set<String>> entry : includeMap.entrySet()) {
Class<?> class1 = entry.getKey();
if(source.getClass() == class1){
Set<String> fields = entry.getValue();
for(String field : fields) {
if(field.equals(name)){
return false;
}
}
}
}
return true;
}
public ComplexPropertyPreFilter(Map<Class<?>, Set<String>> includeMap){
this.includeMap = includeMap;
}
}
测试类:
[java] view
plain copy
class Wheel{
String name;
int size;
String color;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getSize() {
return size;
}
public void setSize(int size) {
this.size = size;
}
public String getColor() {
return color;
}
public void setColor(String color) {
this.color = color;
}
public Wheel() {
super();
this.color="black";
this.name="miqilin";
this.size = 17;
}
}
class Sofa{
String color;
int count;
String texture;
public String getColor() {
return color;
}
public void setColor(String color) {
this.color = color;
}
public int getCount() {
return count;
}
public void setCount(int count) {
this.count = count;
}
public String getTexture() {
return texture;
}
public void setTexture(String texture) {
this.texture = texture;
}
public Sofa() {
super();
this.color = "white";
this.count = 4;
this.texture = "fur";
}
}
class Car {
private Wheel wheel;
private Sofa sofa;
public Wheel getWheel() {
return wheel;
}
public void setWheel(Wheel wheel) {
this.wheel = wheel;
}
public Sofa getSofa() {
return sofa;
}
public void setSofa(Sofa sofa) {
this.sofa = sofa;
}
public Car() {
super();
this.wheel = new Wheel();
this.sofa = new Sofa();
}
}
public class Test {
public static void main(String args[]){
Map<String,Car> map = new HashMap<String, Car>();
map.put("car1", new Car());
map.put("car2", new Car());
//需要过滤的类 + 属性
Map<Class<?>, Set<String>> includeMap = new HashMap<Class<?>, Set<String>>();
Set<String> set = new HashSet<String>();
set.add("color");
includeMap.put(Wheel.class, set);
set = new HashSet<String>();
set.add("texture");
includeMap.put(Sofa.class, set);
ComplexPropertyPreFilter cfilter = new ComplexPropertyPreFilter(includeMap);
SerializeWriter sw = new SerializeWriter();
JSONSerializer serializer = new JSONSerializer(sw);
serializer.getPropertyFilters().add(cfilter);
serializer.write(map);
System.out.println(sw.toString());
}
}
http://www.360doc.com/content/12/0319/15/1542811_195663419.shtml
[java] view
plain copy
//实现PropertyFilter中的apply方法
[java] view
plain copy
public class ComplexPropertyPreFilter implements PropertyFilter {
private Map<Class<?>, Set<String>> includeMap = new HashMap<Class<?>, Set<String>>();
//@Override
public boolean apply(Object source, String name, Object value) {
for(Entry<Class<?>, Set<String>> entry : includeMap.entrySet()) {
Class<?> class1 = entry.getKey();
if(source.getClass() == class1){
Set<String> fields = entry.getValue();
for(String field : fields) {
if(field.equals(name)){
return false;
}
}
}
}
return true;
}
public ComplexPropertyPreFilter(Map<Class<?>, Set<String>> includeMap){
this.includeMap = includeMap;
}
}
测试类:
[java] view
plain copy
class Wheel{
String name;
int size;
String color;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getSize() {
return size;
}
public void setSize(int size) {
this.size = size;
}
public String getColor() {
return color;
}
public void setColor(String color) {
this.color = color;
}
public Wheel() {
super();
this.color="black";
this.name="miqilin";
this.size = 17;
}
}
class Sofa{
String color;
int count;
String texture;
public String getColor() {
return color;
}
public void setColor(String color) {
this.color = color;
}
public int getCount() {
return count;
}
public void setCount(int count) {
this.count = count;
}
public String getTexture() {
return texture;
}
public void setTexture(String texture) {
this.texture = texture;
}
public Sofa() {
super();
this.color = "white";
this.count = 4;
this.texture = "fur";
}
}
class Car {
private Wheel wheel;
private Sofa sofa;
public Wheel getWheel() {
return wheel;
}
public void setWheel(Wheel wheel) {
this.wheel = wheel;
}
public Sofa getSofa() {
return sofa;
}
public void setSofa(Sofa sofa) {
this.sofa = sofa;
}
public Car() {
super();
this.wheel = new Wheel();
this.sofa = new Sofa();
}
}
public class Test {
public static void main(String args[]){
Map<String,Car> map = new HashMap<String, Car>();
map.put("car1", new Car());
map.put("car2", new Car());
//需要过滤的类 + 属性
Map<Class<?>, Set<String>> includeMap = new HashMap<Class<?>, Set<String>>();
Set<String> set = new HashSet<String>();
set.add("color");
includeMap.put(Wheel.class, set);
set = new HashSet<String>();
set.add("texture");
includeMap.put(Sofa.class, set);
ComplexPropertyPreFilter cfilter = new ComplexPropertyPreFilter(includeMap);
SerializeWriter sw = new SerializeWriter();
JSONSerializer serializer = new JSONSerializer(sw);
serializer.getPropertyFilters().add(cfilter);
serializer.write(map);
System.out.println(sw.toString());
}
}
相关文章推荐
- JavaScript 变量声明提升
- javascript高级函数
- 借助 SublimeLinter 编写高质量的 JavaScript & CSS 代码
- JavaScript初学者难以知道的事(初级篇三)
- js常用操作
- JavaScript 函数中的值传递
- 理清JavaScript正则表达式--下篇
- JavaScript 字符串不变性
- 《Web开发过滤Javascript、HTML的方法》
- Js作用域与作用域链详解
- 用JS写Ajax的请求函数(1)
- 不同js异步函数同步的实现方法
- js获取键盘键值
- JSP---web.xml中设置前后台不同的头部和底部
- 4类 JavaScript 内存泄漏及如何避免
- javaScript取得当前元素的下一个元素
- js 给json添加新的字段,或者添加一组数据,在JS数组指定位置删除、插入、替换元素
- JSON基础学习
- js代码错误监控代码
- jstat命令