专业的JAVA编程教程与资源

网站首页 > java教程 正文

fastjson(八)将对象中的空值输出(fastjson valuefilter)

temp10 2024-11-08 13:55:51 java教程 9 ℃ 0 评论

更多java文章与项目资源、毕业设计、福利

关注公众号 程序猿forever

fastjson(八)将对象中的空值输出(fastjson valuefilter)


在fastjson中,缺省是不输出空值的。无论Map中的null和对象属性中的null,序列化的时候都会被忽略不输出,这样会减少产生文本的大小。但如果需要输出空值怎么做呢?

使用SerializerFeature.WriteMapNullValue:

Model obj = ...;
JSON.toJSONString(obj, SerializerFeature.WriteMapNullValue);12

空值特别处理:

SerializerFeature描述WriteNullListAsEmpty将Collection类型字段的字段空值输出为[]WriteNullStringAsEmpty将字符串类型字段的空值输出为空字符串 “”WriteNullNumberAsZero将数值类型字段的空值输出为0WriteNullBooleanAsFalse将Boolean类型字段的空值输出为false


来看一下示例代码:

package json.fastjson.empty;

import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

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

public class TestNull {

    public static void main(String[] args) {

        // WriteNullListAsEmpty 将Collection类型字段的字段空值输出为[]
        // WriteNullStringAsEmpty 将字符串类型字段的空值输出为空字符串 ""
        // WriteNullNumberAsZero 将数值类型字段的空值输出为0
        // WriteNullBooleanAsFalse 将Boolean类型字段的空值输出为false

        Model obj = new Model();

        System.out.println("------------------加上参数------------------------------");
        String text = JSON.toJSONString(obj, 
                SerializerFeature.WriteNullListAsEmpty, 
                SerializerFeature.WriteNullStringAsEmpty,
                SerializerFeature.WriteNullNumberAsZero,
                SerializerFeature.WriteNullBooleanAsFalse,
                SerializerFeature.WriteMapNullValue,
                SerializerFeature.PrettyFormat);

        System.out.println(text + " \n");

        System.out.println("------------------不加参数------------------------------");
        text = JSON.toJSONString(obj,
                SerializerFeature.PrettyFormat);

        System.out.println(text);
    }

}

class Model {
    public int iFull = 9;
    public int iEmpty;

    public Integer integerFull = 9;
    public Integer integerEmpty;

    public boolean bFull = true;
    public boolean bEmpty;

    public Boolean BFull = true;
    public Boolean BEmpty;

    public List<String> listFull = Arrays.asList("挖", "坑", "埋", "你");
    public List<String> listEmpty;

    public Map<String, String> mapFull = new HashMap<String, String>() {{put("a", "b");}};
    public Map<String, String> mapEmpty;

}
package json.fastjson.empty;

import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

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

public class TestNull {

    public static void main(String[] args) {

        // WriteNullListAsEmpty 将Collection类型字段的字段空值输出为[]
        // WriteNullStringAsEmpty 将字符串类型字段的空值输出为空字符串 ""
        // WriteNullNumberAsZero 将数值类型字段的空值输出为0
        // WriteNullBooleanAsFalse 将Boolean类型字段的空值输出为false

        Model obj = new Model();

        System.out.println("------------------加上参数------------------------------");
        String text = JSON.toJSONString(obj, 
                SerializerFeature.WriteNullListAsEmpty, 
                SerializerFeature.WriteNullStringAsEmpty,
                SerializerFeature.WriteNullNumberAsZero,
                SerializerFeature.WriteNullBooleanAsFalse,
                SerializerFeature.WriteMapNullValue,
                SerializerFeature.PrettyFormat);

        System.out.println(text + " \n");

        System.out.println("------------------不加参数------------------------------");
        text = JSON.toJSONString(obj,
                SerializerFeature.PrettyFormat);

        System.out.println(text);
    }

}

class Model {
    public int iFull = 9;
    public int iEmpty;

    public Integer integerFull = 9;
    public Integer integerEmpty;

    public boolean bFull = true;
    public boolean bEmpty;

    public Boolean BFull = true;
    public Boolean BEmpty;

    public List<String> listFull = Arrays.asList("挖", "坑", "埋", "你");
    public List<String> listEmpty;

    public Map<String, String> mapFull = new HashMap<String, String>() {{put("a", "b");}};
    public Map<String, String> mapEmpty;

}
package json.fastjson.empty;

import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

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

public class TestNull {

    public static void main(String[] args) {

        // WriteNullListAsEmpty 将Collection类型字段的字段空值输出为[]
        // WriteNullStringAsEmpty 将字符串类型字段的空值输出为空字符串 ""
        // WriteNullNumberAsZero 将数值类型字段的空值输出为0
        // WriteNullBooleanAsFalse 将Boolean类型字段的空值输出为false

        Model obj = new Model();

        System.out.println("------------------加上参数------------------------------");
        String text = JSON.toJSONString(obj, 
                SerializerFeature.WriteNullListAsEmpty, 
                SerializerFeature.WriteNullStringAsEmpty,
                SerializerFeature.WriteNullNumberAsZero,
                SerializerFeature.WriteNullBooleanAsFalse,
                SerializerFeature.WriteMapNullValue,
                SerializerFeature.PrettyFormat);

        System.out.println(text + " \n");

        System.out.println("------------------不加参数------------------------------");
        text = JSON.toJSONString(obj,
                SerializerFeature.PrettyFormat);

        System.out.println(text);
    }

}

class Model {
    public int iFull = 9;
    public int iEmpty;

    public Integer integerFull = 9;
    public Integer integerEmpty;

    public boolean bFull = true;
    public boolean bEmpty;

    public Boolean BFull = true;
    public Boolean BEmpty;

    public List<String> listFull = Arrays.asList("挖", "坑", "埋", "你");
    public List<String> listEmpty;

    public Map<String, String> mapFull = new HashMap<String, String>() {{put("a", "b");}};
    public Map<String, String> mapEmpty;

}

输出结果:

------------------加上参数------------------------------
{
    "BEmpty":false,
    "BFull":true,
    "bEmpty":false,
    "bFull":true,
    "iEmpty":0,
    "iFull":9,
    "integerEmpty":0,
    "integerFull":9,
    "listEmpty":[],
    "listFull":[
        "挖",
        "坑",
        "埋",
        "你"
    ],
    "mapEmpty":null,
    "mapFull":{
        "a":"b"
    }
} 

------------------不加参数------------------------------
{
    "BFull":true,
    "bEmpty":false,
    "bFull":true,
    "iEmpty":0,
    "iFull":9,
    "integerFull":9,
    "listFull":[
        "挖",
        "坑",
        "埋",
        "你"
    ],
    "mapFull":{
        "a":"b"
    }
}

本文暂时没有评论,来添加一个吧(●'◡'●)

欢迎 发表评论:

最近发表
标签列表