简介
SerializeFilter是通过编程扩展的方式定制序列化。fastjson支持6种SerializeFilter,用于不同场景的定制序列化。@H_404_4@
PropertyPreFilter 根据PropertyName判断是否序列化
PropertyFilter 根据PropertyName和PropertyValue来判断是否序列化
NameFilter 修改Key,如果需要修改Key,process返回值则可
ValueFilter 修改Value
BeforeFilter 序列化时在最前添加内容
AfterFilter 序列化时在最后添加内容@H_404_4@
修改属性值
以下示例将name为“Jack”改为“Foo”.@H_404_4@
public class TestFastJson(){
@Test
@Ignore
public void testJSON(){
Person person = new Person(22,"Jack");
ValueFilter valueFilter = new ValueFilter() {
@Override
public Object process(Object o,String propertyName,Object propertyValue) {
if(propertyName.equals("name")){
return new String("Foo"); //返回修改后的属性值对象
}
return propertyValue;
}
};
String jsonString = JSON.toJSONString(person,valueFilter);
System.out.println("jsonString is: " + jsonString);
}
private static class Person{
int age;
String name;
private Person(int age,String name){
this.age = age;
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}