Fastjson SerializeFilter
通过SerializeFilter可以使用扩展编程的方式实现定制序列化。Fastjson提供了多种SerializeFilter:
- PropertyPreFilter:根据PropertyName判断是否序列化;
- PropertyFilter:根据PropertyName和PropertyValue来判断是否序列化;
- NameFilter:修改Key,如果需要修改Key,process返回值则可;
- ValueFilter:修改Value;
- BeforeFilter:序列化时在最前添加内容;
- AfterFilter:序列化时在最后添加内容。
准备下面示例的实体类User:
package com.yiidian.domain;
/**
* 一点教程网 - http://www.yiidian.com
*/
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 + "]";
}
}
1 PropertyFilter 根据PropertyName和PropertyValue判断是否序列化
1.1 PropertyFilter源码
public interface PropertyFilter extends SerializeFilter {
boolean apply(Object object, String propertyName, Object propertyValue);
}
1.2 编写测试类
可以通过扩展实现根据Object或者属性名称或者属性值进行判断是否需要序列化。
package com.yiidian.fastjson;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.serializer.PropertyFilter;
import com.yiidian.domain.User;
/**
* 一点教程网 - http://www.yiidian.com
*/
public class MainApp {
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(1L);
user.setName("eric");
String jsonString = JSON.toJSONString(user, filter); // 序列化的时候传入filter
System.out.println("序列化,id=1:" + jsonString + "\n");
user.setId(200L);
jsonString = JSON.toJSONString(user, filter); // 序列化的时候传入filter
System.out.println("序列化,id=200:" + jsonString);
}
}
1.3 运行结果
----------------source=User [id=1, name=eric]
----------------name=id
----------------value=1
----------------source=User [id=1, name=eric]
----------------name=name
----------------value=eric
序列化,id=1:{}
----------------source=User [id=200, name=eric]
----------------name=id
----------------value=200
----------------source=User [id=200, name=eric]
----------------name=name
----------------value=eric
序列化,id=200:{"id":200}
2 NameFilter 序列化时修改Key
2.1 NameFilter源码
public interface NameFilter extends SerializeFilter {
String process(Object object, String propertyName, Object propertyValue);
}
2.2 编写测试类
如果需要,修改Key,process返回值则可
package com.yiidian.fastjson;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.serializer.NameFilter;
import com.alibaba.fastjson.serializer.PascalNameFilter;
import com.yiidian.domain.User;
/**
* 一点教程网 - http://www.yiidian.com
*/
public class MainApp {
public static void main(String args[]){
User user = new User();
user.setId(9L);
user.setName("eric");
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");
}
}
2.3 运行结果
普通序列化:{"id":9,"name":"eric"}
----------------object=User [id=9, name=eric]
----------------name=id
----------------value=9
----------------object=User [id=9, name=eric]
----------------name=name
----------------value=eric
NameFilter序列化:{"id$":9,"name":"eric"}
PascalNameFilter序列化:{"Id":9,"Name":"eric"}
3 ValueFilter 序列化时修改Value
3.1 ValueFilter源码
public interface ValueFilter extends SerializeFilter {
Object process(Object object, String propertyName, Object propertyValue);
}
3.2 编写测试类
如果需要,修改Value,process返回值则可
package com.yiidian.fastjson;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.serializer.ValueFilter;
import com.yiidian.domain.User;
/**
* 一点教程网 - http://www.yiidian.com
*/
public class MainApp {
public static void main(String args[]){
User user = new User();
user.setId(9L);
user.setName("eric");
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");
}
}
3.3 运行结果
普通序列化:{"id":9,"name":"eric"}
----------------object=User [id=9, name=eric]
----------------name=id
----------------value=9
----------------object=User [id=9, name=eric]
----------------name=name
----------------value=eric
ValueFilter序列化:{"id":"9$","name":"eric"}
4 BeforeFilter 序列化时在最前添加内容
4.1 BeforeFilter源码
public abstract class BeforeFilter implements SerializeFilter {
protected final void writeKeyValue(String key, Object value) { ... }
// 需要实现的抽象方法,在实现中调用writeKeyValue添加内容
public abstract void writeBefore(Object object);
}
4.2 编写测试类
在序列化对象的所有属性之前执行某些操作,例如调用 writeKeyValue 添加内容
package com.yiidian.fastjson;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.serializer.BeforeFilter;
import com.yiidian.domain.User;
/**
* 一点教程网 - http://www.yiidian.com
*/
public class MainApp {
public static void main(String args[]){
User user = new User();
user.setId(9L);
user.setName("eric");
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");
}
}
4.3 运行结果
普通序列化:{"id":9,"name":"eric"}
----------------object=User [id=9, name=eric]
----------------name=id
----------------value=9
----------------object=User [id=9, name=eric]
----------------name=name
----------------value=eric
ValueFilter序列化:{"id":"9$","name":"eric"}
5 AfterFilter 序列化时在最后添加内容
5.1 AfterFilter源码
public abstract class AfterFilter implements SerializeFilter {
protected final void writeKeyValue(String key, Object value) { ... }
// 需要实现的抽象方法,在实现中调用writeKeyValue添加内容
public abstract void writeAfter(Object object);
}
5.2 编写测试类
在序列化对象的所有属性之后执行某些操作,例如调用 writeKeyValue 添加内容
package com.yiidian.fastjson;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.serializer.AfterFilter;
import com.yiidian.domain.User;
/**
* 一点教程网 - http://www.yiidian.com
*/
public class MainApp {
public static void main(String args[]){
User user = new User();
user.setId(9L);
user.setName("eric");
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);
}
}
5.3 运行结果
普通序列化:{"id":9,"name":"eric"}
------------User.id=9 User.name=eric
AfterFilter序列化:{"id":9,"name":"eric"}
热门文章
优秀文章