在日常使用中,由于涉及到各种序列化和反序列化的处理,就不能不提 注解
,了解注解的常用方式可以极大地方便我们处理序列化,下面介绍一些在使用Jackson
中涉及到的注解。
1. @JsonProperty
- 字段命名
@JsonProperty
注解用于在序列化时按照给定的字段名命名,在反序列化时,在 json 串中的注解字段给该字段设置属性值。
下面是注解的简单示例:
package org.example;
import com.fasterxml.jackson.annotation.JsonProperty;
public class PersonProperty {
@JsonProperty("first_name")
private String firstName;
public PersonProperty() {
}
public String getFirstName() {
return firstName;
}
public void setFirstName(String firstName) {
this.firstName = firstName;
}
}
public static void jsonPropertyDemo() {
ObjectMapper objectMapper = new ObjectMapper();
PersonProperty pp = new PersonProperty();
pp.setFirstName("Alice");
String jsonString = null;
try {
jsonString = objectMapper.writeValueAsString(pp);
System.out.println("json property: " + jsonString);
} catch (Exception e) {
e.printStackTrace();
}
try {
PersonProperty pp1 = objectMapper.readValue(jsonString, PersonProperty.class);
System.out.println(pp1.getFirstName());
} catch (Exception e) {
e.printStackTrace();
}
}
2. @JsonPropertyOrder
- 字段序列化顺序
@JsonPropertyOrder
加在类上,用以规定数据序列化时字段出现的顺序。
package org.example;
import com.fasterxml.jackson.annotation.JsonPropertyOrder;
// {"name":"Bob","id":"111","age":25,"phone":"12345678910"}
@JsonPropertyOrder({"name", "id", "age", "phone"})
// 没有定义顺序,就按照字典序排列,{"age":25,"id":"111","name":"Bob","phone":"12345678910"}
// @JsonPropertyOrder(alphabetic = true)
public class PersonPropertyOrder {
private String id;
private String name;
private int age;
private String phone;
public PersonPropertyOrder() {
}
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public String getPhone() {
return phone;
}
public void setPhone(String phone) {
this.phone = phone;
}
}
public static void jsonPropertyOrder() {
ObjectMapper objectMapper = new ObjectMapper();
PersonPropertyOrder ppo = new PersonPropertyOrder();
ppo.setAge(25);
ppo.setId("111");
ppo.setName("Bob");
ppo.setPhone("12345678910");
String jsonString = null;
try {
jsonString = objectMapper.writeValueAsString(ppo);
System.out.println("json property: " + jsonString);
} catch (Exception e) {
e.printStackTrace();
}
}
3. @JsonAlias
- 字段别名,反序列化
在数据反序列化时,通过 @JsonAlias
注解来设置字段的值,只要是 alias中的和字段本身都可以正常反序列化。
package org.example;
import com.fasterxml.jackson.annotation.JsonAlias;
public class PersonAlias {
@JsonAlias({"firstName", "personName"})
private String name;
public PersonAlias() {
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
public static void jsonAlias() {
String jsonString1 = "{"name":"Bob"}";
String jsonString2 = "{"firstName":"Bob"}";
String jsonString3 = "{"personName":"Bob"}";
ObjectMapper objectMapper = new ObjectMapper();
try {
PersonAlias p1 = objectMapper.readValue(jsonString1, PersonAlias.class);
PersonAlias p2 = objectMapper.readValue(jsonString2, PersonAlias.class);
PersonAlias p3 = objectMapper.readValue(jsonString3, PersonAlias.class);
System.out.printf("p1: %s, p2: %s, p3: %s", p1.getName(),p2.getName(), p3.getName());
} catch (Exception e) {
e.printStackTrace();
}
}
4. @JsonIgnore
-序列化时忽略字段
@JsonIgnore
加在字段上,用以在序列化时,忽略其,在反序列化时,仅赋值null
。
package org.example;
import com.fasterxml.jackson.annotation.JsonIgnore;
public class PersonIgnore {
private String