分享

json lib 属性过滤

 KILLKISS 2012-04-01
  1. import java.lang.reflect.Field;   
  2. import java.util.Collection;   
  3. import java.util.Set;   
  4.   
  5. import net.sf.json.util.PropertyFilter;   
  6.   
  7. import org.apache.commons.logging.Log;   
  8. import org.apache.commons.logging.LogFactory;   
  9.     
  10. /**  
  11.  * <p>Title: 忽略属性</p>  
  12.  * <p>Description:忽略JAVABEAN的指定属性、是否忽略集合类属性</p>  
  13.  *   
  14.  */  
  15. public class IgnoreFieldProcessorImpl implements PropertyFilter {   
  16.     
  17.     Log log = LogFactory.getLog(this.getClass());   
  18.     
  19.     /**  
  20.      * 忽略的属性名称  
  21.      */  
  22.     private String[] fields;   
  23.     
  24.     /**  
  25.      * 是否忽略集合  
  26.      */  
  27.     private boolean ignoreColl = false;   
  28.     
  29.     /**  
  30.      * 空参构造方法<br/>  
  31.      * 默认不忽略集合  
  32.      */  
  33.     public IgnoreFieldProcessorImpl() {   
  34.         // empty   
  35.     }   
  36.     
  37.     /**  
  38.      * 构造方法  
  39.      * @param fields 忽略属性名称数组  
  40.      */  
  41.     public IgnoreFieldProcessorImpl(String[] fields) {   
  42.         this.fields = fields;    
  43.     }   
  44.     
  45.     /**  
  46.      * 构造方法  
  47.      * @param ignoreColl    是否忽略集合  
  48.      * @param fields    忽略属性名称数组  
  49.      */  
  50.     public IgnoreFieldProcessorImpl(boolean ignoreColl, String[] fields) {   
  51.         this.fields = fields;   
  52.         this.ignoreColl = ignoreColl;    
  53.     }   
  54.     
  55.     /**  
  56.      * 构造方法  
  57.      * @param ignoreColl 是否忽略集合  
  58.      */  
  59.     public IgnoreFieldProcessorImpl(boolean ignoreColl) {   
  60.         this.ignoreColl = ignoreColl;    
  61.     }   
  62.     
  63.     public boolean apply(Object source, String name, Object value) {   
  64.         Field declaredField = null;   
  65.         //忽略值为null的属性   
  66.         if(value == null)   
  67.             return true;   
  68.         //剔除自定义属性,获取属性声明类型   
  69.         if(!"data".equals(name) && "data"!=name && !"totalSize".equals(name) && "totalSize"!=name ){   
  70.             try {   
  71.                 declaredField = source.getClass().getDeclaredField(name);   
  72.             } catch (NoSuchFieldException e) {   
  73.                 log.equals("没有找到属性" + name);   
  74.                 e.printStackTrace();   
  75.             }   
  76.         }   
  77.         // 忽略集合   
  78.         if (declaredField != null) {   
  79.             if(ignoreColl) {   
  80.                 if(declaredField.getType() == Collection.class  
  81.                         || declaredField.getType() == Set.class) {   
  82.                     return true;   
  83.                 }   
  84.             }   
  85.         }   
  86.     
  87.         // 忽略设定的属性   
  88.         if(fields != null && fields.length > 0) {   
  89.             if(juge(fields,name)) {     
  90.                 return true;     
  91.             } else {     
  92.                 return false;     
  93.             }    
  94.         }   
  95.             
  96.         return false;   
  97.     }   
  98.     /**  
  99.      * 过滤忽略的属性  
  100.      * @param s  
  101.      * @param s2  
  102.      * @return  
  103.      */  
  104.      public boolean juge(String[] s,String s2){     
  105.          boolean b = false;     
  106.          for(String sl : s){     
  107.              if(s2.equals(sl)){     
  108.                  b=true;     
  109.              }     
  110.          }     
  111.          return b;     
  112.      }     
  113.     public String[] getFields() {   
  114.         return fields;   
  115.     }   
  116.     
  117.     /**  
  118.      * 设置忽略的属性  
  119.      * @param fields  
  120.      */  
  121.     public void setFields(String[] fields) {   
  122.         this.fields = fields;   
  123.     }   
  124.     
  125.     public boolean isIgnoreColl() {   
  126.         return ignoreColl;   
  127.     }   
  128.     
  129.     /**  
  130.      * 设置是否忽略集合类  
  131.      * @param ignoreColl  
  132.      */  
  133.     public void setIgnoreColl(boolean ignoreColl) {   
  134.         this.ignoreColl = ignoreColl;   
  135.     }   
  136. }  

Java代码 复制代码 收藏代码
  1. JsonConfig config = new JsonConfig();   
  2. config.setJsonPropertyFilter(new IgnoreFieldProcessorImpl(true)); // 忽略掉集合对象   
  3.     
  4. People people= session.get(People.class20);   
  5. JSONObject fromObject = JSONObject.fromObject(person, config );   
  6. response.getWriter().print(fromObject.toString());  

Java代码 复制代码 收藏代码
  1. JsonConfig config = new JsonConfig();   
  2. config.setJsonPropertyFilter(new IgnoreFieldProcessorImpl(truenew String[]{"name"})); // 忽略掉name属性及集合对象   
  3.     
  4. People people= session.get(People.class20);   
  5. JSONObject fromObject = JSONObject.fromObject(person, config );   
  6. response.getWriter().print(fromObject.toString());  

    本站是提供个人知识管理的网络存储空间,所有内容均由用户发布,不代表本站观点。请注意甄别内容中的联系方式、诱导购买等信息,谨防诈骗。如发现有害或侵权内容,请点击一键举报。
    转藏 分享 献花(0

    0条评论

    发表

    请遵守用户 评论公约

    类似文章 更多