fastjs转化序列化对象时,属性为空(null)的转化为“”(空字符串)

对规则的理解:

  1. SerializerFeature.WriteMapNullValue 是否输出值为null的字段,默认为false
    也就是说有null时会输出而不是忽略(默认策略是忽略,所以看不到为null的字段)

  2. WriteNullStringAsEmpty—字符类型字段如果为null,输出为”“,而非null 
    注意是字段字段字段,而不是json.put("key",null),所以用它时,字段为null的可以转换为空字符串。

  3. 如果让输出的json中所有为null的字符串都变成空字符串,最简单的做法就是加一个值过滤器,这样就避免了有的字段为null,有的字段为空字符的现象。

贴点示例代码

import java.util.ArrayList;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;

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

public class Demo1 {

    public class Student {
        private String name;
        private int age;
        private boolean isMale;
        private Student gf;

        public String getName() {
            return name;
        }

        public void setName(String name) {
            this.name = name;
        }

        public int getAge() {
            return age;
        }

        public void setAge(int age) {
            this.age = age;
        }

        public boolean isMale() {
            return isMale;
        }

        public void setMale(boolean isMale) {
            this.isMale = isMale;
        }

        public Student getGf() {
            return gf;
        }

        public void setGf(Student gf) {
            this.gf = gf;
        }
    }

    private static ValueFilter filter = new ValueFilter() {
        @Override
        public Object process(Object obj, String s, Object v) {
            if (v == null)
                return "";
            return v;
        }
    };

    public static void main(String[] args) {
        new Demo1().foo();
        new Demo1().bar();
    }

    private void foo() {
        System.out.println("foo()---------------------------");
        JSONObject j1 = new JSONObject();
        j1.put("name", "zhangsan");
        j1.put("age", 13);
        j1.put("isMale", true);
        j1.put("gf", null);
        Map<String, Object> fav = new HashMap<String, Object>();
        Set<String> books = new HashSet<String>();
        books.add("三国");
        books.add("史记");
        fav.put("history", books);
        String[] arts = new String[] {};
        fav.put("arts", arts);
        String[] musics = new String[] { "北京欢迎你", "画心" };
        fav.put("musics", musics);
        List<String> sports = new ArrayList<String>();
        fav.put("sports", sports);
        j1.put("fav", fav);
        List<Student> classmates = new ArrayList<Student>();
        classmates.add(new Student());
        Student lisi = new Student();
        lisi.setMale(false);
        lisi.setAge(11);
        classmates.add(lisi);
        Student zhangsan = new Student();
        zhangsan.setAge(13);
        zhangsan.setName("张三");
        zhangsan.setMale(true);
        zhangsan.setGf(lisi);
        classmates.add(zhangsan);
        j1.put("classmates", classmates);
        String str = null;
        j1.put("str", str);
        System.out.println(j1.toString());
        System.out
            .println(JSON.toJSONString(j1, SerializerFeature.WriteMapNullValue, SerializerFeature.WriteNullStringAsEmpty));
        System.out.println(
            JSON.toJSONString(j1, filter, SerializerFeature.WriteMapNullValue, SerializerFeature.WriteNullStringAsEmpty));
        System.out.println(JSON.toJSONString(j1, SerializerFeature.WriteNullStringAsEmpty));
        System.out.println(JSON.toJSONString(j1, filter, SerializerFeature.WriteNullStringAsEmpty));

        Map<String, JSONObject> m = new HashMap<String, JSONObject>();
        m.put("key", j1);
        System.out.println(
            JSON.toJSONString(m, SerializerFeature.WriteNonStringKeyAsString, SerializerFeature.WriteNullStringAsEmpty));
        System.out.println(JSON.toJSONString(m, filter, SerializerFeature.WriteNonStringKeyAsString,
            SerializerFeature.WriteNullStringAsEmpty));

    }

    private void bar() {
        System.out.println("bar()---------------------------");
        Student zhangsan = new Student();
        zhangsan.setAge(13);
        zhangsan.setName("张三");
        zhangsan.setMale(true);
        Student lisi = new Student();
        // lisi.setName("lisi");
        lisi.setMale(false);
        lisi.setAge(11);
        zhangsan.setGf(lisi);
        System.out.println(
            JSON.toJSONString(zhangsan, SerializerFeature.WriteMapNullValue, SerializerFeature.WriteNullStringAsEmpty));
        System.out.println(JSON.toJSONString(zhangsan, SerializerFeature.WriteMapNullValue));
        System.out.println(JSON.toJSONString(zhangsan, SerializerFeature.WriteNullStringAsEmpty));
        System.out.println(JSON.toJSONString(zhangsan));
        System.out.println(JSON.toJSONString(zhangsan, filter));
        System.out.println(JSON.toJSONString(zhangsan, filter, SerializerFeature.WriteMapNullValue,
            SerializerFeature.WriteNullStringAsEmpty));
    }

}

输出结果:

foo()---------------------------
{"isMale":true,"name":"zhangsan","classmates":[{"age":0,"male":false},{"age":11,"male":false},{"age":13,"gf":{"$ref":"$.classmates[1]"},"male":true,"name":"张三"}],"fav":{"sports":[],"musics":["北京欢迎你","画心"],"history":["史记","三国"],"arts":[]},"age":13}
{"str":null,"isMale":true,"name":"zhangsan","classmates":[{"age":0,"gf":null,"male":false,"name":""},{"age":11,"gf":null,"male":false,"name":""},{"age":13,"gf":{"$ref":"$.classmates[1]"},"male":true,"name":"张三"}],"fav":{"sports":[],"musics":["北京欢迎你","画心"],"history":["史记","三国"],"arts":[]},"age":13,"gf":null}
{"str":"","isMale":true,"name":"zhangsan","classmates":[{"age":0,"gf":"","male":false,"name":""},{"age":11,"gf":"","male":false,"name":""},{"age":13,"gf":{"$ref":"$.classmates[1]"},"male":true,"name":"张三"}],"fav":{"sports":[],"musics":["北京欢迎你","画心"],"history":["史记","三国"],"arts":[]},"age":13,"gf":""}
{"isMale":true,"name":"zhangsan","classmates":[{"age":0,"male":false},{"age":11,"male":false},{"age":13,"gf":{"$ref":"$.classmates[1]"},"male":true,"name":"张三"}],"fav":{"sports":[],"musics":["北京欢迎你","画心"],"history":["史记","三国"],"arts":[]},"age":13}
{"str":"","isMale":true,"name":"zhangsan","classmates":[{"age":0,"gf":"","male":false,"name":""},{"age":11,"gf":"","male":false,"name":""},{"age":13,"gf":{"$ref":"$.classmates[1]"},"male":true,"name":"张三"}],"fav":{"sports":[],"musics":["北京欢迎你","画心"],"history":["史记","三国"],"arts":[]},"age":13,"gf":""}
{"key":{"isMale":true,"name":"zhangsan","classmates":[{"age":0,"male":false},{"age":11,"male":false},{"age":13,"gf":{"$ref":"$.key.classmates[1]"},"male":true,"name":"张三"}],"fav":{"sports":[],"musics":["北京欢迎你","画心"],"history":["史记","三国"],"arts":[]},"age":13}}
{"key":{"str":"","isMale":true,"name":"zhangsan","classmates":[{"age":0,"gf":"","male":false,"name":""},{"age":11,"gf":"","male":false,"name":""},{"age":13,"gf":{"$ref":"$.key.classmates[1]"},"male":true,"name":"张三"}],"fav":{"sports":[],"musics":["北京欢迎你","画心"],"history":["史记","三国"],"arts":[]},"age":13,"gf":""}}
bar()---------------------------
{"age":13,"gf":{"age":11,"gf":null,"male":false,"name":""},"male":true,"name":"张三"}
{"age":13,"gf":{"age":11,"gf":null,"male":false,"name":null},"male":true,"name":"张三"}
{"age":13,"gf":{"age":11,"male":false},"male":true,"name":"张三"}
{"age":13,"gf":{"age":11,"male":false},"male":true,"name":"张三"}
{"age":13,"gf":{"age":11,"gf":"","male":false,"name":""},"male":true,"name":"张三"}
{"age":13,"gf":{"age":11,"gf":"","male":false,"name":""},"male":true,"name":"张三"}
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值