FastJson API接口消息返回JSON数据

前端之家收集整理的这篇文章主要介绍了FastJson API接口消息返回JSON数据前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
package cn.com.shopec.app.common;


import cn.com.shopec.core.common.PageFinder;
import cn.com.shopec.core.common.Query;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.serializer.*;

import java.io.Serializable;
import java.lang.reflect.Type;
import java.util.*;

/**
 * Created by guanfeng.li on 2016/7/8.
 * APP接口返回实体类
 */
public class Result implements Serializable {

    //状态码
    private String status = "200";
    //消息提示
    private String msg = "";
    //响应数据
    private Object data;
    //分页
    private PageFinder page;

    //json格式化
    private SerializeConfig jsonConfig;
    //json 特性配置
    private Set<SerializerFeature> jsonFeatures;
    //json 属性过滤
    private Set<SerializeFilter> jsonFilter;

    public Result() {

    }

    public Result(Object data,Query query,long rowCount) {
        this.data = data;
        this.page = new PageFinder<>(query.getPageNo(),query.getPageSize(),rowCount);
    }

    public Result(String status,String msg,Object data) {
        this.status = status;
        this.msg = msg;
        this.data = data;
    }

    public Result(Object data) {
        this.data = data;
    }

    public Result(Object data,PageFinder page) {
        this.data = data;
        this.page = page;
    }

    /**
     * Created by guanfeng.li   2016/7/19
     * 特性配置
     */
    public Result addSerializerFeature(SerializerFeature... serializerFeatures) {
        addSerializerFeature(3,serializerFeatures);
        return this;
    }

    /**
     * Created by guanfeng.li   2016/7/19
     * 特性配置
     */
    public Result addSerializerFeature(int size,SerializerFeature... serializerFeatures) {
        if (jsonFeatures == null) {
            jsonFeatures = new HashSet<>(size);
        }
        for (SerializerFeature item : serializerFeatures) {
            jsonFeatures.add(item);
        }
        return this;
    }

    /**
     * Created by guanfeng.li   2016/7/19
     * json 属性过滤
     */
    public Result addSerializeFilter(Class<?> clazz,String properties) {
        addSerializeFilter(3,clazz,properties,true);
        return this;
    }

    /**
     * Created by guanfeng.li   2016/7/19
     * json 属性过滤
     */
    public Result addSerializeFilter(int size,SerializeFilter filter) {
        if (jsonFilter == null) {
            jsonFilter = new HashSet<>(size);
        }
        jsonFilter.add(filter);
        return this;
    }

    /**
     * Created by guanfeng.li   2016/7/19
     * json 属性过滤
     */
    public Result addSerializeFilter(SerializeFilter filter) {
        return addSerializeFilter(3,filter);
    }

    /**
     * Created by guanfeng.li   2016/7/19
     * json 属性过滤
     */
    public Result addSerializeFilter(Class<?> clazz,String properties,boolean isinclude) {
        addSerializeFilter(3,isinclude);
        return this;
    }

    /**
     * Created by guanfeng.li   2016/7/19
     * json 属性过滤
     */
    public Result addSerializeFilter(int size,Class<?> clazz,boolean isinclude) {
        if (jsonFilter == null) {
            jsonFilter = new HashSet<>();
        }
        if(clazz!=null && properties==null){
            jsonFilter.add(new SimplePropertyPreFilter(clazz));
            return this;
        }
        if(clazz==null && properties!=null){
            jsonFilter.add(new SimplePropertyPreFilter(properties.split(",")));
            return this;
        }
        if (!isinclude) {
            SimplePropertyPreFilter filter = new SimplePropertyPreFilter(clazz);
            filter.getExcludes().addAll(Arrays.asList(properties.split(",")));
            jsonFilter.add(filter);
        } else {
            jsonFilter.add(new SimplePropertyPreFilter(clazz,properties.split(",")));
        }
        return this;
    }

    /**
     * Created by guanfeng.li   2016/7/19
     * 过滤分页信息
     */
    public Result filterPage(){
        addSerializeFilter(this.getClass(),"page",false);
        return  this;
    }

    /**
     * Created by guanfeng.li   2016/7/19
     * json格式化
     */
    public Result putSerializeConfig(Class cls,ObjectSerializer value) {
        putSerializeConfig(3,cls,value);
        return this;
    }

    /**
     * Created by guanfeng.li   2016/7/19
     * json格式化
     */
    public Result putSerializeConfig(int size,Class cls,ObjectSerializer value) {
        if (jsonConfig == null) {
            jsonConfig = new SerializeConfig(size);
        }
        jsonConfig.put(cls,value);
        return this;
    }

    /**
     * Created by guanfeng.li   2016/7/19
     * 空字符串显示 “”
     * 空集合 []
     * 空Map {}
     * Number 0
     */
    public Result writeNullStringNumberListAsEmpty() {
        addSerializerFeature(5,SerializerFeature.WriteMapNullValue,SerializerFeature.WriteNullStringAsEmpty,SerializerFeature.WriteNullNumberAsZero,SerializerFeature.WriteNullListAsEmpty);
        return this;
    }

    public Result writeNullStringAsEmpty() {
        addSerializerFeature(2,SerializerFeature.WriteNullStringAsEmpty);
        return this;
    }

    public Result writeNullListAsEmpty() {
        addSerializerFeature(2,SerializerFeature.WriteNullListAsEmpty);
        return this;
    }

    public Result writeNullNumberAsZero() {
        addSerializerFeature(2,SerializerFeature.WriteNullNumberAsZero);
        return this;
    }

    /**
     * Created by guanfeng.li   2016/7/19
     * 漂亮格式化
     */
    public Result prettyFormat() {
        addSerializerFeature(SerializerFeature.PrettyFormat);
        return this;
    }

    //添加返回结果
    public Result put(String key,Object value) {
        if (data == null) {
            data = new HashMap();
        } else {
            if (!(data instanceof Map)) {
                return this;
            }
        }

        ((Map) data).put(key,value);
        return this;
    }

    public String getStatus() {
        return status;
    }

    public Result setStatus(String status) {
        this.status = status;
        return this;
    }

    public String getMsg() {
        return msg;
    }

    public Result setMsg(String msg) {
        this.msg = msg;
        return this;
    }

    public Object getData() {
        return data;
    }

    public void setData(Object data) {
        this.data = data;
    }

    public PageFinder getPage() {
        return page;
    }

    public Result setPage(PageFinder page) {
        this.page = page;
        return this;
    }

    public SerializeConfig getJsonConfig() {
        return jsonConfig;
    }

    public Set<SerializerFeature> getJsonFeatures() {
        return jsonFeatures;
    }

    public Set<SerializeFilter> getJsonFilter() {
        return jsonFilter;
    }

    @Override
    public String toString() {
        if (jsonFilter != null || jsonFeatures != null || jsonConfig != null) {
            addSerializeFilter(Result.class,"jsonConfig,jsonFeatures,jsonFilter",false);
        }
        SerializerFeature[] features = new SerializerFeature[jsonFeatures == null ? 0 : jsonFeatures.size()];
        if (jsonFeatures != null) {
            jsonFeatures.toArray(features);
        }

        SerializeFilter[] filters = new SerializeFilter[jsonFilter == null ? 0 : jsonFilter.size()];
        if (jsonFilter != null) {
            jsonFilter.toArray(filters);
        }
        String json;
        if (jsonConfig != null) {
            json = JSON.toJSONString(this,jsonConfig,filters,features);
        } else {
            json = JSON.toJSONString(this,features);
        }
        return json;
    }
}

猜你在找的Json相关文章