美文网首页开发-记录-收藏
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不序

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

  • 接口设计细节

    1、list返回值,如果为空,返回空数组,而不是null; 2、get返回值,如果为空,返回null;

  • Gson & FastJson 序列化

    显示序列化空值 当对象含有空值 默认情况下对象转json字符串: 序列化会过滤掉null值,如果要显示空值需要对序...

  • json序列化和反序列化中需要注意的问题

    空值 对象某一属性为空时,gson和fastjson的默认操作都是直接不序列化该属性,以减小序列化后的大小。那么,...

  • [Android]Gson简单用法

    json为null判断 JsonNull.INSTANCEjson为空数组[]直接将list=.....部分try...

  • MYSQL-空值与非空

    NULL 字段值可以为空 NOT NULL 字段值禁止为空

  • MYSQL第十三课时-测试非空

    非空 语句:NOT NULL作用:字段标志非空后,值不能为空 示例: age 不指定为非空,当插入值的时候,如果不...

  • 简化代码之Java

    1. 去除数组中的空值和null值,并返回新的数组 2. 拼接字符串,若字符串不为空则以空格隔开,为空则忽略(比如...

  • filter ES6方法

    1.创建一个数组,判断数组中是否存在某个值 2.去掉空数组空字符串、undefined、null 3.去掉数组中不...

  • 2-10 空值与非空

    NULL,字段值可以为空,默认。NOT NULL,字段值不能为空。 正确:INSERT users VALUES(...

网友评论

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

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