use of com.alipay.api.internal.mapping.ApiField in project synergic-developing by zeemood.
the class JSONWriter method model.
private void model(Object object) {
add("{");
boolean addedSomething = false;
Field[] ff = object.getClass().getDeclaredFields();
try {
for (int i = 0; i < ff.length; ++i) {
Field field = ff[i];
// 获取注解
ApiField jsonField = field.getAnnotation(ApiField.class);
ApiListField listField = field.getAnnotation(ApiListField.class);
// 优先处理列表类型注解,非列表类型才处理字段注解
if (listField != null) {
PropertyDescriptor pd = new PropertyDescriptor(field.getName(), object.getClass());
Method accessor = pd.getReadMethod();
if (!accessor.isAccessible())
accessor.setAccessible(true);
Object value = accessor.invoke(object, (Object[]) null);
if (value == null)
continue;
if (addedSomething)
add(',');
add(listField.value(), value, true);
addedSomething = true;
} else if (jsonField != null) {
PropertyDescriptor pd = new PropertyDescriptor(field.getName(), object.getClass());
Method accessor = pd.getReadMethod();
if (!accessor.isAccessible())
accessor.setAccessible(true);
Object value = accessor.invoke(object, (Object[]) null);
if (value == null)
continue;
if (addedSomething)
add(',');
add(jsonField.value(), value, true);
addedSomething = true;
}
}
} catch (IntrospectionException e1) {
AlipayLogger.logBizError(e1);
} catch (IllegalAccessException e2) {
AlipayLogger.logBizError(e2);
} catch (IllegalArgumentException e3) {
AlipayLogger.logBizError(e3);
} catch (InvocationTargetException e4) {
AlipayLogger.logBizError(e4);
}
add("}");
}
Aggregations