@JsonView
可以将对象的属性分成不同的组,这些组称之为 view。view 是开发人员定义的空类/接口。可以根据不同的用途使用不同的 view。可用于序列化和反序列化。
@JsonView
可用于属性上,也可用于类级别上。当用于类级别时,即指定了默认 View,可以用属性级别的注解覆盖类级别的默认 View。
Quick Example:
public interface ContactInfoView {}
public class Employee{
private String name;
@JsonView(ContactInfoView.class)
private String phone;
private String dept;
}
Employee emp = ....
ObjectMapper om = new ObjectMapper();
om.disable(MapperFeature.DEFAULT_VIEW_INCLUSION);
String jsonString = om.writerWithView(ContactInfoView.class).writeValueAsString(emp);
System.out.println(jsonString);
{"phone":"123-123-123"}
Example
Views
public class Views {
public interface QuickContact {}
public interface SummaryView {}
}
POJO
public class Customer {
@JsonView({Views.SummaryView.class, Views.QuickContact.class})
private String name;
@JsonView(Views.SummaryView.class)
private String address;
private String phone;
@JsonView(Views.QuickContact.class)
private String cellPhone;
private String emailAddress;
@JsonView(Views.SummaryView.class)
@JsonFormat(shape = JsonFormat.Shape.STRING, pattern = "yyyy/MM/dd", timezone = "America/Chicago" )
private Date customerSince;
...
}
序列化 QuickContactView
public class MainQuickContactViewSerialization {
public static void main(String[] args) throws IOException {
Customer customer = new Customer();
customer.setName("Emily");
customer.setAddress("642 Buckhannan Avenue Stratford");
customer.setPhone("111-111-111");
customer.setCellPhone("222-222-222");
customer.setCustomerSince(Date.from(ZonedDateTime.now().minusYears(8).toInstant()));
customer.setEmailAddress("emily@example.com");
System.out.println("-- before serialization --");
System.out.println(customer);
ObjectMapper om = new ObjectMapper();
om.disable(MapperFeature.DEFAULT_VIEW_INCLUSION);
String jsonString = om.writerWithView(Views.QuickContact.class).writeValueAsString(customer);
System.out.println("-- after serialization --");
System.out.println(jsonString);
}
}
-- before serialization --
Customer{name='Emily', address='642 Buckhannan Avenue Stratford', phone='111-111-111', cellPhone='222-222-222', emailAddress='emily@example.com', customerSince=Sun Apr 21 20:15:24 CST 2013}
-- after serialization --
{"name":"Emily","cellPhone":"222-222-222"}
序列化 SummaryView
public class MainSummaryViewSerialization {
public static void main(String[] args) throws IOException {
Customer customer = new Customer();
customer.setName("Emily");
customer.setAddress("642 Buckhannan Avenue Stratford");
customer.setPhone("111-111-111");
customer.setCellPhone("222-222-222");
customer.setCustomerSince(Date.from(ZonedDateTime.now().minusYears(8).toInstant()));
customer.setEmailAddress("emily@example.com");
System.out.println("-- before serialization --");
System.out.println(customer);
ObjectMapper om = new ObjectMapper();
om.disable(MapperFeature.DEFAULT_VIEW_INCLUSION);
String jsonString = om.writerWithView(Views.SummaryView.class).writeValueAsString(customer);
System.out.println("-- after serialization --");
System.out.println(jsonString);
}
}
-- before serialization --
Customer{name='Emily', address='642 Buckhannan Avenue Stratford', phone='111-111-111', cellPhone='222-222-222', emailAddress='emily@example.com', customerSince=Sun Apr 21 20:24:20 CST 2013}
-- after serialization --
{"name":"Emily","address":"642 Buckhannan Avenue Stratford","customerSince":"2013/04/21"}
反序列化 SummaryView
public class MainSummaryViewDeserialization {
public static void main(String[] args) throws IOException {
String jsonString = "{\"name\":\"Emily\",\"address\":\"642 Buckhannan Avenue Stratford\","
+ "\"phone\":\"111-111-111\",\"cellPhone\":\"222-222-222\","
+ "\"emailAddress\":\"emily@example.com\",\"customerSince\":\"2010/08/23\"}";
System.out.println("-- before deserialization --");
System.out.println(jsonString);
ObjectMapper om = new ObjectMapper();
om.disable(MapperFeature.DEFAULT_VIEW_INCLUSION);
Customer customer = om.readerWithView(Views.SummaryView.class)
.forType(Customer.class)
.readValue(jsonString);
System.out.println("-- after deserialization --");
System.out.println(customer);
}
}
-- before deserialization --
{"name":"Emily","address":"642 Buckhannan Avenue Stratford","phone":"111-111-111","cellPhone":"222-222-222","emailAddress":"emily@example.com","customerSince":"2010/08/23"}
-- after deserialization --
Customer{name='Emily', address='642 Buckhannan Avenue Stratford', phone='null', cellPhone='null', emailAddress='null', customerSince=Mon Aug 23 13:00:00 CST 2010}
不使用 View
public class MainWithoutViewSerialization {
public static void main(String[] args) throws IOException {
Customer customer = new Customer();
customer.setName("Emily");
customer.setAddress("642 Buckhannan Avenue Stratford");
customer.setPhone("111-111-111");
customer.setCellPhone("222-222-222");
customer.setCustomerSince(Date.from(ZonedDateTime.now().minusYears(8).toInstant()));
customer.setEmailAddress("emily@example.com");
System.out.println("-- before serialization --");
System.out.println(customer);
ObjectMapper om = new ObjectMapper();
String jsonString = om.writeValueAsString(customer);
System.out.println("-- after serialization --");
System.out.println(jsonString);
}
}
-- before serialization --
Customer{name='Emily', address='642 Buckhannan Avenue Stratford', phone='111-111-111', cellPhone='222-222-222', emailAddress='emily@example.com', customerSince=Sun Apr 21 20:28:23 CST 2013}
-- after serialization --
{"name":"Emily","address":"642 Buckhannan Avenue Stratford","phone":"111-111-111","cellPhone":"222-222-222","emailAddress":"emily@example.com","customerSince":"2013/04/21"}