Exemple #1
0
  protected String value2string(JsonEntityField jef, Object value) {

    Format df = jef.getDataFormat();
    if (df == null) {
      Mirror mirror = Mirror.me(value);
      if (value instanceof Date) {
        df = format.getDateFormat();
      } else if (mirror.isNumber()) {
        df = format.getNumberFormat();
      }
    }
    if (df != null) {
      return df.format(value);
    }
    return value.toString();
  }
Exemple #2
0
  @SuppressWarnings("unchecked")
  private void pojo2Json(Object obj) throws IOException {
    if (null == obj) return;
    /*
     * Default
     */
    Class<?> type = obj.getClass();
    JsonEntity jen = Json.getEntity(Mirror.me(type));
    Method toJsonMethod = jen.getToJsonMethod();
    if (toJsonMethod != null) {
      try {
        if (toJsonMethod.getParameterTypes().length == 0) {
          writer.append(String.valueOf(toJsonMethod.invoke(obj)));
        } else {
          writer.append(String.valueOf(toJsonMethod.invoke(obj, format)));
        }
        return;
      } catch (Exception e) {
        throw Lang.wrapThrow(e);
      }
    }
    List<JsonEntityField> fields = jen.getFields();
    appendBraceBegin();
    increaseFormatIndent();
    ArrayList<Pair> list = new ArrayList<Pair>(fields.size());
    for (JsonEntityField jef : fields) {
      if (jef.isIgnore()) continue;
      String name = jef.getName();
      try {
        Object value = jef.getValue(obj);

        // 判断是否应该被忽略
        if (!this.isIgnore(name, value)) {
          Mirror mirror = null;
          // 以前曾经输出过 ...
          if (null != value) {
            // zozoh: 循环引用的默认行为,应该为 null,以便和其他语言交换数据
            mirror = Mirror.me(value);
            if (mirror.isPojo()) {
              if (memo.contains(value)) value = null;
            }
          }
          // 如果是强制输出为字符串的
          if (null != value && jef.isForceString()) {
            // 数组
            if (value.getClass().isArray()) {
              String[] ss = new String[Array.getLength(value)];
              for (int i = 0; i < ss.length; i++) {
                ss[i] = Array.get(value, i).toString();
              }
              value = ss;
            }
            // 集合
            else if (value instanceof Collection) {
              Collection col = (Collection) Mirror.me(value).born();
              for (Object ele : (Collection) value) {
                col.add(ele.toString());
              }
              value = col;
            }
            // 其他统统变字符串
            else {
              value = value2string(jef, value);
            }
          } else if (jef.hasDataFormat() && null != value && value instanceof Date) {
            value = jef.getDataFormat().format((Date) value);
          } else if (jef.hasDataFormat() && null != value && mirror.isNumber()) {
            value = jef.getDataFormat().format(value);
          }

          // 加入输出列表 ...
          list.add(new Pair(name, value));
        }
      } catch (FailToGetValueException e) {
      }
    }
    writeItem(list);
  }