FastJson ???? ???
/ Fastjson 定制序列化
Fastjson 定制序列化
Fastjson 定制序列化
1. 簡(jiǎn)介
fastjson支持多種方式定制序列化。
- 通過(guò)@JSONField定制序列化
- 通過(guò)@JSONType定制序列化
- 通過(guò)SerializeFilter定制序列化
- 通過(guò)ParseProcess定制反序列化
2. 使用@JSONField配置
可以把@JSONField配置在字段或者getter/setter方法上。例如:
public class VO { @JSONField(name="ID") private int id; }
或者
public class VO { private int id; @JSONField(name="ID") public int getId() { return id;} @JSONField(name="ID") public void setId(int value) {this.id = id;} }
更多看這里:JSONField
3. 使用@JSONType配置
和JSONField類似,但JSONType配置在類上,而不是field或者getter/setter方法上。
4. 通過(guò)SerializeFilter定制序列化
通過(guò)SerializeFilter可以使用擴(kuò)展編程的方式實(shí)現(xiàn)定制序列化。fastjson提供了多種SerializeFilter:
- PropertyPreFilter 根據(jù)PropertyName判斷是否序列化
- PropertyFilter 根據(jù)PropertyName和PropertyValue來(lái)判斷是否序列化
- NameFilter 修改Key,如果需要修改Key,process返回值則可
- ValueFilter 修改Value
- BeforeFilter 序列化時(shí)在最前添加內(nèi)容
- AfterFilter 序列化時(shí)在最后添加內(nèi)容
SerializeFilter filter = ...; // 可以是上面5個(gè)SerializeFilter的任意一種。 JSON.toJSONString(obj, filter);
更多看這里: SerializeFilter
5. 通過(guò)ParseProcess定制反序列化
定制反系列化API ParseProcess