Search in sources :

Example 1 with Mirror

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

the class _Infos method create.

@SuppressWarnings("rawtypes")
private static <T extends FieldInfo> T create(Class<T> classOfT, Field field) {
    T info = Mirror.me(classOfT).born();
    info.name = field.getName();
    // XXX 兼容性改变 从1.b.51开始, 优先走getter/setter
    // 老版本是只从属性取值/设置值,不走getter/setter
    info.fieldType = field.getGenericType();
    Mirror me = Mirror.me(field.getDeclaringClass());
    info.injecting = me.getInjecting(field.getName());
    info.ejecting = me.getEjecting(field.getName());
    return info;
}
Also used : Mirror(org.nutz.lang.Mirror)

Example 2 with Mirror

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

the class JsonEntityField method eval.

@SuppressWarnings({ "deprecation", "rawtypes" })
public static JsonEntityField eval(Mirror<?> mirror, Field fld) {
    if (fld == null) {
        return null;
    }
    // if (fld.getName().startsWith("_") || fld.getName().startsWith("$"))
    if (fld.getName().startsWith("$") && fld.getAnnotation(JsonField.class) == null)
        return null;
    JsonField jf = fld.getAnnotation(JsonField.class);
    JsonEntityField jef = new JsonEntityField();
    jef.genericType = Lang.getFieldType(mirror, fld);
    jef.name = Strings.sBlank(null == jf ? null : jf.value(), fld.getName());
    jef.ejecting = mirror.getEjecting(fld.getName());
    jef.injecting = mirror.getInjecting(fld.getName());
    // 瞬时变量和明确声明忽略的,变 ignore
    if (Modifier.isTransient(fld.getModifiers()) || (null != jf && jf.ignore())) {
        jef.setIgnore(true);
    }
    // 判断字段是否被强制输出为字符串
    if (null != jf) {
        jef.setForceString(jf.forceString());
        String dataFormat = jf.dataFormat();
        if (Strings.isBlank(dataFormat)) {
            dataFormat = jf.dateFormat();
        }
        if (!Strings.isBlank(dataFormat)) {
            Mirror jfmirror = Mirror.me(jef.genericType);
            if (jfmirror.isNumber()) {
                jef.dataFormat = new DecimalFormat(dataFormat);
            } else if (jfmirror.isDateTimeLike()) {
                jef.dataFormat = new SimpleDateFormat(dataFormat);
            }
        }
    }
    JsonIgnore jsonIgnore = fld.getAnnotation(JsonIgnore.class);
    if (jsonIgnore != null) {
        Mirror<?> fldMirror = Mirror.me(fld.getType());
        jef.isInt = fldMirror.isInt();
        jef.isDouble = fldMirror.isDouble() || fldMirror.isFloat();
        jef.hasJsonIgnore = true;
        if (jef.isDouble)
            jef.ignoreNullDouble = jsonIgnore.null_double();
        if (jef.isInt)
            jef.ignoreNullInt = jsonIgnore.null_int();
    }
    return jef;
}
Also used : JsonField(org.nutz.json.JsonField) JsonIgnore(org.nutz.json.JsonIgnore) DecimalFormat(java.text.DecimalFormat) Mirror(org.nutz.lang.Mirror) SimpleDateFormat(java.text.SimpleDateFormat)

Example 3 with Mirror

use of org.nutz.lang.Mirror 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

Mirror (org.nutz.lang.Mirror)3 IOException (java.io.IOException)1 Method (java.lang.reflect.Method)1 DecimalFormat (java.text.DecimalFormat)1 SimpleDateFormat (java.text.SimpleDateFormat)1 ArrayList (java.util.ArrayList)1 Collection (java.util.Collection)1 Date (java.util.Date)1 JsonField (org.nutz.json.JsonField)1 JsonIgnore (org.nutz.json.JsonIgnore)1 JsonEntity (org.nutz.json.entity.JsonEntity)1 JsonEntityField (org.nutz.json.entity.JsonEntityField)1 FailToGetValueException (org.nutz.lang.FailToGetValueException)1