Detailed explanation of @JSONField annotation in fastJSON

Looking at the source code of the @JSONField annotation, you can see that its scope is on the method (METHOD), the property (FIELD), and the parameter (PARAMETER) in the method.

1. Act on FIELD (member variables)

Note: 1. If the property is private, there must be a set* method. Otherwise it cannot be deserialized.

package com.zhujie;

import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.annotation.JSONField;

public class User {
    @JSONField(name="new_username")
    private String username;
    @JSONField(name="new_password")
    private String password;

    ...

    @Override
    public String toString() {
        // TODO Auto-generated method stub
        return JSONObject.toJSONString(this);
    }
    public static void main(String[] args) {
        User user = new User();
        user.setUsername("lili");
        user.setPassword("123456");

        String jsonStr = JSONObject.toJSONString(user);
        System.out.println("bean to json:"+jsonStr);

        User user1 = JSONObject.toJavaObject(JSONObject.parseObject(jsonStr), User.class);
        System.out.println("json to bean:"+user1.toString());
    }
}

The output is:

bean to json:{"new_password":"123456","new_username":"lili"}
json to bean:{"new_password":"123456","new_username":"lili"}

It can be seen from the above example that the @JSONField annotation can change the name of the serialized and deserialized field.

2. The annotation acts on the set and get methods
    @JSONField(name="new_username")
    public String getUsername() {
        return username;
    }
    @JSONField(name="new_username")
    public void setUsername(String username) {
        this.username = username;
    }

The field name of the object generated by serialization and deserialization is new_username, which can change the name of the field, which has the same effect as directly annotating the property above.

3. Serialize and deserialize usage in annotations

Looking at the source code, you can see that the default values ​​of serialize and deserialize are both true, which means that object serialization and deserialization are allowed by default (even if serialize and deserialize attributes are not set).

    @JSONField(name="new_password", serialize=false, deserialize=false)
    private String password;
4. Format usage in annotations

The default value of the format attribute in the source code is "" an empty string. What I have learned is the format used on the date attribute. If there are other usages, you can exchange and learn from each other.

    @JSONField(format="yyyy-MM-dd HH:mm:ss")
    private Date currentDate;
5. The serialzeFeatures and parseFeatures attributes in the annotation can control some rules of serialization.

Under normal circumstances, if the attribute value of a field is null, the field will not be output during serialization or deserialization;

So is there any way we can control it?

The answer is yes;

We can control it through the attribute value SerializerFeature.WriteMapNullValue of serialzeFeatures and parseFeatures. When the value of the attribute is null, the field is also output, and the value of the field is null.

    @JSONField(format="yyyy-MM-dd HH:mm:ss", serialzeFeatures=SerializerFeature.WriteMapNullValue)
    private Date currentDate;

Of course there are many other properties in the SerializerFeature class, which I will explain in the next chapters.

6. Use ordinal to specify field order

The default fastjson serializes a java bean, which is serialized according to the alphabetical order of fieldName. You can specify the order of fields through ordinal. This feature requires version 1.1.42 or higher.

    @JSONField(ordinal =2)
    private String username;

    @JSONField(ordinal =3)
    private String password;

    @JSONField(ordinal =1)
    private Date currentDate;

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325559001&siteId=291194637
Recommended