Search in sources :

Example 1 with JsonEntity

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

the class JsonEntityTest method test_entity_parse.

@Test
public void test_entity_parse() {
    JsonEntity jen = Json.getEntity(Mirror.me(JENObj.class));
    assertEquals(3, jen.getFields().size());
    assertEquals(long.class, jen.getField("id").getGenericType());
    assertEquals(String.class, jen.getField("name").getGenericType());
    assertEquals(int.class, jen.getField("age").getGenericType());
}
Also used : JsonEntity(org.nutz.json.entity.JsonEntity) JENObj(org.nutz.json.meta.JENObj) Test(org.junit.Test)

Example 2 with JsonEntity

use of org.nutz.json.entity.JsonEntity 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 JsonEntity

use of org.nutz.json.entity.JsonEntity 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 JsonEntity

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

the class Json method getEntity.

/**
     * 获取一个 Json 实体
     */
public static JsonEntity getEntity(Mirror<?> mirror) {
    JsonEntity je = entities.get(mirror.getTypeId());
    if (null == je) {
        je = new JsonEntity(mirror);
        entities.put(mirror.getTypeId(), je);
    }
    return je;
}
Also used : JsonEntity(org.nutz.json.entity.JsonEntity)

Example 5 with JsonEntity

use of org.nutz.json.entity.JsonEntity 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

JsonEntity (org.nutz.json.entity.JsonEntity)5 JsonEntityField (org.nutz.json.entity.JsonEntityField)3 ArrayList (java.util.ArrayList)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 LinkedHashMap (java.util.LinkedHashMap)1 Map (java.util.Map)1 Test (org.junit.Test)1 El (org.nutz.el.El)1 JENObj (org.nutz.json.meta.JENObj)1 Mirror (org.nutz.lang.Mirror)1