json-lib的toBean问题

XXBean有nested字段需要inject数据和有日期型数据

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
JsonConfig config = new JsonConfig();
config.setPropertySetStrategy(new PropertySetStrategy() {
@Override
public void setProperty(Object bean, String key, Object value)
throws JSONException {
try {
PropertyUtils.setNestedProperty(bean, key, value);
} catch (Exception e) {
e.printStackTrace();
}
}
});

XXBean xx = new XXBean();
XXBeanPK pk = new XXBeanPK();
xx.setPk(pk);

// 需要将字段中的日期型数据由java.sql.Date/java.sql.Timestamp改为java.util.Date
JSONUtils.getMorpherRegistry().registerMorpher(
new DateMorpher(new String[] { "dd/MM/yyyy", "HH:mm" }));

xx = (XX) JSONObject.toBean(JSONObject.fromObject(data, config), xx, config);
log.info("xx "+ ToStringBuilder.reflectionToString(xx, ToStringStyle.MULTI_LINE_STYLE));