Manual tutorial FastJson
/ Fastjson 定制序列化
Fastjson 定制序列化
Fastjson 定制序列化
1. 簡介
fastjson支持多種方式定制序列化。
- 通過@JSONField定制序列化
- 通過@JSONType定制序列化
- 通過SerializeFilter定制序列化
- 通過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. 通過SerializeFilter定制序列化
通過SerializeFilter可以使用擴展編程的方式實現定制序列化。fastjson提供了多種SerializeFilter:
- PropertyPreFilter 根據PropertyName判斷是否序列化
- PropertyFilter 根據PropertyName和PropertyValue來判斷是否序列化
- NameFilter 修改Key,如果需要修改Key,process返回值則可
- ValueFilter 修改Value
- BeforeFilter 序列化時在最前添加內容
- AfterFilter 序列化時在最后添加內容
SerializeFilter filter = ...; // 可以是上面5個SerializeFilter的任意一種。 JSON.toJSONString(obj, filter);
更多看這里: SerializeFilter
5. 通過ParseProcess定制反序列化
定制反系列化API ParseProcess