Search in sources :

Example 1 with JsonEntityField

use of org.nutz.json.entity.JsonEntityField in project nutz by nutzam.

the class JsonEntityTest method test_entity_field_maker.

@Test
public void test_entity_field_maker() {
    Json.clearEntityCache();
    JENObj obj = new JENObj();
    obj.setAge(100);
    obj.setName("name");
    obj.setObjId(9l);
    Json.setDefaultFieldMaker(new AbstractJsonEntityFieldMaker() {

        @Override
        public JsonEntityField make(Mirror<?> mirror, Method method) {
            return null;
        }

        @Override
        public JsonEntityField make(Mirror<?> mirror, Field field) {
            return null;
        }
    });
    assertEquals("{}", Json.toJson(obj, JsonFormat.compact()));
    Json.clearEntityCache();
    Json.setDefaultFieldMaker(new AbstractJsonEntityFieldMaker() {

        @Override
        public JsonEntityField make(Mirror<?> mirror, Method method) {
            if (method.getName().equals("setName")) {
                String fn = Strings.lowerFirst(method.getName().substring(3));
                return JsonEntityField.eval("another_name", method.getParameterTypes()[0], mirror.getEjecting(fn), new InjectBySetter(method));
            }
            return null;
        }

        @Override
        public JsonEntityField make(Mirror<?> mirror, Field field) {
            return JsonEntityField.eval("test_" + field.getName(), field.getType(), mirror.getEjecting(field), mirror.getInjecting(field.getName()));
        }
    });
    String json = Json.toJson(obj, JsonFormat.compact());
    assertTrue(json.contains("\"test_objId\":9"));
    assertTrue(json.contains("\"test_name\":\"name\""));
    assertTrue(json.contains("\"test_age\":100"));
    assertTrue(json.contains("\"another_name\":\"name\""));
    Json.clearEntityCache();
    Json.setDefaultFieldMaker(new JsonEntityFieldMakerImpl());
}
Also used : JsonEntityField(org.nutz.json.entity.JsonEntityField) Field(java.lang.reflect.Field) JENObj(org.nutz.json.meta.JENObj) JsonEntityField(org.nutz.json.entity.JsonEntityField) InjectBySetter(org.nutz.lang.inject.InjectBySetter) JsonEntityFieldMakerImpl(org.nutz.json.impl.JsonEntityFieldMakerImpl) Method(java.lang.reflect.Method) Test(org.junit.Test)

Example 2 with JsonEntityField

use of org.nutz.json.entity.JsonEntityField 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 3 with JsonEntityField

use of org.nutz.json.entity.JsonEntityField in project nutz by nutzam.

the class ObjConvertImpl method injectObj.

@SuppressWarnings("unchecked")
private Object injectObj(Object model, Mirror<?> mirror) {
    // zzh: 如果是 Object,那么就不要转换了
    if (mirror.getType() == Object.class)
        return model;
    Object obj = mirror.born();
    context.set(fetchPath(), obj);
    Map<String, ?> map = (Map<String, ?>) model;
    JsonEntity jen = Json.getEntity(mirror);
    for (String key : map.keySet()) {
        JsonEntityField jef = jen.getField(key);
        if (jef == null) {
            continue;
        }
        Object val = map.get(jef.getName());
        if (val == null) {
            continue;
        }
        if (isLeaf(val)) {
            if (val instanceof El) {
                val = ((El) val).eval(context);
            }
            // zzh@2012-09-14: 暂时去掉 createBy 吧
            // jef.setValue(obj, Castors.me().castTo(jef.createValue(obj,
            // val, null), Lang.getTypeClass(jef.getGenericType())));
            // jef.setValue(obj, jef.createValue(obj, val, null));
            jef.setValue(obj, Mapl.maplistToObj(val, jef.getGenericType()));
            continue;
        } else {
            path.push(key);
            // jef.setValue(obj, Mapl.maplistToObj(val,
            // me.getGenericsType(0)));
            jef.setValue(obj, Mapl.maplistToObj(val, jef.getGenericType()));
        // zzh@2012-09-14: 暂时去掉 createBy 吧
        // jef.setValue(obj, jef.createValue(obj, val,
        // me.getGenericsType(0)));
        }
    }
    return obj;
}
Also used : JsonEntity(org.nutz.json.entity.JsonEntity) JsonEntityField(org.nutz.json.entity.JsonEntityField) El(org.nutz.el.El) LinkedHashMap(java.util.LinkedHashMap) Map(java.util.Map)

Example 4 with JsonEntityField

use of org.nutz.json.entity.JsonEntityField in project nutz by nutzam.

the class JsonEntityFieldMakerImpl method make.

@Override
public JsonEntityField make(Mirror<?> mirror, final Method method) {
    final JsonField jf = method.getAnnotation(JsonField.class);
    // 忽略方法
    if (null == jf || jf.ignore())
        return null;
    final JsonEntityField[] result = new JsonEntityField[1];
    // 如果有,尝试作新的 Entity
    Callback<Method> whenError = new Callback<Method>() {

        // 给定方法即不是 getter 也不是 setter,靠!玩我!
        public void invoke(Method m) {
            throw Lang.makeThrow(JsonException.class, "JsonField '%s' should be getter/setter pair!", m);
        }
    };
    Callback3<String, Method, Method> whenOk = new Callback3<String, Method, Method>() {

        public void invoke(String name, Method getter, Method setter) {
            // 防止错误
            if (null == getter || null == setter || Strings.isBlank(name)) {
                throw Lang.makeThrow(JsonException.class, "JsonField '%s' should be getter/setter pair!", method);
            }
            // 加入字段表
            JsonEntityField ef = JsonEntityField.eval(Strings.sBlank(jf.value(), name), getter, setter);
            result[0] = ef;
        }
    };
    Mirror.evalGetterSetter(method, whenOk, whenError);
    return result[0];
}
Also used : JsonField(org.nutz.json.JsonField) JsonEntityField(org.nutz.json.entity.JsonEntityField) Callback(org.nutz.lang.util.Callback) Callback3(org.nutz.lang.util.Callback3) Method(java.lang.reflect.Method)

Example 5 with JsonEntityField

use of org.nutz.json.entity.JsonEntityField in project nutz by nutzam.

the class AbstractJsonEntityFieldMaker method make.

@Override
public List<JsonEntityField> make(Mirror<?> mirror) {
    Field[] flds = mirror.getFields();
    List<JsonEntityField> fields = new ArrayList<JsonEntityField>(flds.length);
    for (Field fld : flds) {
        JsonEntityField ef = make(mirror, fld);
        if (null != ef)
            fields.add(ef);
    }
    for (Method m : mirror.getMethods()) {
        JsonEntityField ef = make(mirror, m);
        if (null != ef)
            fields.add(ef);
    }
    return fields;
}
Also used : JsonEntityField(org.nutz.json.entity.JsonEntityField) Field(java.lang.reflect.Field) JsonEntityField(org.nutz.json.entity.JsonEntityField) ArrayList(java.util.ArrayList) Method(java.lang.reflect.Method)

Aggregations

JsonEntityField (org.nutz.json.entity.JsonEntityField)6 Method (java.lang.reflect.Method)4 ArrayList (java.util.ArrayList)3 JsonEntity (org.nutz.json.entity.JsonEntity)3 Field (java.lang.reflect.Field)2 FailToGetValueException (org.nutz.lang.FailToGetValueException)2 IOException (java.io.IOException)1 Collection (java.util.Collection)1 Date (java.util.Date)1 LinkedHashMap (java.util.LinkedHashMap)1 Map (java.util.Map)1 Test (org.junit.Test)1 El (org.nutz.el.El)1 JsonField (org.nutz.json.JsonField)1 JsonEntityFieldMakerImpl (org.nutz.json.impl.JsonEntityFieldMakerImpl)1 JENObj (org.nutz.json.meta.JENObj)1 Mirror (org.nutz.lang.Mirror)1 InjectBySetter (org.nutz.lang.inject.InjectBySetter)1 Callback (org.nutz.lang.util.Callback)1 Callback3 (org.nutz.lang.util.Callback3)1