@JsonView 将属性分组

@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"}

原文链接

  1. Jackson JSON - Using @JsonView Annotation to include/exclude group of properties to be serialized/deserialized
  2. Jackson JSON - Using @JsonView Annotation On Class level to specify default view
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值