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
定制序列化
温高铁 edited this page Jul 27, 2013
·
24 revisions
fastjson支持定制序列化,提供定制序列化的方式包括:
- 根据Key判断是否需要序列化
- 根据Key和Value判断是否需要序列化
- 序列化时修改Key
- 序列化时修改Value
- 序列化时添加KeyValue
API
// 根据PropertyName判断是否序列化
public interface PropertyPreFilter extends SerializeFilter {
boolean apply(JSONSerializer serializer, Object object, String name);
}
// 根据PropertyName和PropertyValue来判断是否序列化
public interface PropertyFilter extends SerializeFilter {
boolean apply(Object object, String propertyName, Object propertyValue);
}
// 修改Key,如果需要修改Key,process返回值则可
public interface NameFilter extends SerializeFilter {
String process(Object object, String propertyName, Object propertyValue);
}
// 修改Value
public interface ValueFilter extends SerializeFilter {
Object process(Object object, String propertyName, Object propertyValue);
}
// 序列化时在最前添加内容
public abstract class BeforeFilter implements SerializeFilter {
protected final void writeKeyValue(String key, Object value) { ... }
// 需要实现的抽象方法,在实现中调用writeKeyValue添加内容
public abstract void writeBefore(Object object);
}
以上的SerializeFilter在JSON.toJSONString中可以使用。
SerializeFilter filter = ...; // 可以是上面5个SerializeFilter的任意一种。
JSON.toJSONString(obj, filter);
如有需要修改本注脚,请联系阿里巴巴,
© 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