I'll explain to you from beginning to end why Alibaba specifications force Boolean types not to add is? Usage and analysis of three java json parsing frameworks jackson, gson and fast json

Check Alibaba specifications and you can see that there is a mandatory requirement.
Don't add is to Boolean variables in POJO class. I was a little confused when I saw this for the first time. Basic data type Boolean? Isn't Boolean the packaging type of Boolean? I found that it should be a clerical error. It should be the basic data type of Boolean. But what does that mean? Let's do an experiment to see

Step 1: in POM The json parsing framework commonly used in java is introduced into XML.

<!-- https://mvnrepository.com/artifact/com.fasterxml.jackson.core/jackson-databind -->
<dependency>
    <groupId>com.fasterxml.jackson.core</groupId>
    <artifactId>jackson-databind</artifactId>
    <version>2.13.1</version>
</dependency>

<!-- https://mvnrepository.com/artifact/com.google.code.gson/gson -->
<dependency>
    <groupId>com.google.code.gson</groupId>
    <artifactId>gson</artifactId>
    <version>2.8.9</version>
</dependency>

<!-- https://mvnrepository.com/artifact/com.alibaba/fastjson -->
<dependency>
    <groupId>com.alibaba</groupId>
    <artifactId>fastjson</artifactId>
    <version>1.2.79</version>
</dependency>

Step 2: add a pojo class for testing

class Course {
    private boolean isExpire;

    public boolean isExpire() {
        return isExpire;
    }

    public void setExpire(boolean expire) {
        isExpire = expire;
    }
}

It can be seen that for boolean values of basic types, if the field name contains is, the getter method name and field name are the same in the getter and setter automatically generated, while the is of setter method is automatically removed. Then, how will different frameworks deal with this situation? Next, write test classes for testing.

Step 3: write test classes and test them with different frameworks

    @Test
    public void testJson() throws JsonProcessingException {
        System.out.println("jackson");
        System.out.println(new ObjectMapper().writeValueAsString(new Course()));
        System.out.println("==========================");
        System.out.println("gson");
        System.out.println(new Gson().toJson(new Course()));
        System.out.println("==========================");
        System.out.println("fastjson");
        System.out.println(JSON.toJSONString(new Course()));
    }

View console output

jackson
{"expire":false}
==========================
gson
{"isExpire":false}
==========================
fastjson
{"expire":false}

According to the console output, we can see that the json fields parsed by jackson and fastjson are called expire, while the fields parsed by gson are called isExpire, so we can infer that both jackson and fastjson are parsed by using the get method, while gson is parsed by using the field name.

Note that the set method is not required to parse an object into a json string. The set method can be removed for testing. It is found that there will be no error and the results can be output normally.

class Course {
   private boolean isExpire;

   public boolean isExpire() {
       return isExpire;
   }
}

However, if the get method is also removed, there will be a problem.
You can conduct the test yourself. The conclusion is:
For jackson framework, if there is no get method, JsonProcessingException will be thrown;
For the gson framework, if there is no get method, it can be parsed normally, because it is based on field parsing.
For the fsonjson framework, if there is no get method, it returns {}.

Experiment 1: change the type of pojo to Boolean

Since the basic type is Boolean, is its packing type Boolean OK?
Change the basic type of Boolean to Boolean, and rewrite the getter and setter methods to test

class Course {
    private Boolean isExpire;

    public Boolean getExpire() {
        return isExpire;
    }

    public void setExpire(Boolean expire) {
        isExpire = expire;
    }
}

Run the test class again to see the console output

jackson
{"expire":null}
==========================
gson
{}
==========================
fastjson
{}

You can see that jackson resolved the field name to expire and the value is null. This is because jackson uses the get method to resolve, and the get method is called getExpire. All i resolved fields are called expire. As for why it is null, it is because the default values of all reference types are null
gson and fastjson do not resolve null value attributes.

Experiment 2: modify the field of pojo to expire and the type to boolean

Since there will be problems if there is is is in the name, can I remove is?

class Course {
    private boolean expire;

    public boolean isExpire() {
        return expire;
    }

    public void setExpire(boolean expire) {
        this.expire = expire;
    }
}

Run the test class and view the test results.

jackson
{"expire":false}
==========================
gson
{"expire":false}
==========================
fastjson
{"expire":false}

It can be seen that the results of the three frameworks are the same. What if it is a Boolean of reference type?

Experiment 3: modify the field of pojo to expire and the type to Boolean

class Course {
    private Boolean expire;

    public Boolean getExpire() {
        return expire;
    }

    public void setExpire(Boolean expire) {
        this.expire = expire;
    }
}
jackson
{"expire":null}
==========================
gson
{}
==========================
fastjson
{}

It can be seen that the result is the same as that of Experiment 1.

Summary:

  1. If our field name contains isXxx, if the basic type boolean is used, the getter method automatically generated is called isXxx, which will be the same as our field name; If the reference type used is boolean, the automatically generated getter method will call getXxx away, and the is in the original field name will be gone! Whether it is a basic type or a reference type, the generated name is not what we want. Of course, we can modify the generated name by ourselves, but it will increase the workload. Moreover, in the case of multi person cooperation, a development team will sometimes undoubtedly increase our debug ging time and communication time, resulting in unexpected results.
  2. jackson and fasterjson are both parsed into json strings using the get method of the object. If there is no get method, there will be problems. The gson framework parses the fields and will not report an error even if there is no get method. However, we usually generate get and set methods.

Keywords: Java JSON

Added by aidema on Tue, 01 Feb 2022 15:53:25 +0200