如何对返回给前端的JSON数据进行黑白名单过滤

用Hibernate查询数据的时候,往往是查询了一个bean的数据,然后将这个bean的数据转成json,假设这个Bean有100个属性,但是页面只需要10个属性的数据,无效的数据就会占用大部分的带宽。而做这个黑白名单的过滤就是减少无效数据的传输。

使用的jar包是jackson-databind.jar

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.node.ArrayNode;
import com.fasterxml.jackson.databind.node.ObjectNode;

public class AjaxResult {

    private JsonNode node = null;
    //白名单
    private List whites = new ArrayList();
    //黑名单
    private List blacks = new ArrayList();

    public AjaxResult(JsonNode node) {
        this.node = node;
    }

    /**
     * 加入白名单属性
     * @param props
     * @return
     */
    public AjaxResult whitesProps(String...props) {
        for(String prop:props) {
            if(whites.contains(prop)) continue;
            this.whites.add(prop);
        }
        return this;
    }

    /**
     * 加入黑名单属性
     * @param props
     * @return
     */
    public AjaxResult blacksProps(String...props) {
        for(String prop:props) {
            if(blacks.contains(prop)) continue;
            this.blacks.add(prop);
        }
        return this;
    }


    /**
     * 过滤
     * @return
     */
    public JsonNode filter() {
        //判断JsonNode是ObjectNode还是ArrayNode
        if(this.node instanceof ArrayNode) {
            node = (ArrayNode)node;
            ArrayNode copyNodes = ((ArrayNode) node).deepCopy();
            for(int i = 0;i < copyNodes.size();i++) {
                JsonNode copyNode = copyNodes.get(i);
                whitesFilter((ObjectNode)copyNode);
                blacksFilter((ObjectNode)copyNode);
            }
            return copyNodes;
        }else {
            node = (ObjectNode)node;
            ObjectNode copyNode = node.deepCopy();
            whitesFilter((ObjectNode)copyNode);
            blacksFilter((ObjectNode)copyNode);
            return copyNode;
        }       
    }

    /**
     * 白名单过滤
     * @param objectNode
     * @return 判断objectNode中的属性是否在whites中,如果不存在,就把这个元素从objectNode中移除
     */
    public void whitesFilter(ObjectNode objectNode) {
        if(this.whites.isEmpty()) return;
        Iterator iter = objectNode.fieldNames();
        while(iter.hasNext()) {
            String fieldName = iter.next();
            if(!this.whites.contains(fieldName)) {
                iter.remove();  
            }
        }
    }

    /**
     * 黑名单过滤
     * @param objectNode
     * @return 判断objectNode中的属性是否在blacks中,如果存在,就把这个元素从objectNode中移除
     */
    public void blacksFilter(ObjectNode objectNode) {
        if(this.blacks.isEmpty()) return;
        Iterator iter = objectNode.fieldNames();
        while(iter.hasNext()) {
            String fieldName = iter.next();
            if(this.blacks.contains(fieldName)) {
                iter.remove();
            }
        }
    }
}

单元测试

import org.junit.Test;

import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.node.ArrayNode;
import com.fasterxml.jackson.databind.node.ObjectNode;

import utils.AjaxResult;
import utils.JsonHelper;

public class TestAjaxResult {

    @Test
    public void testFilterJsonNode() {
        ObjectMapper mapper = new ObjectMapper();
        ObjectNode objectNode = mapper.createObjectNode();;
        objectNode.put("name","test");
        objectNode.put("age",10);
        AjaxResult ajaxResult = new AjaxResult(objectNode);
        ajaxResult.blacksProps("name");
        ObjectNode objectNode2 = (ObjectNode) ajaxResult.filter();
        Iterator iter = objectNode2.fieldNames();
        while(iter.hasNext()) {
            String key = iter.next();
            System.out.println(key +"-->"+objectNode2.get(key));
        }
    }

    @Test
    public void testWhiteFilter() {
        ObjectMapper mapper = new ObjectMapper();
        ObjectNode whiteNode = mapper.createObjectNode();
        whiteNode.put("name","test");
        whiteNode.put("age",10);
        ArrayNode arrayNode = mapper.createArrayNode();
        arrayNode.add(whiteNode);
        arrayNode.add(whiteNode);
        AjaxResult ajaxResult = new AjaxResult(arrayNode);
        ObjectNode result = mapper.createObjectNode();
        result.put("Succ",1);
        ArrayNode content = result.putArray("content");
        content.addAll((ArrayNode) ajaxResult.whitesProps("age").filter());
        System.out.println(result.toString());
    }
}

测试结果:
如何对返回给前端的JSON数据进行黑白名单过滤_第1张图片

如何对返回给前端的JSON数据进行黑白名单过滤_第2张图片

你可能感兴趣的:(工具Util类)