SerializeFilter 是通过编程扩展的方式定制序列化。Fastjson 支持如下6种 SerializeFilter,用于不同场景的定制序列化。

  • PropertyPreFilter:根据 PropertyName 判断是否序列化;
  • PropertyFilter:根据 PropertyName 和 PropertyValue 来判断是否序列化;
  • NameFilter:修改 Key,如果需要修改 Key,process 返回值则可;
  • ValueFilter:修改 Value;
  • BeforeFilter:序列化时在最前添加内容;
  • AfterFilter:序列化时在最后添加内容;

需求

JSON 数据格式如下,需要过滤掉其中 “book” 的 “price” 属性。

JSON 数据格式:

  1. {
  2. "store": {
  3. "book": [
  4. {
  5. "category": "reference",
  6. "author": "Nigel Rees",
  7. "title": "Sayings of the Century",
  8. "price": 8.95
  9. },
  10. {
  11. "category": "fiction",
  12. "author": "Evelyn Waugh",
  13. "title": "Sword of Honour",
  14. "price": 12.99
  15. }
  16. ],
  17. "bicycle": {
  18. "color": "red",
  19. "price": 19.95
  20. }
  21. },
  22. "expensive": 10
  23. }

SimplePropertyPreFilter 过滤器

该过滤器由 Fastjson 提供,测试代码如下:

  1. String json = "{\"store\":{\"book\":[{\"category\":\"reference\",\"author\":\"Nigel Rees\",\"title\":\"Sayings of the Century\",\"price\":8.95},{\"category\":\"fiction\",\"author\":\"Evelyn Waugh\",\"title\":\"Sword of Honour\",\"price\":12.99}],\"bicycle\":{\"color\":\"red\",\"price\":19.95}},\"expensive\":10}";
  2. SimplePropertyPreFilter filter = new SimplePropertyPreFilter();
  3. filter.getExcludes().add("price");
  4. JSONObject jsonObject = JSON.parseObject(json);
  5. String str = JSON.toJSONString(jsonObject, filter);
  6. System.out.println(str);

运行结果:

  1. {
  2. "store": {
  3. "bicycle": {
  4. "color": "red"
  5. },
  6. "book": [
  7. {
  8. "author": "Nigel Rees",
  9. "category": "reference",
  10. "title": "Sayings of the Century"
  11. },
  12. {
  13. "author": "Evelyn Waugh",
  14. "category": "fiction",
  15. "title": "Sword of Honour"
  16. }
  17. ]
  18. },
  19. "expensive": 10
  20. }

查看 JSON 数据的过滤结果,发现 “bicycle” 中的 “price” 属性也被过滤掉了,不符合需求。

LevelPropertyPreFilter 过滤器

该自定义过滤器实现 PropertyPreFilter 接口,实现根据层级过滤 JSON 数据中的属性。

扩展类:

LevelPropertyPreFilter.java

测试代码如下:

  1. public static void main(String[] args) {
  2. String json = "{\"store\":{\"book\":[{\"category\":\"reference\",\"author\":\"Nigel Rees\",\"title\":\"Sayings of the Century\",\"price\":8.95},{\"category\":\"fiction\",\"author\":\"Evelyn Waugh\",\"title\":\"Sword of Honour\",\"price\":12.99}],\"bicycle\":{\"color\":\"red\",\"price\":19.95}},\"expensive\":10}";
  3. JSONObject jsonObj = JSON.parseObject(json);
  4. LevelPropertyPreFilter propertyPreFilter = new LevelPropertyPreFilter();
  5. propertyPreFilter.addExcludes("store.book.price");
  6. String json2 = JSON.toJSONString(jsonObj, propertyPreFilter);
  7. System.out.println(json2);
  8. }

运行结果:

  1. {
  2. "store": {
  3. "bicycle": {
  4. "color": "red",
  5. "price": 19.95
  6. },
  7. "book": [
  8. {
  9. "author": "Nigel Rees",
  10. "category": "reference",
  11. "title": "Sayings of the Century"
  12. },
  13. {
  14. "author": "Evelyn Waugh",
  15. "category": "fiction",
  16. "title": "Sword of Honour"
  17. }
  18. ]
  19. },
  20. "expensive": 10
  21. }

查看 JSON 数据的过滤结果,实现了上面的需求。

参考:http://www.cnblogs.com/dirgo/p/5178629.html

作者:殷建卫 链接:https://www.yuque.com/yinjianwei/vyrvkf/zsi0fh 来源:殷建卫 - 架构笔记 著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。