FastJson自定義複雜物件序列化
阿新 • • 發佈:2018-11-02
總結:
SerializeFilter是通過程式設計擴充套件的方式定製序列化。fastjson支援6種SerializeFilter,用於不同場景的定製序列化。
PropertyPreFilter 根據PropertyName判斷是否序列化
PropertyFilter 根據PropertyName和PropertyValue來判斷是否序列化
NameFilter 修改Key,如果需要修改Key,process返回值則可
ValueFilter 修改Value
BeforeFilter 序列化時在最前新增內容
AfterFilter 序列化時在最後新增內容
package com.sward.dy; import static com.sward.dy.StaticFieldClass.testField; import com.alibaba.fastjson.JSON; import com.alibaba.fastjson.JSONObject; import com.alibaba.fastjson.serializer.AfterFilter; import com.alibaba.fastjson.serializer.BeforeFilter; import com.alibaba.fastjson.serializer.NameFilter; import com.alibaba.fastjson.serializer.PropertyFilter; import com.alibaba.fastjson.serializer.SimplePropertyPreFilter; import com.alibaba.fastjson.serializer.ValueFilter; class Student{ private Integer id; private String name; private char grander; public Integer getId() { return id; } public void setId(Integer id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } public char getGrander() { return grander; } public void setGrander(char grander) { this.grander = grander; } } /** * FastJson自定義複雜物件序列化 * @author dy * */ public class StaticImportDemo { public static void main(String[] args) { //System.out.println(testField); Student s = new Student(); s.setId(1); s.setName("xiaoming"); s.setGrander('M'); //******************************全部欄位序列化******************************************* String jsonS = JSONObject.toJSONString(s); //**************PropertyPreFilter根據PropertyName判斷是否序列化 *************************** SimplePropertyPreFilter filter = new SimplePropertyPreFilter( Student.class, "name", "id"); String chooseJsonFieldS = JSONObject.toJSONString(s, filter); //*****PropertyFilter根據PropertyName和PropertyValue來判斷是否序列化 ************************ PropertyFilter propertyFilter = new PropertyFilter() { @Override public boolean apply(Object object, String name, Object value) { //如果Value為小明就不序列化該欄位 if(value.equals(1)) return false; //欄位Name為grander就不序列化該欄位 if(name.equals("grander")) return false; return true; } }; String chooseJsonFieldValueS = JSON.toJSONString(s,propertyFilter); //***************NameFilter修改Key,如果需要修改Key,process返回值則可 ************************ NameFilter nameFileter = new NameFilter() { @Override public String process(Object object, String name, Object value) { if(name.equals("name")) return "姓名"; return name; } }; String createJsonSNameBySelf = JSON.toJSONString(s, nameFileter); //******************ValueFilter修改Value*********************************************** ValueFilter valueFilter = new ValueFilter() { @Override public Object process(Object object, String name, Object value) { if(name.equals("name")){ return "xiaohong"; } return value; } }; String createJsonSValueBySelf = JSON.toJSONString(s, valueFilter); //*********************BeforeFilter 序列化時在最前新增內容 ********************************* BeforeFilter beforeFilter = new BeforeFilter() { @Override public void writeBefore(Object object) { //這裡可以做點有趣的事情 Student student = (Student)object; student.setId(5); //物件屬性已被修改 } }; String beforeFilterJsonS = JSON.toJSONString(s, beforeFilter); //********************AfterFilter 序列化時在最後新增內容*********************************** AfterFilter afterFilter = new AfterFilter() { @Override public void writeAfter(Object object) { //這裡可以做點有趣的事情 Student student = (Student)object; student.setId(10); } }; String afterFilterJsonS = JSON.toJSONString(s, afterFilter); //********************************輸出結果******************************************** System.out.println("jsonS=====================>"+jsonS); System.out.println("chooseJsonFieldS==========>"+chooseJsonFieldS); System.out.println("chooseJsonFieldValueS=====>"+chooseJsonFieldValueS); System.out.println("createJsonSNameBySelf=====>"+createJsonSNameBySelf); System.out.println("createJsonSValueBySelf====>"+createJsonSValueBySelf); System.out.println("beforeFilterJsonS=========>"+beforeFilterJsonS); System.out.println("afterFilterJsonS=========>"+afterFilterJsonS); System.out.println("s的Id值為:"+s.getId()); } }
結果:
jsonS=====================>{"grander":"M","id":1,"name":"xiaoming"} chooseJsonFieldS==========>{"id":1,"name":"xiaoming"} chooseJsonFieldValueS=====>{"name":"xiaoming"} createJsonSNameBySelf=====>{"grander":"M","id":1,"姓名":"xiaoming"} createJsonSValueBySelf====>{"grander":"M","id":1,"name":"xiaohong"} beforeFilterJsonS=========>{"grander":"M","id":5,"name":"xiaoming"} afterFilterJsonS=========>{"grander":"M","id":5,"name":"xiaoming"} s的Id值為:10