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