美文网首页开发-记录-收藏
Fastjson序列化定制+空值、Null、空数组、List不序

Fastjson序列化定制+空值、Null、空数组、List不序

作者: 依然慢节奏 | 来源:发表于2019-07-28 15:13 被阅读0次

    一、背景

    项目中某个实体的某些属性为空或者为null,数组或List中的个数为0时取消JSON的序列化;

    二、实体类

    实体的大概属性如下:

    import com.baomidou.mybatisplus.annotations.TableName;
    import com.unnet.yjs.base.TreeEntity;
    import io.swagger.annotations.ApiModel;
    import io.swagger.annotations.ApiModelProperty;
    import lombok.ToString;
    
    /**
     * Email: love1208tt@foxmail.com
     * Copyright (c)  2019. missbe
     *
     * @author lyg   19-5-24 下午3:08
     **/
    @TableName("t_department")
    @ApiModel
    public class DepartmentEntity extends TreeEntity<DepartmentEntity> {
        private static final long serialVersionUID = 1L;
    
        @ApiModelProperty(example = "deptName")
        private String deptName;
    
        @ApiModelProperty(example = "office|agent")
        private String deptType;
    
        @ApiModelProperty(example = "deptAddress")
        private String deptAddress;
    
        public String getDeptName() {
            return deptName;
        }
    
        public void setDeptName(String deptName) {
            this.deptName = deptName;
        }
    
        public String getDeptType() {
            return deptType;
        }
    
        public void setDeptType(String deptType) {
            this.deptType = deptType;
        }
    
        public String getDeptAddress() {
            return deptAddress;
        }
    
        public void setDeptAddress(String deptAddress) {
            this.deptAddress = deptAddress;
        }
    
        @Override
        public String toString() {
            return "DepartmentEntity{" +
                    "deptName='" + deptName + '\'' +
                    ", deptType='" + deptType + '\'' +
                    ", deptAddress='" + deptAddress + '\'' +
                    ", parentId=" + parentId +
                    ", level=" + level +
                    ", remarks='" + remarks + '\'' +
                    ", areaId='" + getAreaId() + '\'' +
                    ", id=" + id +
                    '}';
        }
    }
    
    import com.baomidou.mybatisplus.activerecord.Model;
    import com.baomidou.mybatisplus.annotations.TableField;
    import org.hibernate.validator.constraints.Length;
    
    import java.util.List;
    
    /**
     *  Email: love1208tt@foxmail.com
     *  Copyright (c) 2018. missbe
     *  @author lyg  19-5-21 下午9:43
     *
     **/
    
    public abstract class TreeEntity<T extends Model> extends DataEntity<T> {
    
        private static final long serialVersionUID = 1L;
    
        /**
         * varchar(64) NULL父id
         */
        @TableField(value = "parent_id")
        protected Long parentId;
    
        /**
         * 节点层次(第一层,第二层,第三层....)
         */
        protected Integer level;
        /**
         * varchar(1000) NULL路径
         */
        @TableField(value = "parent_ids")
        protected String parentIds;
        /**
         * int(11) NULL排序
         */
        protected Integer sort = 30;
    
        @TableField(exist = false)
        protected List<T> children;
    
        @TableField(exist = false)
        protected T parentTree;
    
        public TreeEntity() {
            super();
            this.sort = 30;
        }
    
        public TreeEntity(Long id) {
            super(id);
        }
    
        public Long getParentId() {
            return parentId;
        }
    
        public void setParentId(Long parentId) {
            this.parentId = parentId;
        }
    
        public Integer getLevel() {
            return level;
        }
    
        public void setLevel(Integer level) {
            this.level = level;
        }
    
        @Length( max = 1000, message = "路径长度必须介于 1 和 1000 之间")
        public String getParentIds() {
            return parentIds;
        }
    
        public void setParentIds(String parentIds) {
            this.parentIds = parentIds;
        }
    
        public Integer getSort() {
            return sort;
        }
    
        public void setSort(Integer sort) {
            this.sort = sort;
        }
    
        public List<T> getChildren() {
            return children;
        }
    
        public void setChildren(List<T> children) {
            this.children = children;
        }
    
        public T getParentTree() {
            return parentTree;
        }
    
        public void setParentTree(T parentTree) {
            this.parentTree = parentTree;
        }
    }
    

    三、定制序列化

        @Test
        public void serializeFilter(){
            DepartmentEntity departmentEntity = new DepartmentEntity();
            departmentEntity.setDeptName("");
            departmentEntity.setDeptType("");
            departmentEntity.setDeptAddress("");
            departmentEntity.setParentId(0L);
            departmentEntity.setLevel(0);
            departmentEntity.setParentIds("");
            departmentEntity.setSort(0);
            departmentEntity.setChildren(Lists.newArrayList());
            departmentEntity.setParentTree(null);
            departmentEntity.setCreateId(0L);
            departmentEntity.setCreateDate(new Date());
            departmentEntity.setUpdateId(0L);
            departmentEntity.setRemarks("");
            departmentEntity.setUpdateDate(new Date());
            departmentEntity.setDelFlag(false);
            departmentEntity.setAreaId("");
            departmentEntity.setId(0L);、
            ////老的写法
            PropertyFilter f = new PropertyFilter() {
                @Override
                public boolean apply(Object o, String key, Object value) {
                    if (value == null) {
                        return false;
                    }
                    if(value instanceof  String && ((String) value).isEmpty()){
                        return false;
                    }
                    if(value instanceof List && ((List) value).size() == 0){
                        return  false;
                    }
                    return true;
                }
            };
    
            ///lambda写法
            PropertyFilter filter = (source, key, value) -> {
                if (value == null) {
                    return false;
                }
                if(value instanceof  String && ((String) value).isEmpty()){
                    return false;
                }
                if(value instanceof List && ((List) value).size() == 0){
                    return  false;
                }
                return true;
            };
            System.out.println(JSON.toJSONString(departmentEntity, filter));
        }
    

    取消定制序列化结果:

    {
        "createDate": 1564297304086,
        "createId": 0,
        "delFlag": false,
        "id": 0,
        "level": 0,
        "parentId": 0,
        "sort": 0,
        "updateDate": 1564297304086,
        "updateId": 0
    }
    

    默认序列化结果对比:

    {
        "areaId": "",
        "children": [],
        "createDate": 1564297811104,
        "createId": 0,
        "delFlag": false,
        "deptAddress": "",
        "deptName": "",
        "deptType": "",
        "id": 0,
        "level": 0,
        "parentId": 0,
        "parentIds": "",
        "remarks": "",
        "sort": 0,
        "updateDate": 1564297811104,
        "updateId": 0
    }
    

    参考:Fastjson API SerializeFilter序列化定制

    相关文章

      网友评论

        本文标题:Fastjson序列化定制+空值、Null、空数组、List不序

        本文链接:https://www.haomeiwen.com/subject/ljxprctx.html