Attribute serialization customization and alphabet sorting-JSON framework Jackson Elaboration Part 3

Jackson is the default JSON data processing framework of Spring Boot, 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.

In the usual JSON data serialization process, there is often a need to serialize attributes in a certain order. This article introduces how to sort the JSON serialized attributes. You can customize the order or sort in alphabetical order.

One, @JsonPropertyOrderattribute sorting

Specify the property serialization order of each member variable in the java POJO through the JsonPropertyOrder annotation.

@Data
@JsonPropertyOrder({"salary","name","age","hobbies","friends"})
public class PlayerStar {

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

The final Java POJO object is serialized into the following string, and the properties are serialized in the order defined by the JsonPropertyOrder annotation ("salary", "name", "age", "hobbies", "friends"). If you do not use JsonPropertyOrder to specify the order, the default is to serialize according to the declaration order of the Java class member variables.

{
  "salary" : {
    "2000" : 10000000,
    "2010" : 62000000,
    "2020" : 112400000
  },
  "name" : "乔丹",
  "age" : 45,
  "hobbies" : [ "高尔夫球", "棒球" ],
  "friends" : [ "kobe", "curry", "james" ]
}

You can use the following code to serialize the PlayerStar object into a string

  //getInstance是一个为PlayerStar各属性赋值的初始化方法,可以参考本系列文章第一节:基础数据准备
  PlayerStar jordan = PlayerStar.getInstance();

  //ObjectMapper作为Jackson的API工具类存在
  ObjectMapper mapper = new ObjectMapper();
  String jsonInString2 = mapper.writerWithDefaultPrettyPrinter().writeValueAsString(jordan);
  System.out.println(jsonInString2);

Second, alphabetical order

Use to @JsonPropertyOrder(alphabetic = true)define the serialization order of Java class member variables

@JsonPropertyOrder(alphabetic = true)
public class PlayerStar {
     //成员变量定义和上文一致
}

In the English alphabet, age (beginning with a)> friends (beginning with f) -> hobbies (beginning with h), and so on, so the final JSON serialization result is as follows:

{
  "age" : 45,
  "friends" : [ "kobe", "curry", "james" ],
  "hobbies" : [ "高尔夫球", "棒球" ],
  "name" : "乔丹",
  "salary" : {
    "2000" : 10000000,
    "2010" : 62000000,
    "2020" : 112400000
  }
}

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/108702143