詳解jackson注解(二)jackson反系列化注解
jackson中用于反系列化的主要注解有:
注解 | 描述 | |
---|---|---|
1 | JsonCreator | JsonCreator注解在構(gòu)造函數(shù)或者工廠方法上击孩,用于指示實例化類的對象甸鸟。構(gòu)造函數(shù)或者工廠方法有多個參數(shù)時璃谨,參數(shù)必須使用JsonProperty或JacksonInject注解攒发。只有一個參數(shù)時,可以不加盼砍。 |
2 | JsonAlias | @JsonAlias在反序列化時為屬性定義一個或多個別名 |
3 | JsonSetter | @JsonSetter是@JsonProperty的替代方法默辨。JsonSetter注解在某個方法上忘古,表明這個方法是setter方法 |
4 | JsonAnySetter | 反系列化時,@JsonAnySetter注解可以靈活地把類型為Map的屬性作為標(biāo)準(zhǔn)屬性使用疾渴。 |
一千贯、JsonCreator
JsonCreator注解在構(gòu)造函數(shù)或者工廠方法上,用于指示實例化類的對象搞坝。構(gòu)造函數(shù)或者工廠方法有多個參數(shù)時搔谴,參數(shù)必須使用JsonProperty或JacksonInject注解。只有一個參數(shù)時桩撮,可以不加敦第。
1峰弹、bean
import com.fasterxml.jackson.annotation.JsonCreator;
import com.fasterxml.jackson.annotation.JsonProperty;
/**
* @author chushiyan
* @email Y2h1c2hpeWFuMDQxNUAxNjMuY29t(base64)
* @description
*/
public class Linkman implements Serializable {
public int id;
public String name;
public Linkman() {
}
@JsonCreator
// 構(gòu)造函數(shù)或者工廠方法有多個參數(shù)時,參數(shù)必須使用JsonProperty或JacksonInject注解芜果。
public Linkman(@JsonProperty("linkman_name") String name, @JsonProperty("linkman_id") int id) {
this.name = name;
this.id = id;
}
@JsonCreator
// 構(gòu)造函數(shù)或者工廠方法只有一個參數(shù)時鞠呈,可以不加JsonProperty或JacksonInject注解。
public Linkman(String name) {
this.name = name;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
@Override
public String toString() {
return "Linkman{" +
"id=" + id +
", name='" + name + '\'' +
'}';
}
}
2右钾、測試
(1)測試多個參數(shù)的構(gòu)造函數(shù):
@Test
public void testJsonCreate() throws JsonProcessingException {
String json = "{\"linkman_id\":100,\"linkman_name\":\"chushiyan\"}";
ObjectMapper mapper = new ObjectMapper();
Linkman linkman = mapper
.readerFor(Linkman.class)
.readValue(json);
System.out.println(linkman);
}
控制臺打右狭摺:
Linkman{id=100, name='chushiyan'}
(2)測試只有單個參數(shù)的構(gòu)造函數(shù)
@Test
public void testJsonCreate2() throws JsonProcessingException {
String json = "{\"linkman_name\":\"chushiyan\"}";
ObjectMapper mapper = new ObjectMapper();
Linkman linkman = mapper
.readerFor(Linkman.class)
.readValue(json);
System.out.println(linkman);
}
控制臺打印:
Linkman{id=0, name='chushiyan'}
二舀射、JsonAlias
@JsonAlias在反序列化時為屬性定義一個或多個別名窘茁。
1、bean
import com.fasterxml.jackson.annotation.JsonAlias;
import lombok.Data;
import java.io.Serializable;
/**
* @author chushiyan
* @email Y2h1c2hpeWFuMDQxNUAxNjMuY29t(base64)
* @description
*/
@Data
public class Customer implements Serializable {
private String id;
// 為name屬性定義兩個別名
@JsonAlias({ "customer_name", "CustomerName" })
private String name;
}
2后控、測試
@Test
public void testJsonAlias() throws JsonProcessingException {
String json = "{\"id\":100,\"customer_name\":\"chushiyan\"}";
ObjectMapper mapper = new ObjectMapper();
Customer customer = mapper
.readerFor(Customer.class)
.readValue(json);
System.out.println(customer);
}
控制臺打用硎铩:
Customer(id=100, name=chushiyan)
三、JsonSetter
@JsonSetter是@JsonProperty的替代方法浩淘。JsonSetter注解在某個方法上捌朴,表明這個方法是setter方法
1、bean
import com.fasterxml.jackson.annotation.JsonGetter;
import com.fasterxml.jackson.annotation.JsonSetter;
import java.io.Serializable;
/**
* @author chushiyan
* @email Y2h1c2hpeWFuMDQxNUAxNjMuY29t(base64)
* @description
*/
public class Label implements Serializable {
private String id;
private String name;
@JsonGetter("label_id")
public String getId() {
return id;
}
@JsonSetter("label_id")
public void setId(String id) {
this.id = id;
}
@JsonGetter("label_name")
public String getName() {
return name;
}
@JsonSetter("label_name")
public void setName(String name) {
this.name = name;
}
public Label() {
}
public Label(String id, String name) {
this.id = id;
this.name = name;
}
@Override
public String toString() {
return "Label{" +
"id='" + id + '\'' +
", name='" + name + '\'' +
'}';
}
}
2张抄、測試
@Test
public void testJsonSetter() throws JsonProcessingException {
Label label = new Label("100", "jackson");
ObjectMapper mapper = new ObjectMapper();
String json = mapper.writeValueAsString(label);
System.out.println(json);
Label label2 = mapper
.readerFor(Label.class)
.readValue(json);
System.out.println(label2);
}
控制臺打由氨巍:
{"label_id":"100","label_name":"jackson"}
Label{id='100', name='jackson'}
四、JsonAnySetter
反系列化時署惯,@JsonAnySetter注解可以靈活地把類型為Map的屬性作為標(biāo)準(zhǔn)屬性使用左驾。
1、bean
import com.fasterxml.jackson.annotation.JsonAnySetter;
import lombok.Data;
import java.io.Serializable;
import java.util.HashMap;
import java.util.Map;
/**
* @author chushiyan
* @email Y2h1c2hpeWFuMDQxNUAxNjMuY29t(base64)
* @description
*/
@Data
public class Book implements Serializable {
private String id;
@JsonAnySetter
private Map<String, String> details = new HashMap<String, String>();
}
2极谊、測試
@Test
public void testJsonAnySetter() throws JsonProcessingException {
String json = "{"
+ "\"id\" : \"100\","
+ "\"author\" : \"chushiyan\","
+ "\"price\" : \"30\","
+ "\"category\" : \"IT\""
+ "}";
ObjectMapper mapper = new ObjectMapper();
Book book = mapper.readValue(json, Book.class);
System.out.println(book);
}
控制臺輸出:
Book(id=100, details={author=chushiyan, price=30, category=IT})