@JsonPropertyOrder 定义序列化时属性的顺序

@JsonPropertyOrder 可用于定义序列化时属性的顺序,注解中包含的属性会首先被序列化(按照定义的顺序),然后是未被包含的属性。

用于类级别

@JsonPropertyOrder({"name", "phoneNumber","email", "salary", "id" })
public class Employee {
    private String id;
    private String name;
    private int salary;
    private String phoneNumber;
    private String email;
    ...
}    
public class MainEmployeeSerialization {
    public static void main(String[] args) throws IOException {
        Employee e = new Employee();
        e.setId("1");
        e.setName("Jack");
        e.setSalary(3000);
        e.setPhoneNumber("111-111-111");
        e.setEmail("jack@example.com");
        System.out.println("-- before serialization --");
        System.out.println(e);

        ObjectMapper om = new ObjectMapper();
        String jsonString = om.writeValueAsString(e);
        System.out.println("-- after serialization --");
        System.out.println(jsonString);
    }
}
-- before serialization --
Employee{id='1', name='Jack', salary=3000, phoneNumber='111-111-111', email='jack@example.com'}
-- after serialization --
{"name":"Jack","phoneNumber":"111-111-111","email":"jack@example.com","salary":3000,"id":"1"}

不使用 @JsonPropertyOrder 的输出

-- before serialization --
Employee{id='1', name='Jack', salary=3000, phoneNumber='111-111-111', email='jack@example.com'}
-- after serialization --
{"id":"1","name":"Jack","salary":3000,"phoneNumber":"111-111-111","email":"jack@example.com"}

按字母顺序

@JsonPropertyOrder#alphabetic 可以按照字母顺序排序属性:

@JsonPropertyOrder(alphabetic = true)
public class Employee2 {
    private String id;
    private String name;
    private int salary;
    private String phoneNumber;
    private String email;
    ...
}    
public class MainEmployeeSerialization2 {
    public static void main(String[] args) throws IOException {
        Employee2 e = new Employee2();
        e.setId("1");
        e.setName("Jack");
        e.setSalary(3000);
        e.setPhoneNumber("111-111-111");
        e.setEmail("jack@example.com");
        System.out.println("-- before serialization --");
        System.out.println(e);

        ObjectMapper om = new ObjectMapper();
        String jsonString = om.writeValueAsString(e);
        System.out.println("-- after serialization --");
        System.out.println(jsonString);
    }
}
-- before serialization --
Employee{id='1', name='Jack', salary=3000, phoneNumber='111-111-111', email='jack@example.com'}
-- after serialization --
{"email":"jack@example.com","id":"1","name":"Jack","phoneNumber":"111-111-111","salary":3000}

用于字段级别

@JsonPropertyOrder 可以用于字段,主要用于支持 Map 条目的字母顺序。如果此注释也同时在类级别上使用,则属性将覆盖类级别的。

@JsonPropertyOrder({"otherDetails", "name"})
public class Project {
    private String name;
    @JsonPropertyOrder(alphabetic = true)
    private Map<String, String> otherDetails;
    ...
}    
public class MainProjectSerialization {
    public static void main(String[] args) throws JsonProcessingException {
        Project p = new Project();
        p.setName("Scheduler");
        p.setOtherDetails(Map.of("start-month", "Jan 2019", "supervisor",
                "Jack", "budget", "3000"));

        System.out.println("-- before serialization --");
        System.out.println(p);

        ObjectMapper om = new ObjectMapper();
        String jsonString = om.writeValueAsString(p);
        System.out.println("-- after serialization --");
        System.out.println(jsonString);
    }
}
-- before serialization --
Project{name='Scheduler', otherDetails={supervisor=Jack, start-month=Jan 2019, budget=3000}}
-- after serialization --
{"otherDetails":{"budget":"3000","start-month":"Jan 2019","supervisor":"Jack"},"name":"Scheduler"}

原文链接

Jackson JSON - Using @JsonPropertyOrder annotation to define serialized properties ordering

  • 2
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值