Search in sources :

Example 1 with JsonIgnore

use of org.nutz.json.JsonIgnore 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)

Aggregations

DecimalFormat (java.text.DecimalFormat)1 SimpleDateFormat (java.text.SimpleDateFormat)1 JsonField (org.nutz.json.JsonField)1 JsonIgnore (org.nutz.json.JsonIgnore)1 Mirror (org.nutz.lang.Mirror)1