类注解
Properties(属性处理)
Rename(属性重命名)
public class Name {
@JsonProperty("firstName")
public String _first_name;
}
在将 Name 实体类转化为 JSON 的时候,就会变成:
{ "firstName": "Bob" }
Ignore(属性忽略)
public class Value {
public int value;
@JsonIgnore
public int internalValue;
}
最终生成的 JSON 是如下格式:
{ "value": 42 }
也可以在类的头部统一声明:
@JsonIgnoreProperties({ "extra", "uselessValue" })
public class Value {
public int value;
}
那么如下的 JSON 字符串也是可以被转化为该实体类的:
{ "value": 42, "extra": "fluffy", "uselessValue": -13 }
对于意外地未知属性,也可以统一忽略:
@JsonIgnoreProperties(ignoreUnknown = true)
public class PojoWithAny {
public int value;
}
@JsonView(动态控制展示的成员变量)
首先定义一个简单的 View 控制类:
public class Views {
public static class Normal {}
public static class Manager extends Normal {}
}
在下面的代码实现中,如果是选择了 Normal View,那么 salary 属性将会被隐藏,而在 Manager View 状态下,任何属性都会被展示。
public class Staff {
@JsonView(Views.Normal.class)
private String name;
@JsonView(Views.Normal.class)
private int age;
@JsonView(Views.Normal.class)
private String position;
@JsonView(Views.Manager.class)
private BigDecimal salary;
@JsonView(Views.Normal.class)
private List<String> skills;
在进行 Object 转化为 JSON 的过程中,进行视图控制:
public class Jackson2Example {
public static void main(String[] args) {
Jackson2Example obj = new Jackson2Example();
obj.run();
}
private void run() {
ObjectMapper mapper = new ObjectMapper();
Staff staff = createDummyObject();
try {
// Salary will be hidden
System.out.println("Normal View");
String normalView = mapper
.writerWithView(Views.Normal.class)
.writeValueAsString(staff);
System.out.println(normalView);
String jsonInString =
"{\"name\":\"mkyong\",\"age\":33,\"position\":\"Developer\",\"salary\":7500,\"skills\":[\"java\",\"python\"]}";
Staff normalStaff = mapper
.readerWithView(Views.Normal.class)
.forType(Staff.class)
.readValue(jsonInString);
System.out.println(normalStaff);
// Display everything
System.out.println("\nManager View");
String managerView = mapper
.writerWithView(Views.Manager.class)
.writeValueAsString(staff);
System.out.println(managerView);
Staff managerStaff = mapper
.readerWithView(Views.Manager.class)
.forType(Staff.class)
.readValue(jsonInString);
System.out.println(managerStaff);
} catch (JsonGenerationException e) {
e.printStackTrace();
} catch (JsonMappingException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
private Staff createDummyObject() {
Staff staff = new Staff();
staff.setName("mkyong");
staff.setAge(33);
staff.setPosition("Developer");
staff.setSalary(new BigDecimal("7500"));
List<String> skills = new ArrayList<>();
skills.add("java");
skills.add("python");
staff.setSkills(skills);
return staff;
}
}
最终输出的结果为:
Normal View
{"name":"mkyong","age":33,"position":"Developer","skills":["java","python"]}
Staff [name=mkyong, age=33, position=Developer, salary=null, skills=[java, python]]
Manager View
{"name":"mkyong","age":33,"position":"Developer","salary":7500,"skills":["java","python"]}
Staff [name=mkyong, age=33, position=Developer, salary=7500, skills=[java, python]]