コード例 #1
0
ファイル: GurmsValidConfig.java プロジェクト: mopeleo/gurms
 /**
  * 从json字符串转换为GurmsValidRule对象
  *
  * @param 完整的key
  * @return GurmsValidRule
  */
 private static List<GurmsValidRule> json2Rule(String rule, String field, boolean serverValid) {
   JSONArray o = JSONArray.fromObject(rule);
   List<GurmsValidRule> list = JSONArray.toList(o, GurmsValidRule.class);
   for (GurmsValidRule gvr : list) {
     // 若是服务器端验证或者没有指定执行页面控件的ID,则默认为属性名
     if (serverValid || StringUtils.isBlank(gvr.getField())) {
       gvr.setField(field);
     }
   }
   return list;
 }
  public void toObject(Reader in, Object target) throws IOException {
    StringBuilder sb = new StringBuilder();
    char[] buffer = new char[1024];
    int len = 0;
    while ((len = in.read(buffer)) > 0) {
      sb.append(buffer, 0, len);
    }
    if (target != null && sb.length() > 0 && sb.charAt(0) == '[') {
      JSONArray jsonArray = JSONArray.fromObject(sb.toString());
      if (target.getClass().isArray()) {
        JSONArray.toArray(jsonArray, target, new JsonConfig());
      } else {
        JSONArray.toList(jsonArray, target, new JsonConfig());
      }

    } else {
      JSONObject jsonObject = JSONObject.fromObject(sb.toString());
      JSONObject.toBean(jsonObject, target, new JsonConfig());
    }
  }