原文鏈接:https://www.dubby.cn/detail.html?id=9071
字段命名
-
@JsonProperty
可以指定字段的命名(還可以指定這個(gè)字段需要參與序列化和反序列化)默责。
- @JsonProperty.value:指定的字段名字
- @JsonProperty.index:指定順序多艇,默寫數(shù)據(jù)格式是基于順序(JSON不是這種數(shù)據(jù)格式)蚁堤。
- @JsonProperty.defaultValue:默認(rèn)值幔戏。注意:這個(gè)屬性目前為止并沒有被core和data-bind使用;制備一些擴(kuò)展模塊使用攘烛。
字段包含
- @JsonAutoDetect:定義默認(rèn)的字段包含規(guī)則
- @JsonIgnore:忽略某個(gè)指定的字段:
- 修飾字段魏滚,setter和getter中的任何一個(gè),相當(dāng)于所有都加了
- 除非使用@JsonProperty修飾坟漱,可以實(shí)現(xiàn)只忽略序列化或者反序列化
- 修飾字段魏滚,setter和getter中的任何一個(gè),相當(dāng)于所有都加了
- @JsonIgnoreProperties:修飾類鼠次,指定忽略一個(gè)字段列表,或者忽略那些未知的字段
- @JsonIgnoreType:修飾類,忽略指定的類型的字段
- @JsonInclude:可以定義空值是否參與(反)序列化
字段文檔腥寇,元數(shù)據(jù)
- @JsonPropertyDescription:2.3支持成翩,給字段配置人類閱讀的解釋
- core和data-bind不會(huì)使用這個(gè)注解,主要是被JSON Schema generator這個(gè)模塊使用
反序列化和序列化的細(xì)節(jié)
- @JsonFormat:對(duì)于Date/Time字段赦役,可以指定格式化格式
- @JsonUnwrapped:指定某個(gè)字段(類型是POJO)序列化成扁平化麻敌,而不是嵌套對(duì)象,在反序列化時(shí)再包裝成對(duì)象
- @JsonView:可以定義視圖
@JsonUnwrapped(prefix = "pre")
簡單解釋:
public class MyValue {
public String name;
JsonUnwrapped(prefix = "pre_", suffix = "_suf")
public MyValue myValue;
public int age;
public Date date;
}
序列化結(jié)果:
{"name":"楊正","pre_name_suf":null,"pre_age_suf":0,"pre_date_suf":null,"age":24,"date":"2017-12-09"}
@JsonView
簡單解釋:
public class JsonViewTest {
public static void main(String[] args) throws IOException {
ObjectMapper objectMapper = new ObjectMapper();
String json = "{\"username\":\"dubby.cn\",\"password\":\"123456\"}";
//反序列化掂摔,使用視圖
User user = objectMapper.readerWithView(User.UserWithoutPassword.class).forType(User.class).readValue(json);
System.out.println(user);
user.password = "xxxx";
//序列化术羔,使用視圖
String result1 = objectMapper.writerWithView(User.UserWithoutPassword.class).writeValueAsString(user);
System.out.println(result1);
String result2 = objectMapper.writerWithView(User.UserWithPassword.class).writeValueAsString(user);
System.out.println(result2);
}
}
class User {
@JsonView({UserWithoutPassword.class})
public String username;
@JsonView({UserWithPassword.class})
public String password;
public interface UserWithPassword extends UserWithoutPassword {
}
public interface UserWithoutPassword {
}
@Override
public String toString() {
return "User{" +
"username='" + username + '\'' +
", password='" + password + '\'' +
'}';
}
}
反序列化細(xì)節(jié)
-
@JacksonInject
:指示某個(gè)字段的值是注入的,而不是從JSON中取出的 -
@JsonAnySetter
:修飾一個(gè)2個(gè)參數(shù)的方法乙漓,任何JSON中有级历,而對(duì)象中沒有的字段都會(huì)以(key,value)的形式傳給這個(gè)方法 -
@JsonCreator
:上篇文章自定義構(gòu)造方法介紹過了 -
@JsonSetter
:是@JsonProperty
的替代注解 -
@JsonEnumDefaultValue
:反序列化時(shí),如果遇到未定義的枚舉值時(shí)叭披,賦值為默認(rèn)枚舉
@JsonAnySetter
簡單解釋:
public class JsonAnySetterTest {
public static void main(String[] args) throws IOException {
String json = "{\"username\":\"dubby.cn\",\"password\":\"123456\",\"x-key\":\"xxx-value\",\"y-key\":\"yyy-value\"}";
ObjectMapper objectMapper = new ObjectMapper();
Data data = objectMapper.readValue(json, Data.class);
System.out.println(data);
}
}
class Data {
public String username;
public String password;
public String other;
@JsonAnySetter
public void anySetter(String a, String b) {
if (other == null) {
other = "";
}
other += a;
other += ",";
other += b;
other += ";";
}
@Override
public String toString() {
return "Data{" +
"username='" + username + '\'' +
", password='" + password + '\'' +
", other='" + other + '\'' +
'}';
}
}
輸出:
Data{username='dubby.cn', password='123456', other='x-key,xxx-value;y-key,yyy-value;'}
@JsonEnumDefaultValue
簡單解釋:
public class EnumTest {
public static void main(String[] args) throws IOException {
ObjectMapper objectMapper = new ObjectMapper();
objectMapper.configure(DeserializationFeature.READ_UNKNOWN_ENUM_VALUES_USING_DEFAULT_VALUE, true);
String json = "{\"myEnum\":\"V4\"}";
EnumData enumData = objectMapper.readValue(json, EnumData.class);
System.out.println(enumData);
}
}
class EnumData {
public MyEnum myEnum;
@Override
public String toString() {
return "EnumData{" +
"myEnum=" + myEnum +
'}';
}
}
enum MyEnum {
V1, V2, V3, @JsonEnumDefaultValue Default;
}
輸出:
EnumData{myEnum=Default}
序列化細(xì)節(jié)
-
@JsonAnyGetter
:修飾一個(gè)方法寥殖,返回Map
,這個(gè)方法的返回值會(huì)被序列化成(key,value)形式 -
@JsonGetter
:@JsonPropert
的替代注解 -
@JsonPropertyOrder
:注定序列化的順序 -
@JsonRawValue
:被修飾的字段“準(zhǔn)確”的顯示出來涩蜘,沒有轉(zhuǎn)義或裝飾嚼贡,雙引號(hào)都不加 -
@JsonValue
:指定序列化輸出的值 -
@JsonRootName
:使用這個(gè)指定的值作為JSON的根,前提是SerializationFeature.WRAP_ROOT_VALUE
已經(jīng)打開了
@JsonAnyGetter
簡單解釋:
public class JsonAnyGetterTest {
public static void main(String[] args) throws JsonProcessingException {
ObjectMapper objectMapper = new ObjectMapper();
AnyGetterData data = new AnyGetterData();
data.data = "http://dubby.cn";
System.out.println(objectMapper.writeValueAsString(data));
}
}
class AnyGetterData {
public String data;
@JsonAnyGetter
public Map other() {
Map<String, String> map = new HashMap<>();
map.put("key1", "value1");
map.put("key2", "value2");
map.put("key3", "value3");
return map;
}
}
輸出:
{"data":"http://dubby.cn","key1":"value1","key2":"value2","key3":"value3"}
@JsonPropertyOrder
簡單解釋:
public class JsonPropertyOrderTest {
public static void main(String[] args) throws JsonProcessingException {
ObjectMapper objectMapper = new ObjectMapper();
JsonPropertyOrderData data = new JsonPropertyOrderData();
data.name1 = "value1";
data.name2 = "value3";
data.name3 = "value4";
System.out.println(objectMapper.writeValueAsString(data));
}
}
@JsonPropertyOrder(value = {"name2", "name3", "name1"})
class JsonPropertyOrderData {
public String name1;
public String name2;
public String name3;
}
輸出:
{"name2":"value3","name3":"value4","name1":"value1"}
@JsonValue
簡單解釋:
public class JsonValueTest {
public static void main(String[] args) throws IOException {
ObjectMapper objectMapper = new ObjectMapper();
String json ="{\"name2\":\"value3\",\"name3\":\"value4\",\"name1\":\"value1\"}";
JsonValueData data = objectMapper.readValue(json, JsonValueData.class);
System.out.println(data.toString());
System.out.println(objectMapper.writeValueAsString(data));
}
}
class JsonValueData {
public String name1;
public String name2;
public String name3;
@JsonValue
public String other() {
return name1+name2+name3;
}
@Override
public String toString() {
return "JsonValueData{" +
"name1='" + name1 + '\'' +
", name2='" + name2 + '\'' +
", name3='" + name3 + '\'' +
'}';
}
}
輸出:
JsonValueData{name1='value1', name2='value3', name3='value4'}"value1value3value4"
@JsonRootName
簡單解釋:
public class JsonRootNameTest {
public static void main(String[] args) throws JsonProcessingException {
ObjectMapper objectMapper = new ObjectMapper();
objectMapper.configure(SerializationFeature.WRAP_ROOT_VALUE, true);
RootData data = new RootData();
data.name1 = "value1";
data.name2 = "value2";
data.name3 = "value3";
System.out.println(objectMapper.writeValueAsString(data));
}
}
@JsonRootName(value = "root")
class RootData {
public String name1;
public String name2;
public String name3;
}
輸出:
{"root":{"name1":"value1","name2":"value2","name3":"value3"}}
關(guān)注我的微信公眾號(hào):FramePower
我會(huì)不定期發(fā)布相關(guān)技術(shù)積累同诫,歡迎對(duì)技術(shù)有追求粤策、志同道合的朋友加入,一起學(xué)習(xí)成長误窖!