更多java文章與專案資源、畢業設計、福利
透過SerializeFilter可以使用擴充套件程式設計的方式實現定製序列化。fastjson提供了多種SerializeFilter:
PropertyPreFilter 根據PropertyName判斷是否序列化;PropertyFilter 根據PropertyName和PropertyValue來判斷是否序列化;NameFilter 修改Key,如果需要修改Key,process返回值則可;ValueFilter 修改Value;BeforeFilter 序列化時在最前新增內容;AfterFilter 序列化時在最後新增內容。下面來看示例程式碼:
示例物件:
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 + "]"; }}12345678910111213141516171819202122232425262728
一、PropertyFilter 根據PropertyName和PropertyValue來判斷是否序列化
PropertyFilter 介面:
public interface PropertyFilter extends SerializeFilter { boolean apply(Object object, String propertyName, Object propertyValue); }123
可以透過擴充套件實現根據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); }}12345678910111213141516171819202122232425262728293031323334353637383940
輸出結果:
----------------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 序列化時修改Key
NameFilter 介面:
public interface NameFilter extends SerializeFilter { String process(Object object, String propertyName, Object propertyValue);}123
如果需要修改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":"挖坑埋你"}12345678910111213
三、ValueFilter 序列化時修改Value
ValueFilter 介面:
public interface ValueFilter extends SerializeFilter { Object process(Object object, String propertyName, Object propertyValue); }123
如果需要修改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"); }}12345678910111213141516171819202122232425262728293031323334353637
輸出結果:
普通序列化:{"id":9,"name":"挖坑埋你"}----------------object=User [id=9, name=挖坑埋你]----------------name=id----------------value=9----------------object=User [id=9, name=挖坑埋你]----------------name=name----------------value=挖坑埋你ValueFilter序列化:{"id":"9$","name":"挖坑埋你"}1234567891011
四、BeforeFilter 序列化時在最前新增內容
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":"挖坑埋你$$$"}123456
五、AfterFilter 序列化時在最後新增內容
AfterFilter 介面:
public abstract class AfterFilter implements SerializeFilter { protected final void writeKeyValue(String key, Object value) { ... } // 需要實現的抽象方法,在實現中呼叫writeKeyValue新增內容 public abstract void writeAfter(Object object); }12345
在序列化物件的所有屬性之後執行某些操作,例如呼叫 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=挖坑埋你$$$]
最新評論