亚洲中字慕日产2020,大陆极品少妇内射AAAAAA,无码av大香线蕉伊人久久,久久精品国产亚洲av麻豆网站

資訊專欄INFORMATION COLUMN

Fastjson - 自定義過濾器(PropertyPreFilter)

everfight / 2934人閱讀

摘要:是通過編程擴展的方式定制序列化。支持種,用于不同場景的定制序列化。數(shù)據(jù)格式過濾器該過濾器由提供,代碼實現(xiàn)運行結(jié)果查看數(shù)據(jù)的過濾結(jié)果,發(fā)現(xiàn)中的屬性也被過濾掉了,不符合需求。過濾器該自定義過濾器實現(xiàn)接口,實現(xiàn)根據(jù)層級過濾數(shù)據(jù)中的屬性。

SerializeFilter是通過編程擴展的方式定制序列化。Fastjson 支持6種 SerializeFilter,用于不同場景的定制序列化。

PropertyPreFilter:根據(jù) PropertyName 判斷是否序列化

PropertyFilter:根據(jù) PropertyName 和 PropertyValue 來判斷是否序列化

NameFilter:修改 Key,如果需要修改 Key,process 返回值則可

ValueFilter:修改 Value

BeforeFilter:序列化時在最前添加內(nèi)容

AfterFilter:序列化時在最后添加內(nèi)容

1. 需求

JSON 數(shù)據(jù)格式如下,需要過濾掉其中 "book" 的 "price" 屬性。

JSON數(shù)據(jù)格式:

{
  "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 過濾器

該過濾器由?Fastjson 提供,代碼實現(xiàn):?

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}";
SimplePropertyPreFilter filter = new SimplePropertyPreFilter();
filter.getExcludes().add("price");
JSONObject jsonObject = JSON.parseObject(json);
String str = JSON.toJSONString(jsonObject, filter);
System.out.println(str);

運行結(jié)果:

{
  "store": {
    "bicycle": {
      "color": "red"
    },
    "book": [
      {
        "author": "Nigel Rees",
        "category": "reference",
        "title": "Sayings of the Century"
      },
      {
        "author": "Evelyn Waugh",
        "category": "fiction",
        "title": "Sword of Honour"
      }
    ]
  },
  "expensive": 10
}

查看 JSON 數(shù)據(jù)的過濾結(jié)果,發(fā)現(xiàn) "bicycle" 中的 "price" 屬性也被過濾掉了,不符合需求。

3. LevelPropertyPreFilter 過濾器

該自定義過濾器實現(xiàn) PropertyPreFilter 接口,實現(xiàn)根據(jù)層級過濾 JSON 數(shù)據(jù)中的屬性。
擴展類:

/**
 * 層級屬性刪除
 * 
 * @author yinjianwei
 * @date 2017年8月24日 下午3:55:19
 *
 */
public class LevelPropertyPreFilter implements PropertyPreFilter {

    private final Class clazz;
    private final Set includes = new HashSet();
    private final Set excludes = new HashSet();
    private int maxLevel = 0;

    public LevelPropertyPreFilter(String... properties) {
        this(null, properties);
    }

    public LevelPropertyPreFilter(Class clazz, String... properties) {
        super();
        this.clazz = clazz;
        for (String item : properties) {
            if (item != null) {
                this.includes.add(item);
            }
        }
    }

    public LevelPropertyPreFilter addExcludes(String... filters) {
        for (int i = 0; i < filters.length; i++) {
            this.getExcludes().add(filters[i]);
        }
        return this;
    }

    public LevelPropertyPreFilter addIncludes(String... filters) {
        for (int i = 0; i < filters.length; i++) {
            this.getIncludes().add(filters[i]);
        }
        return this;
    }

    public boolean apply(JSONSerializer serializer, Object source, String name) {
        if (source == null) {
            return true;
        }

        if (clazz != null && !clazz.isInstance(source)) {
            return true;
        }

        // 過濾帶層級屬性(store.book.price)
        SerialContext serialContext = serializer.getContext();
        String levelName = serialContext.toString();
        levelName = levelName + "." + name;
        levelName = levelName.replace("$.", "");
        levelName = levelName.replaceAll("[d+]", "");
        if (this.excludes.contains(levelName)) {
            return false;
        }

        if (maxLevel > 0) {
            int level = 0;
            SerialContext context = serializer.getContext();
            while (context != null) {
                level++;
                if (level > maxLevel) {
                    return false;
                }
                context = context.parent;
            }
        }

        if (includes.size() == 0 || includes.contains(name)) {
            return true;
        }

        return false;
    }

    public int getMaxLevel() {
        return maxLevel;
    }

    public void setMaxLevel(int maxLevel) {
        this.maxLevel = maxLevel;
    }

    public Class getClazz() {
        return clazz;
    }

    public Set getIncludes() {
        return includes;
    }

    public Set getExcludes() {
        return excludes;
    }
}

代碼實現(xiàn):

public static void main(String[] args) {
    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}";
    JSONObject jsonObj = JSON.parseObject(json);
    LevelPropertyPreFilter propertyPreFilter = new LevelPropertyPreFilter();
    propertyPreFilter.addExcludes("store.book.price");
    String json2 = JSON.toJSONString(jsonObj, propertyPreFilter);
    System.out.println(json2);
}

運行結(jié)果:?

{
  "store": {
    "bicycle": {
      "color": "red",
      "price": 19.95
    },
    "book": [
      {
        "author": "Nigel Rees",
        "category": "reference",
        "title": "Sayings of the Century"
      },
      {
        "author": "Evelyn Waugh",
        "category": "fiction",
        "title": "Sword of Honour"
      }
    ]
  },
  "expensive": 10
}

查看 JSON 數(shù)據(jù)的過濾結(jié)果,實現(xiàn)了上面的需求。

參考:http://www.cnblogs.com/dirgo/...

文章版權(quán)歸作者所有,未經(jīng)允許請勿轉(zhuǎn)載,若此文章存在違規(guī)行為,您可以聯(lián)系管理員刪除。

轉(zhuǎn)載請注明本文地址:http://www.ezyhdfw.cn/yun/70245.html

相關(guān)文章

  • 只因數(shù)據(jù)過濾,方可模擬beanutils框架

    摘要:因而,我從中也知道了,很多公司沒有實現(xiàn)數(shù)據(jù)過濾。因為,那樣將會造成數(shù)據(jù)的冗余。因而,我們這時需要過濾數(shù)據(jù)對象,如代碼所示常用的把圖片轉(zhuǎn)成結(jié)構(gòu)如上訴代碼的轉(zhuǎn)換,公司使用的是這個框架。而棧是存放數(shù)據(jù)的一種結(jié)構(gòu),其采用,即先進后出。 導讀 上一篇文章已經(jīng)詳細介紹了框架與RTTI的關(guān)系,RTTI與反射之間的關(guān)系。尤其是對反射做了詳細說明,很多培訓機構(gòu)也將其作為高級教程來講解。 其實,我工作年限...

    yzzz 評論0 收藏0
  • FastJson幾種常用場景

    JavaBean package com.daily.json; import com.alibaba.fastjson.annotation.JSONField; import java.util.Date; public class Student { @JSONField(name = NAME, ordinal = 3) private String name; ...

    Lionad-Morotar 評論0 收藏0
  • ApiBoot - ApiBoot Http Converter 使用文檔

    摘要:如下所示不配置默認使用自定義是的概念,用于自定義轉(zhuǎn)換實現(xiàn),比如自定義格式化日期自動截取小數(shù)點等。下面提供一個的簡單示例,具體的使用請參考官方文檔。 ApiBoot是一款基于SpringBoot1.x,2.x的接口服務集成基礎框架, 內(nèi)部提供了框架的封裝集成、使用擴展、自動化完成配置,讓接口開發(fā)者可以選著性完成開箱即用, 不再為搭建接口框架而犯愁,從而極大...

    dance 評論0 收藏0
  • 記錄_使用JSR303規(guī)范進行數(shù)據(jù)校驗

    摘要:時間年月日星期三說明使用規(guī)范校驗接口請求參數(shù)源碼第一章理論簡介背景介紹如今互聯(lián)網(wǎng)項目都采用接口形式進行開發(fā)。該規(guī)范定義了一個元數(shù)據(jù)模型,默認的元數(shù)據(jù)來源是注解。 時間:2017年11月08日星期三說明:使用JSR303規(guī)范校驗http接口請求參數(shù) 源碼:https://github.com/zccodere/s... 第一章:理論簡介 1-1 背景介紹 如今互聯(lián)網(wǎng)項目都采用HTTP接口...

    187J3X1 評論0 收藏0
  • 這一次,我連 web.xml 都不要了,純 Java 搭建 SSM 環(huán)境!

    摘要:環(huán)境要求使用純來搭建環(huán)境,要求的版本必須在以上。即視圖解析器解析文件上傳等等,如果都不需要配置的話,這樣就可以了。可以將一個字符串轉(zhuǎn)為對象,也可以將一個對象轉(zhuǎn)為字符串,實際上它的底層還是依賴于具體的庫。中,默認提供了和的,分別是和。 在 Spring Boot 項目中,正常來說是不存在 XML 配置,這是因為 Spring Boot 不推薦使用 XML ,注意,并非不支持,Spring...

    liaorio 評論0 收藏0

發(fā)表評論

0條評論

最新活動
閱讀需要支付1元查看
<