@JsonCreator Custom Deserialization Function-JSON Framework Jackson Elaboration Part 5

Jackson is the default JSON data processing framework of Spring Boot (SpringBoot), but it does not depend on any Spring library. Some friends think that Jackson can only be used in the Spring framework, but it is not. There is no such restriction. It provides many JSON data processing methods and annotations , as well as functions such as streaming API, tree model, data binding , and complex data type conversion. Although it is simple and easy to use, it is definitely not a small toy. For more content, I will write a series of 5-10 articles. Please continue to pay attention to me.

This section continues to introduce how to use @JsonCreatorannotations and @ConstructorPropertiesannotations to customize the deserialization function in the JSON deserialization process .

1. What did Jackson's deserialization process do?

For Jackson's JSON data format, the deserialization process is to deserialize the JSON string into a java object .

ObjectMapper mapper = new ObjectMapper();

//将JSON字符串反序列化为java对象
String jsonInString = "{\"name\":\"乔丹\",\"age\":45,\"hobbies\":[\"高尔夫球\",\"棒球\"]}";
PlayerStar3 jordan = mapper.readValue(jsonInString, PlayerStar3.class);

System.out.println(jordan);

By default, which functions Jackson calls during the deserialization process, let me introduce to you

  • First call the parameterless constructor of the deserialized target class PlayerStar3 to construct a java object
  • Then call the set method of the member variable of the class to assign a value to each member variable of the object.

So by default, a Java class uses Jackson for deserialization, it must have a public parameterless constructor (it is not written in java and has a default), and it must have a set method for member variables.

2. @JsonCreatorAnnotation

By default, Jackson's deserialization process is as described above, using parameterless constructors and set methods. In addition, we can also use @JsonCreatorannotations to customize the deserialization process. In our custom deserialization function, we have greater flexibility and can complete more non-specified actions . There are two custom deserialization channels:

  • @JsonCreatorAnnotations are added to the construction method
  • @JsonCreatorAnnotations are added to the factory static method

Used @JsonCreatorafter the annotation, the annotation will be marked using the method deserialized constructed object, using the default constructor with no arguments and methods set deserialization process failure.

2.1. @JsonCreatorAnnotations are added to the construction method

The JSON string corresponding to this PlayerStar3 is jsonInString in the first section. In the following constructor, which attribute values ​​you want to assign to the member variables of the java object, you use @JsonProperty("salary")to define it.

public class PlayerStar3 {

  private String name;
  private Integer age;
  private String[] hobbies;    //业余爱好,数组
  private List<String> friends;   //  朋友
  private Map<String, BigDecimal> salary; //年收入 Map

  //这段是我们的核心代码,使用JsonCreator注解说明该方法是反序列化构造方法。
  @JsonCreator
  public PlayerStar3(@JsonProperty("name") String name,
                     @JsonProperty("age") Integer age,
                     @JsonProperty("hobbies") String[] hobbies,
                     @JsonProperty("friends") List<String> friends,
                     @JsonProperty("salary") Map<String, BigDecimal> salary) {
    this.name = name;
    this.age = age;
    this.hobbies = hobbies;
    this.friends = friends;
    this.salary = salary;
  }

  //这里省略一个toString()方法
}

We use the deserialization code in the first section to deserialize jsonInString to construct a PlayerStar3 object. The console output is as follows (the object’s toString() method output):

PlayerStar3{name='乔丹', age=45, hobbies=[高尔夫球, 棒球], friends=null, salary=null}

In @JsonCreatorthe method of labeling the breakpoint configuration which, indeed, that the use of the java object deserialization process is the process configuration.

2.2. @JsonCreatorAnnotations are added to the factory static method

In addition to @JsonCreatoradding to the construction method, you can also use static factory functions to deserialize and construct java objects. The method of use is as follows:

public class PlayerStar3 {

  //  省略若干成员变量的定义,和上文一样

  @JsonCreator
  public static PlayerStar3 createPlayer(@JsonProperty("name") String name,
                                         @JsonProperty("age") Integer age,
                                         @JsonProperty("hobbies") String[] hobbies,
                                         @JsonProperty("friends") List<String> friends,
                                         @JsonProperty("salary") Map<String, BigDecimal> salary) {
    PlayerStar3 player = new PlayerStar3();  //new 一个java对象
    player.name = name;    //赋值
    player.age = age;
    player.hobbies = hobbies;
    player.friends = friends;
    player.salary = salary;
    return player;
  }
    //这里省略一个toString()方法
}

Use @JsonCreatorannotations added to the static factory method, you can achieve the same effect deserialization.

Three, @ConstructorPropertiesannotations

@ConstructorPropertiesThe role of the @JsonCreatorannotation is the same as that of the annotation, but it can only be added to the construction method as a deserialization function. But its grammar is more concise and it is more convenient to use, without @JsonPropertyannotations. It also provides flexibility. We can do more non-specified operations on the object during the deserialization process of the construction method.

@ConstructorProperties({"name", "age","hobbies", "friends","salary"})
public PlayerStar3(String name,
                   Integer age,
                   String[] hobbies,
                   List<String> friends,
                   Map<String, BigDecimal> salary) {
  this.name = name;
  this.age = age;
  this.hobbies = hobbies;
  this.friends = friends;
  this.salary = salary;
}

Use @ConstructorPropertiesannotations added in construction methods to achieve the same effect deserialization.

Welcome to follow my blog, there are many boutique collections

  • This article is reproduced indicate the source (en must not turn only the text): letters Gebo off .

If you think it is helpful to you, please like and share it for me! Your support is my inexhaustible creative motivation! . In addition, the author has output the following high-quality content recently, and I look forward to your attention.

Guess you like

Origin blog.csdn.net/hanxiaotongtong/article/details/108745009