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());
}
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);
}
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;
}
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;
}
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);
}
Aggregations