关于struts2 json插件的正则表达式写法的一点儿总结

Linux大全评论2.5K views阅读模式

最近碰到一个问题,需要将一个集体序列化成json对象,如:List<Person> list=new ArrayList<Person>();

Person对象中有一个属性是Dept类型的,序列化的时候不想将此属性也序列化,也就是要排除该属性,可以在Result中加一个属性,excludeProperties,关键在于值写什么,这是个正则表达式,我还加了一个root参数,值是"list",也就是说让struts2从这个对象开始进行序列化的操作,如果不配的话会默认使用action.

在序列化的过程中,struts2会逐渐进入这个方法

  1. public String write(Object object, Collection<Pattern> excludeProperties,  
  2.                       Collection<Pattern> includeProperties, boolean excludeNullProperties) throws JSONException {  
  3.       this.excludeNullProperties = excludeNullProperties;  
  4.       this.buf.setLength(0);  
  5.       this.root = object;  
  6.       this.exprStack = "";  
  7.       this.buildExpr = ((excludeProperties != null) && !excludeProperties.isEmpty())  
  8.               || ((includeProperties != null) && !includeProperties.isEmpty());  
  9.       this.excludeProperties = excludeProperties;  
  10.       this.includeProperties = includeProperties;  
  11.       this.value(object, null);  
  12.   
  13.       return this.buf.toString();  
  14.   }  

其中的root就是我配的list对象了.最重要的一步是调用value方法,咱们看看这个方法,

  1. this.process(object, method);  

这个方法中又调用了process方法来处理这个对象,

  1. private void process(Object object, Method method) throws JSONException {  
  2.         this.stack.push(object);  
  3.   
  4.         if (object instanceof Class) {  
  5.             this.string(object);  
  6.         } else if (object instanceof Boolean) {  
  7.             this.bool((Boolean) object);  
  8.         } else if (object instanceof Number) {  
  9.             this.add(object);  
  10.         } else if (object instanceof String) {  
  11.             this.string(object);  
  12.         } else if (object instanceof Character) {  
  13.             this.string(object);  
  14.         } else if (object instanceof Map) {  
  15.             this.map((Map) object, method);  
  16.         } else if (object.getClass().isArray()) {  
  17.             this.array(object, method);  
  18.         } else if (object instanceof Iterable) {  
  19.             this.array(((Iterable) object).iterator(), method);  
  20.         } else if (object instanceof Date) {  
  21.             this.date((Date) object, method);  
  22.         } else if (object instanceof Calendar) {  
  23.             this.date(((Calendar) object).getTime(), method);  
  24.         } else if (object instanceof Locale) {  
  25.             this.string(object);  
  26.         } else if (object instanceof Enum) {  
  27.             this.enumeration((Enum) object);  
  28.         } else {  
  29.             this.bean(object);  
  30.         }  
  31.   
  32.         this.stack.pop();  
  33.     }  

这个里面其实就是根据对象的类型来调用相应的方法来处理,可以看出struts2 json插件支持的数据类型.由于咱们是list类型的,会调用 array这个方法.

  1. private void array(Iterator it, Method method) throws JSONException {  
  2.        this.add("[");  
  3.   
  4.        boolean hasData = false;  
  5.        for (int i = 0; it.hasNext(); i++) {  
  6.            String expr = null;  
  7.            if (this.buildExpr) {  
  8.                expr = this.expandExpr(i);  
  9.                if (this.shouldExcludeProperty(expr)) {  
  10.                    it.next();  
  11.                    continue;  
  12.                }  
  13.                expr = this.setExprStack(expr);  
  14.            }  
  15.            if (hasData) {  
  16.                this.add(',');  
  17.            }  
  18.            hasData = true;  
  19.            this.value(it.next(), method);  
  20.            if (this.buildExpr) {  
  21.                this.setExprStack(expr);  
  22.            }  
  23.        }  
  24.   
  25.        this.add("]");  
  26.    }  

add方法就是在最后的结果添加字符串,expandExpr方法就是用来产生正则是表达式的,对每一个属性,都会产生一个正则是表达式,可以看出表达式里面现在是[0],

然后又调用value方法处理第一个person对象.

person对象是Object类型的,在process方法中会调用bean方法,

  1. PropertyDescriptor[] props = info.getPropertyDescriptors();  
  2.   
  3.          boolean hasData = false;  
  4.          for (PropertyDescriptor prop : props) {  
  5.              String name = prop.getName();  
  6.              Method accessor = prop.getReadMethod();  
  7.              Method baseAccessor = findBaseAccessor(clazz, accessor);  
  8.   
  9.              if (baseAccessor != null) {  
  10.                  if (baseAccessor.isAnnotationPresent(JSON.class)) {  
  11.                      JSONAnnotationFinder jsonFinder = new JSONAnnotationFinder(baseAccessor).invoke();  
  12.   
  13.                      if (!jsonFinder.shouldSerialize()) continue;  
  14.                      if (jsonFinder.getName() != null) {  
  15.                          name = jsonFinder.getName();  
  16.                      }  
  17.                  }  
  18.                  // ignore "class" and others   
  19.                  if (this.shouldExcludeProperty(prop)) {  
  20.                      continue;  
  21.                  }  
  22.                  String expr = null;  
  23.                  if (this.buildExpr) {  
  24.                      expr = this.expandExpr(name);  
  25.                      if (this.shouldExcludeProperty(expr)) {  
  26.                          continue;  
  27.                      }  
  28.                      expr = this.setExprStack(expr);  
  29.                  }  
  30.   
  31.                  Object value = accessor.invoke(object);  
  32.                  if (baseAccessor.isAnnotationPresent(JSONFieldBridge.class)) {  
  33.                      value = getBridgedValue(baseAccessor, value);  
  34.                  }  
  35.   
  36.                  boolean propertyPrinted = this.add(name, value, accessor, hasData);  
  37.                  hasData = hasData || propertyPrinted;  
  38.                  if (this.buildExpr) {  
  39.                      this.setExprStack(expr);  
  40.                  }  
  41.              }  
  42.          }  

在bean方法中会得到这个对象的所有属性,然后遍历属性,如果属性名称为id,则通过expandExpr方法会生成[0].id这个表达式,然后在shouldExcludeProperty方法中跟你传的正则表达式进行比较,如果匹配上就会忽略掉该属性,这个方法里面有两个比较,一个是忽略(excludeProperties)的比较,一个是包含(includeProperties)的比较,会先进行忽略的比较,因此,如果匹配上了,就直接返回true,就不会进行包含的比较,这就跟短路的情况差不多,如果没有匹配,则进行下一个属性的比较.

企鹅博客
  • 本文由 发表于 2019年7月20日 22:46:04
  • 转载请务必保留本文链接:https://www.qieseo.com/176044.html

发表评论