成人国产在线小视频_日韩寡妇人妻调教在线播放_色成人www永久在线观看_2018国产精品久久_亚洲欧美高清在线30p_亚洲少妇综合一区_黄色在线播放国产_亚洲另类技巧小说校园_国产主播xx日韩_a级毛片在线免费

資訊專欄INFORMATION COLUMN

Fastjson - 自定義過濾器(PropertyPreFilter)

everfight / 2766人閱讀

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

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

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

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

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

ValueFilter:修改 Value

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

AfterFilter:序列化時(shí)在最后添加內(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 提供,代碼實(shí)現(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);

運(yùn)行結(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 過濾器

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

/**
 * 層級(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;
        }

        // 過濾帶層級(jí)屬性(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;
    }
}

代碼實(shí)現(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);
}

運(yùn)行結(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é)果,實(shí)現(xiàn)了上面的需求。

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

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

轉(zhuǎn)載請(qǐng)注明本文地址:http://systransis.cn/yun/70245.html

相關(guān)文章

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

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

    yzzz 評(píng)論0 收藏0
  • FastJson幾種常用場(chǎng)景

    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 評(píng)論0 收藏0
  • ApiBoot - ApiBoot Http Converter 使用文檔

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

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

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

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

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

    liaorio 評(píng)論0 收藏0

發(fā)表評(píng)論

0條評(píng)論

最新活動(dòng)
閱讀需要支付1元查看
<