This article is participating in the Java Theme Month – Java Debug Notes EventActive link

Example output code

    public static void main(String[] args) {
        Map<String, String> map = new HashMap<>(16);
        map.put("test1".null);
        map.put("test2".null);
        map.put("test3"."test3");
        map.put("test4"."test4");
        map.put("test5"."test5");
        Object obj = JSON.toJSON(map);
        System.out.println(obj);
    }
Copy the code

The output

{"test4":"test4"."test5":"test5"."test3":"test3"}
Copy the code

Json null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null

  • First exercise to find the wrong idea
  • Second, get familiar with fastJSON source code

debugging

Let’s put the breakpoint on line 15 so we can see if toJSON kills NULL or prints it

And then we use F7 to Step into the toString() method. Because of runtime polymorphism, we call the subclass method, in this case the toString() method in json.class is called as follows:

    public String toString(a) {
        return this.toJSONString();
    }
Copy the code

Then continue with the Step into toJSONString() method

As shown, we have found the most important step, ending debugging (using F9)

    public static void main(String[] args) {
        Map<String, String> map = new HashMap<>(16);
        map.put("test1".null);
        map.put("test2".null);
        map.put("test3"."test3");
        map.put("test4"."test4");
        map.put("test5"."test5");
        Object obj = JSON.toJSON(map);
        SerializeWriter out = new SerializeWriter();
        JSONSerializer jsonSerializer = new JSONSerializer(out);
        jsonSerializer.write(obj);/ / the breakpoint
        System.out.println(obj);
    }
Copy the code

Step into the Write method of MapSerializer

Found the key, is to configure SerializerFeature. WriteMapNullValue

View method header

public void write(JSONSerializer serializer, Object object, Object fieldName, Type fieldType, int features) throws IOException {
        SerializeWriter out = serializer.out;
Copy the code

Guesses are either in SerializeWriter or to configure in SerializeWriter SerializerFeature. WriteMapNullValue

The following code can also be configured

jsonSerializer.config(SerializerFeature.WriteMapNullValue, true);
Copy the code

Check the config method source code

    public void config(SerializerFeature feature, boolean state) {
        this.out.config(feature, state);
    }
Copy the code

It’s still called in SerializeWriter

We can also use json.tojsonString to print directly, and find that there are also configurable areas in the constructor

The final code

    public static void main(String[] args) {
        Map<String, String> map = new HashMap<>(16);
        map.put("test1".null);
        map.put("test2".null);
        map.put("test3"."test3");
        map.put("test4"."test4");
        map.put("test5"."test5");
        Object obj = JSON.toJSON(map);


        SerializeWriter out = new SerializeWriter();
// out.config(SerializerFeature.WriteMapNullValue, true);
        JSONSerializer jsonSerializer = new JSONSerializer(out);
        jsonSerializer.config(SerializerFeature.WriteMapNullValue, true);
        jsonSerializer.write(obj);
        
        // Choose either 1 or 2
        System.out.println(out);/ / 1
        System.out.println(JSON.toJSONString(obj,SerializerFeature.WriteMapNullValue)); / / 2
        
    }
Copy the code

Some people say why don’t you just check the documentation? Well, I can check the documentation, but I don’t. I just debug, I just play