This repository has been archived by the owner on Oct 23, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 6.5k
PropertyProcessable_cn
高铁 edited this page Jul 16, 2017
·
3 revisions
PropertyProcessable是1.2.35版本开始支持的自定义反序列化接口。
interface PropertyProcessable {
// 返回property的类型,如果返回空,则由parser自行推断。
Type getType(String name);
// 处理属性值
void apply(String name, Object value);
}
public static class VO implements PropertyProcessable {
public int id;
public String name;
public Value value;
public Type getType(String name) {
if ("value".equals(name)) {
return Value.class;
}
return null;
}
public void apply(String name, Object value) {
if ("vo_id".equals(name)) {
this.id = ((Integer) value).intValue();
} else if ("vo_name".equals(name)) {
this.name = (String) value;
} else if ("value".equals(name)) {
this.value = (Value) value;
}
}
}
public static class Value {
}
///////////// 使用
VO vo = JSON.parseObject("{\"vo_id\":123,\"vo_name\":\"abc\",\"value\":{}}", VO.class);
assertEquals(123, vo.id);
assertEquals("abc", vo.name);
assertNotNull(vo.value);
如有需要修改本注脚,请联系阿里巴巴,
© Alibaba Fastjson Develop Team
注明: 版权所有阿里巴巴,请注明版权所有者
If you need to amend this footnote, please contact Alibaba.
© Alibaba Fastjson Develop Team
Note: Copyright Alibaba, please indicate the copyright owner