当前位置:网站首页>@jsonfield annotation in fastjson

@jsonfield annotation in fastjson

2022-06-23 05:36:00 y_ bccl27

Use fastjson The following dependencies must be introduced before , The current version is 1.2.75

<dependency>
	<groupId>com.alibaba</groupId>
	<artifactId>fastjson</artifactId>
	<version>1.2.75</version>
</dependency>

JSONField Medium name Property to specify JSON In a string key The name of

[email protected] Annotation works on attributes ( Member variables ) On

import com.alibaba.fastjson.annotation.JSONField;

public class Person {

    @JSONField(name = "userName")
    private String name;

    @JSONField(name = "AGE")
    private String age;

    public String getName() {
        return name;
    }

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

    public String getAge() {
        return age;
    }

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

Serialization test : 

import com.alibaba.fastjson.JSONObject;
import com.bc.model.Person;

import java.util.Date;

public class Demo {

    public static void main(String[] args){
        Person person=new Person();
        person.setName(" Zhang San ");
        person.setAge("20");
        person.setDate(new Date());
        String jsonStr = JSONObject.toJSONString(person);
        System.out.println(jsonStr);
    }
}

Execute the above code , The output is :

{"AGE":"20","userName":" Zhang San "}

Deserialization test :

import com.alibaba.fastjson.JSONObject;
import com.bc.model.Person;

public class Demo {

    public static void main(String[] args){
        String jsonStr="{\"AGE\":\"20\",\"userName\":\" Zhang San \"}";
        Person person = JSONObject.toJavaObject(JSONObject.parseObject(jsonStr), Person.class);
        System.out.println("json to bean:" + person.getName());
    }
}

Execute the above code , The output is :

json to bean: Zhang San 

@JSONField It works on Field when , Its name Not only does it define the name of the output , It also defines the input key The name of

[email protected] Annotation works on methods

import com.alibaba.fastjson.annotation.JSONField;

public class Person {

    private String name;

    private String age;

    //  For serialization operations 
    @JSONField(name = "userName")
    public String getName() {
        return name;
    }

    //  For deserialization 
    @JSONField(name = "userName")
    public void setName(String name) {
        this.name = name;
    }

    @JSONField(name = "AGE")
    public String getAge() {
        return age;
    }

    @JSONField(name = "AGE")
    public void setAge(String age) {
        this.age = age;
    }
}
import com.alibaba.fastjson.JSONObject;
import com.bc.model.Person;

public class Demo {

    public static void main(String[] args){
        //  serialize 
        Person person=new Person();
        person.setName(" Zhang San ");
        person.setAge("20");
        String jsonStr = JSONObject.toJSONString(person);
        System.out.println(jsonStr);

        //  Deserialization 
        // String jsonStr="{\"AGE\":\"20\",\"userName\":\" Zhang San \"}";
        // Person person = JSONObject.toJavaObject(JSONObject.parseObject(jsonStr), Person.class);
        // System.out.println("json to bean:" + person.getName());
    }
}

Execute the above code , The output is :

{"AGE":"20","userName":" Zhang San "}

fastjson During operation , It's based on getter and setter The method of , Not based on Field Conduct

[email protected] In the annotations format attribute

 format Property is used to specify the date format of member variables when serializing and deserializing  

import com.alibaba.fastjson.annotation.JSONField;
import java.util.Date;

public class Person {

    private String name;

    private String age;

    @JSONField(format="yyyy-MM-dd HH:mm:ss")
    private Date date;

    public String getName() {
        return name;
    }

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

    public String getAge() {
        return age;
    }

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

    public Date getDate() {
        return date;
    }

    public void setDate(Date date) {
        this.date = date;
    }
}
import com.alibaba.fastjson.JSONObject;
import com.bc.model.Person;

import java.util.Date;

public class Demo {

    public static void main(String[] args){
        //  serialize 
        Person person=new Person();
        person.setName(" Zhang San ");
        person.setAge("20");
        person.setDate(new Date());
        String jsonStr = JSONObject.toJSONString(person);
        System.out.println(jsonStr);
    }
}

Execute the above code , The output is :

{"age":"20","date":"2022-06-21 09:52:37","name":" Zhang San "}

[email protected] In the annotations ordinal attribute

ordinal Property is used to specify the order of fields when serializing

import com.alibaba.fastjson.annotation.JSONField;

public class Person {

    @JSONField(ordinal = 1)
    private String name;

    @JSONField(ordinal = 2)
    private String age;

    @JSONField(ordinal = 3)
    private String sex;

    public String getName() {
        return name;
    }

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

    public String getAge() {
        return age;
    }

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

    public String getSex() {
        return sex;
    }

    public void setSex(String sex) {
        this.sex = sex;
    }
}
import com.alibaba.fastjson.JSONObject;
import com.bc.model.Person;

public class Demo {

    public static void main(String[] args){
        //  serialize 
        Person person=new Person();
        person.setName(" Zhang San ");
        person.setAge("20");
        person.setSex(" male ");
        String jsonStr = JSONObject.toJSONString(person);
        System.out.println(jsonStr);
    }
}

Execute the above code , The output is :

{"name":" Zhang San ","age":"20","sex":" male "}

[email protected] In the annotations serialize attribute

serialize Attribute whose value is false Time indicates that the field is not serialized , Is to transform into json String does not generate this field

import com.alibaba.fastjson.annotation.JSONField;
import java.util.Date;

public class Person {

    private String name;

    private String age;

    //  The specified field is not serialized , Is to transform into json String does not generate this field 
    @JSONField(serialize=false)
    private Date date;

    public String getName() {
        return name;
    }

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

    public String getAge() {
        return age;
    }

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

    public Date getDate() {
        return date;
    }

    public void setDate(Date date) {
        this.date = date;
    }
}
import com.alibaba.fastjson.JSONObject;
import com.bc.model.Person;

import java.util.Date;

public class Demo {

    public static void main(String[] args){
        //  serialize 
        Person person=new Person();
        person.setName(" Zhang San ");
        person.setAge("20");
        person.setDate(new Date());
        String jsonStr = JSONObject.toJSONString(person);
        System.out.println(jsonStr);
    }
}

Execute the above code , The output is :

{"age":"20","name":" Zhang San "}
原网站

版权声明
本文为[y_ bccl27]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/174/202206230343256695.html