通过SerializeFilter可以使用扩展编程的方式实现定制序列化。fastjson提供了多种SerializeFilter:
下面来看示例代码:
示例对象:
package json.fastjson.SerializeFilter;
public class User {
private Long id;
private String name;
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
@Override
public String toString() {
return "User [id=" + id + ", name=" + name + "]";
}
}
PropertyFilter 接口:
public interface PropertyFilter extends SerializeFilter {
boolean apply(Object object, String propertyName, Object propertyValue);
}
可以通过扩展实现根据object或者属性名称或者属性值进行判断是否需要序列化。例如:
package json.fastjson.SerializeFilter;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.serializer.PropertyFilter;
public class TestPropertyFilter {
public static void main(String[] args) {
PropertyFilter filter = new PropertyFilter() {
@Override
public boolean apply(Object source, String name, Object value) {
System.out.println("----------------source=" + source);
System.out.println("----------------name=" + name);
System.out.println("----------------value=" + value);
System.out.println("");
// 属性是id并且大于等于100时进行序列化
if ("id".equals(name)) {
long id = ((Long) value).longValue();
return id >= 100;
}
return false;
}
};
User user = new User();
user.setId(9L);
user.setName("挖坑埋你");
String jsonString = JSON.toJSONString(user, filter); // 序列化的时候传入filter
System.out.println("序列化,id=9:" + jsonString + "\n");
user.setId(200L);
jsonString = JSON.toJSONString(user, filter); // 序列化的时候传入filter
System.out.println("序列化,id=200:" + jsonString);
}
}
输出结果:
----------------source=User [id=9, name=挖坑埋你]
----------------name=id
----------------value=9
----------------source=User [id=9, name=挖坑埋你]
----------------name=name
----------------value=挖坑埋你
序列化,id=9:{}
----------------source=User [id=200, name=挖坑埋你]
----------------name=id
----------------value=200
----------------source=User [id=200, name=挖坑埋你]
----------------name=name
----------------value=挖坑埋你
序列化,id=200:{"id":200}
NameFilter 接口:
public interface NameFilter extends SerializeFilter {
String process(Object object, String propertyName, Object propertyValue);
}
如果需要修改Key,process返回值则可,例如:
package json.fastjson.SerializeFilter;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.serializer.NameFilter;
import com.alibaba.fastjson.serializer.PascalNameFilter;
public class TestNameFilter {
public static void main(String[] args) {
User user = new User();
user.setId(9L);
user.setName("挖坑埋你");
String jsonString = JSON.toJSONString(user); // 序列化的时候传入filter
System.out.println("普通序列化:" + jsonString + "\n");
NameFilter filter = new NameFilter() {
@Override
public String process(Object object, String name, Object value) {
System.out.println("----------------object=" + object);
System.out.println("----------------name=" + name);
System.out.println("----------------value=" + value);
System.out.println("");
// 属性是id是修改id的名字
if ("id".equals(name)) {
return name + "$";
}
return name;
}
};
jsonString = JSON.toJSONString(user, filter); // 序列化的时候传入filter
System.out.println("NameFilter序列化:" + jsonString + "\n");
// fastjson内置一个PascalNameFilter,用于输出将首字符大写的Pascal风格
jsonString = JSON.toJSONString(user, new PascalNameFilter()); // 序列化的时候传入filter
System.out.println("PascalNameFilter序列化:" + jsonString + "\n");
}
}
输出结果:
普通序列化:{"id":9,"name":"挖坑埋你"}
----------------object=User [id=9, name=挖坑埋你]
----------------name=id
----------------value=9
----------------object=User [id=9, name=挖坑埋你]
----------------name=name
----------------value=挖坑埋你
NameFilter序列化:{"id$":9,"name":"挖坑埋你"}
PascalNameFilter序列化:{"Id":9,"Name":"挖坑埋你"}
ValueFilter 接口:
public interface ValueFilter extends SerializeFilter {
Object process(Object object, String propertyName, Object propertyValue);
}
如果需要修改Value,process返回值则可,例如:
package json.fastjson.SerializeFilter;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.serializer.ValueFilter;
public class TestValueFilter {
public static void main(String[] args) {
User user = new User();
user.setId(9L);
user.setName("挖坑埋你");
String jsonString = JSON.toJSONString(user); // 序列化的时候传入filter
System.out.println("普通序列化:" + jsonString + "\n");
ValueFilter filter = new ValueFilter() {
@Override
public Object process(Object object, String name, Object value) {
System.out.println("----------------object=" + object);
System.out.println("----------------name=" + name);
System.out.println("----------------value=" + value);
System.out.println("");
// 属性是id时修改id的值
if ("id".equals(name)) {
long id = ((Long) value).longValue();
return id + "$";
}
return value;
}
};
jsonString = JSON.toJSONString(user, filter); // 序列化的时候传入filter
System.out.println("ValueFilter序列化:" + jsonString + "\n");
}
}
输出结果:
普通序列化:{"id":9,"name":"挖坑埋你"}
----------------object=User [id=9, name=挖坑埋你]
----------------name=id
----------------value=9
----------------object=User [id=9, name=挖坑埋你]
----------------name=name
----------------value=挖坑埋你
ValueFilter序列化:{"id":"9$","name":"挖坑埋你"}
BeforeFilter 接口:
public abstract class BeforeFilter implements SerializeFilter {
protected final void writeKeyValue(String key, Object value) { ... }
// 需要实现的抽象方法,在实现中调用writeKeyValue添加内容
public abstract void writeBefore(Object object);
}
在序列化对象的所有属性之前执行某些操作,例如调用 writeKeyValue 添加内容:
package json.fastjson.SerializeFilter;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.serializer.BeforeFilter;
public class TestBeforeFilter {
public static void main(String[] args) {
User user = new User();
user.setId(9L);
user.setName("挖坑埋你");
String jsonString = JSON.toJSONString(user); // 序列化的时候传入filter
System.out.println("普通序列化:" + jsonString + "\n");
BeforeFilter filter = new BeforeFilter() {
@Override
public void writeBefore(Object object) {
System.out.println("----------------object=" + object);
User user = (User) object;
System.out.println("----------------User.id=" + user.getId() + " " + "User.name=" + user.getName() + "\n");
user.setName(user.getName() + "$$$");
}
};
jsonString = JSON.toJSONString(user, filter); // 序列化的时候传入filter
System.out.println("BeforeFilter序列化:" + jsonString + "\n");
}
}
输出结果:
普通序列化:{"id":9,"name":"挖坑埋你"}
----------------object=User [id=9, name=挖坑埋你]
----------------User.id=9 User.name=挖坑埋你
BeforeFilter序列化:{"id":9,"name":"挖坑埋你$$$"}
AfterFilter 接口:
public abstract class AfterFilter implements SerializeFilter {
protected final void writeKeyValue(String key, Object value) { ... }
// 需要实现的抽象方法,在实现中调用writeKeyValue添加内容
public abstract void writeAfter(Object object);
}
在序列化对象的所有属性之后执行某些操作,例如调用 writeKeyValue 添加内容,例如:
package json.fastjson.SerializeFilter;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.serializer.AfterFilter;
public class TestAfterFilter {
public static void main(String[] args) {
User user = new User();
user.setId(9L);
user.setName("挖坑埋你");
String jsonString = JSON.toJSONString(user); // 序列化的时候传入filter
System.out.println("普通序列化:" + jsonString + "\n");
AfterFilter filter = new AfterFilter() {
@Override
public void writeAfter(Object object) {
User user = (User) object;
System.out.println("------------User.id=" + user.getId() + " " + "User.name=" + user.getName() + "\n");
user.setName(user.getName() + "$$$");
}
};
jsonString = JSON.toJSONString(user, filter); // 序列化的时候传入filter
System.out.println("AfterFilter序列化:" + jsonString + "\n");
System.out.println(user);
}
}
输出结果:
普通序列化:{"id":9,"name":"挖坑埋你"}
------------User.id=9 User.name=挖坑埋你
AfterFilter序列化:{"id":9,"name":"挖坑埋你"}
User [id=9, name=挖坑埋你$$$]