Search in sources :

Example 1 with FailToGetValueException

use of org.nutz.lang.FailToGetValueException in project nutz by nutzam.

the class ObjCompileImpl method pojo2Json.

private Map<String, Object> pojo2Json(Object obj, Map<String, Object> map) {
    if (null == obj)
        return null;
    Class<? extends Object> type = obj.getClass();
    JsonEntity jen = Json.getEntity(Mirror.me(type));
    List<JsonEntityField> fields = jen.getFields();
    ArrayList<Pair> list = new ArrayList<Pair>(fields.size());
    for (JsonEntityField jef : fields) {
        String name = jef.getName();
        try {
            Object value = jef.getValue(obj);
            if (null != value) {
                // 递归
                Mirror<?> mirror = Mirror.me(value);
                if (mirror.isPojo()) {
                    value = parse(value);
                }
            }
            // 加入输出列表 ...
            list.add(new Pair(name, value));
        } catch (FailToGetValueException e) {
        }
    }
    return writeItem(list, map);
}
Also used : JsonEntity(org.nutz.json.entity.JsonEntity) JsonEntityField(org.nutz.json.entity.JsonEntityField) ArrayList(java.util.ArrayList) FailToGetValueException(org.nutz.lang.FailToGetValueException)

Example 2 with FailToGetValueException

use of org.nutz.lang.FailToGetValueException in project nutz by nutzam.

the class JsonRenderImpl method pojo2Json.

@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 != null && mirror.isNumber())) {
                    value = jef.getDataFormat().format(value);
                }
                // 加入输出列表 ...
                list.add(new Pair(name, value));
            }
        } catch (FailToGetValueException e) {
        }
    }
    writeItem(list);
}
Also used : JsonEntity(org.nutz.json.entity.JsonEntity) ArrayList(java.util.ArrayList) Method(java.lang.reflect.Method) FailToGetValueException(org.nutz.lang.FailToGetValueException) IOException(java.io.IOException) FailToGetValueException(org.nutz.lang.FailToGetValueException) Date(java.util.Date) JsonEntityField(org.nutz.json.entity.JsonEntityField) Collection(java.util.Collection) Mirror(org.nutz.lang.Mirror)

Aggregations

ArrayList (java.util.ArrayList)2 JsonEntity (org.nutz.json.entity.JsonEntity)2 JsonEntityField (org.nutz.json.entity.JsonEntityField)2 FailToGetValueException (org.nutz.lang.FailToGetValueException)2 IOException (java.io.IOException)1 Method (java.lang.reflect.Method)1 Collection (java.util.Collection)1 Date (java.util.Date)1 Mirror (org.nutz.lang.Mirror)1