Java利用Jackson轻松处理JSON序列化与反序列化

发布时间 2023-09-19 17:24:35作者: 角刀牛Java

目录
  • 1. @JsonProperty
  • 2. @JsonFormat
  • 3. @JsonIgnore
  • 4. @JsonIgnoreProperties
  • 5. @JsonInclude
  • 6. @JsonTypeInfo 和 @JsonSubTypes
  • 7. @JsonView
  • 8. @JsonNaming
  • 9. @JsonSerialize 和 @JsonDeserialize
  • 10. @JsonAnyGetter 和 @JsonAnySetter
  • 11. @JsonIdentityInfo
  • 总结

1. @JsonProperty

@JsonProperty 注解用于自定义 JSON 属性名称,以及在序列化和反序列化过程中控制属性的包含。通过使用这个注解,您可以确保 JSON 数据与 Java 对象之间正确映射。

  1. public class User {
  2. @JsonProperty("first_name")
  3. private String firstName;
  4. // ...
  5. }

2. @JsonFormat

@JsonFormat 注解用于指定日期、时间和数字的格式。这使得您可以自定义如何在 JSON 中表示这些值。

  1. public class User {
  2. @JsonFormat(shape = JsonFormat.Shape.STRING, pattern = "yyyy-MM-dd")
  3. private LocalDate birthDate;
  4. // ...
  5. }

3. @JsonIgnore

@JsonIgnore 注解用于在序列化和反序列化过程中忽略某个属性。这在处理敏感数据或不需要序列化的字段时非常有用。

  1. public class User {
  2. @JsonIgnore
  3. private String password;
  4. // ...
  5. }

4. @JsonIgnoreProperties

@JsonIgnoreProperties 注解用于在类级别忽略一个或多个属性。这对于在处理来自外部系统的 JSON 数据时忽略未知属性很有用。

  1. @JsonIgnoreProperties({"age", "address"})
  2. public class User {
  3. // ...
  4. }

5. @JsonInclude

@JsonInclude 注解用于指定仅在某些条件下才包含属性。例如,您可以要求仅在属性具有非空值时才包含它。

  1. @JsonInclude(JsonInclude.Include.NON_NULL)
  2. public class User {
  3. private String middleName;
  4. // ...
  5. }

6. @JsonTypeInfo 和 @JsonSubTypes

@JsonTypeInfo@JsonSubTypes 注解用于处理多态类型。这些注解使得在序列化和反序列化时能够正确处理继承关系和类型信息。

  1. @JsonTypeInfo(use = JsonTypeInfo.Id.NAME, include = JsonTypeInfo.As.PROPERTY, property = "type")
  2. @JsonSubTypes({
  3. @JsonSubTypes.Type(value = Manager.class, name = "manager"),
  4. @JsonSubTypes.Type(value = Employee.class, name = "employee")
  5. })
  6. public abstract class Person {
  7. // ...
  8. }

7. @JsonView

@JsonView 注解用于根据视图定义序列化时包含的属性。这允许您根据不同的上下文返回不同的 JSON 结构。

  1. public class User {
  2. @JsonView(Views.Public.class)
  3. private String firstName;
  4. @JsonView(Views.Internal.class)
  5. private String ssn;
  6. // ...
  7. }

8. @JsonNaming

@JsonNaming 注解用于自定义属性名称的命名策略。例如,您可以将所有属性名称自动转换为蛇形命名法。

  1. @JsonNaming(PropertyNamingStrategy.SnakeCaseStrategy.class)
  2. public class User {
  3. private String firstName;
  4. private String lastName;
  5. // ...
  6. }

9. @JsonSerialize 和 @JsonDeserialize

@JsonSerialize@JsonDeserialize 注解用于指定自定义的序列化和反序列化器。这在处理复杂的数据类型或需要定制序列化逻辑的情况下非常有用。

  1. public class User {
  2. @JsonSerialize(using = CustomDateSerializer.class)
  3. @JsonDeserialize(using = CustomDateDeserializer.class)
  4. private LocalDate birthDate;
  5. // ...
  6. }

10. @JsonAnyGetter 和 @JsonAnySetter

@JsonAnyGetter@JsonAnySetter 注解用于处理动态属性。这些注解允许您在序列化和反序列化过程中处理未知或动态生成的属性。

  1. public class User {
  2. private Map<String, Object> properties;
  3.  
  4. @JsonAnyGetter
  5. public Map<String, Object> getProperties() {
  6. return properties;
  7. }
  8.  
  9. @JsonAnySetter
  10. public void setProperty(String key, Object value) {
  11. properties.put(key, value);
  12. }
  13. // ...
  14. }

11. @JsonIdentityInfo

@JsonIdentityInfo 注解用于处理循环引用和重复引用的问题。它可以确保在序列化和反序列化过程中正确处理对象间的关系。

  1. @JsonIdentityInfo(generator = ObjectIdGenerators.PropertyGenerator.class, property = "id")
  2. public class User {
  3. private int id;
  4. private List<User> friends;
  5. // ...
  6. }

总结

Jackson 提供了丰富的注解来处理各种 JSON 序列化和反序列化需求。通过了解这些注解及其用途,您将能够更高效地处理 JSON 数据。请记住,根据您的需求和具体场景选择合适的注解非常重要。希望本文能帮助您更好地理解和使用 Jackson 注解。

 

 

 
原文:https://www.zhangshengrong.com/p/JKN8WYOda6/