大神们看看有什么不合理的没
public class ToJson implements JFinalJsonKit.ToJson<BaseModel> {
private static final Object[] NULL_ARGS = new Object[0];
@Override
public void toJson(BaseModel value, int depth, JFinalJsonKit.JsonResult ret) {
if (JFinalJsonKit.checkDepth(depth--, ret)) {
return ;
}
Map<String, Object> attrs = CPI.getAttrs(value);
Method[] methods = value.getClass().getDeclaredMethods();
for (Method m : methods) {
if (m.getParameterCount() != 0 || m.getReturnType() == void.class) {
continue ;
}
String methodName = m.getName();
int indexOfGet = methodName.indexOf("get");
if (indexOfGet == 0 && methodName.length() > 3) { // Only getter
String attrName = methodName.substring(3);
if (!attrName.equals("Class")) { // Ignore Object.getClass()
try {
attrName = StrKit.firstCharToLowerCase(attrName);
Object attrValue = m.invoke(value, NULL_ARGS);
attrs.put(attrName,attrValue);
} catch (Exception e) {
throw new RuntimeException(e);
}
}
}
else {
int indexOfIs = methodName.indexOf("is");
if (indexOfIs == 0 && methodName.length() > 2) {
String attrName = methodName.substring(2);
try {
attrName = StrKit.firstCharToLowerCase(attrName);
Object attrValue = m.invoke(value, NULL_ARGS);
attrs.put(attrName,attrValue);
} catch (Exception e) {
throw new RuntimeException(e);
}
}
}
}
JFinalJsonKit.modelAndRecordToJson(attrs, depth, ret);
}
}
当然,你的 ToJson 扩展在纯用法上是对的