When fastjs converts the serialized object, the one whose property is null is converted to '' (empty string)

Understanding of rules:

  1. SerializerFeature.WriteMapNullValue output field with null value, default is false
    That is to say, when there is null, it will output instead of ignore (the default policy is ignore, so no null field can be seen)

  2. WriteNullStringAsEmpty - if the character type field is null, the output is "", not null
    Note that field is field, not json.put("key",null), so when it is used, the null field can be converted to an empty string.

  3. If you want all null strings in the output json to become empty strings, the easiest way is to add a value filter, so as to avoid the phenomenon that some fields are null and some fields are empty characters.

Paste sample code

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("Three countries");
        books.add("Redords of the Grand History of China");
        fav.put("history", books);
        String[] arts = new String[] {};
        fav.put("arts", arts);
        String[] musics = new String[] { "welcome to beijing", "Painting heart" };
        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("Zhang San");
        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("Zhang San");
        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));
    }

}

Output results:

foo()---------------------------
{"isMale":true,"name":"zhangsan","classmates":[{"age":0,"male":false},{"age":11,"male":false},{"age":13,"gf":{"$ref":"$.classmates[1]"},"male":true,"name":"Zhang San"}],"fav":{"sports":[],"musics":["welcome to beijing","Painting heart"],"history":["Redords of the Grand History of China","Three countries"],"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":"Zhang San"}],"fav":{"sports":[],"musics":["welcome to beijing","Painting heart"],"history":["Redords of the Grand History of China","Three countries"],"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":"Zhang San"}],"fav":{"sports":[],"musics":["welcome to beijing","Painting heart"],"history":["Redords of the Grand History of China","Three countries"],"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":"Zhang San"}],"fav":{"sports":[],"musics":["welcome to beijing","Painting heart"],"history":["Redords of the Grand History of China","Three countries"],"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":"Zhang San"}],"fav":{"sports":[],"musics":["welcome to beijing","Painting heart"],"history":["Redords of the Grand History of China","Three countries"],"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":"Zhang San"}],"fav":{"sports":[],"musics":["welcome to beijing","Painting heart"],"history":["Redords of the Grand History of China","Three countries"],"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":"Zhang San"}],"fav":{"sports":[],"musics":["welcome to beijing","Painting heart"],"history":["Redords of the Grand History of China","Three countries"],"arts":[]},"age":13,"gf":""}}
bar()---------------------------
{"age":13,"gf":{"age":11,"gf":null,"male":false,"name":""},"male":true,"name":"Zhang San"}
{"age":13,"gf":{"age":11,"gf":null,"male":false,"name":null},"male":true,"name":"Zhang San"}
{"age":13,"gf":{"age":11,"male":false},"male":true,"name":"Zhang San"}
{"age":13,"gf":{"age":11,"male":false},"male":true,"name":"Zhang San"}
{"age":13,"gf":{"age":11,"gf":"","male":false,"name":""},"male":true,"name":"Zhang San"}
{"age":13,"gf":{"age":11,"gf":"","male":false,"name":""},"male":true,"name":"Zhang San"}

Keywords: JSON Java

Added by spamyboy on Thu, 02 Apr 2020 04:31:21 +0300