0%

FastJson自定义解析

Link:https://github.com/alibaba/fastjson/wiki/SerializeFilter

SerializeFilter是通过编程扩展的方式定制序列化。fastjson支持6种SerializeFilter,用于不同场景的定制序列化。

PropertyPreFilter 根据PropertyName判断是否序列化
PropertyFilter 根据PropertyName和PropertyValue来判断是否序列化
NameFilter 修改Key,如果需要修改Key,process返回值则可
ValueFilter 修改Value
BeforeFilter 序列化时在最前添加内容
AfterFilter 序列化时在最后添加内容

1
2
3
4
5
6
7
8
NameFilter nameFilter = (object, propertyName, propertyValue) -> {
if (propertyName.equals("pre_id")) return "id";
if (propertyName.equals("pre_name")) return "name";
return propertyName;
};

String s = JSON.toJSONString(json, nameFilter);
System.out.println(s);