Home > Java > javaTutorial > body text

How to serialize the order of properties using Jackson library in Java?

PHPz
Release: 2023-08-28 12:45:06
forward
1488 people have browsed it

How to serialize the order of properties using Jackson library in Java?

@JsonPropertyOrder is a annotation used at the class level . It takes as an attribute a list of fields that defines the order in which the fields appear in the string generated by JSON serialization of the object. Properties included in the annotation declaration can be serialized first (in the order they are defined), followed by any properties not included in the definition.

Syntax

public @interface JsonPropertyOrder
Copy after login

Example

import com.fasterxml.jackson.core.*;
import com.fasterxml.jackson.databind.*;
import com.fasterxml.jackson.annotation.JsonPropertyOrder;
import java.util.*;
import java.io.*;
public class JsonPropertyOrderTest {
   public static void main(String args[]) throws JsonGenerationException, JsonMappingException,        IOException {
      Employee emp = new Employee();
      emp.setFirstName("Adithya");
      emp.setEmpId(25);
      emp.setLastName("Jai");
      emp.getTechnologies().add("Java");
      emp.getTechnologies().add("Scala");
      emp.getTechnologies().add("Python");
      ObjectMapper mapper = new ObjectMapper();
      mapper.writerWithDefaultPrettyPrinter().writeValue(System.out, emp);
   }
}
// Employee class
@JsonPropertyOrder({
   "firstName",
   "lastName",
   "technologies",
   "empId"
})
class Employee {
   private int empId;
   private String firstName;
   private String lastName;
   private List<String> technologies = new ArrayList<>();
   public int getEmpId() {
      return empId;
   }
   public void setEmpId(int empId) {
      this.empId = empId;
   }
   public String getFirstName() {
      return firstName;
   }
   public void setFirstName(String firstName) {
      this.firstName = firstName;
   }
   public String getLastName() {
      return lastName;
   }
   public void setLastName(String lastName) {
      this.lastName = lastName;
   }
   public List<String> getTechnologies() {
      return technologies;
   }
   public void setTechnologies(List<String> technologies) {
      this.technologies = technologies;
   }
}
Copy after login

Output

{
   "firstName" : "Adithya",
   "lastName" : "Jai",
   "technologies" : [ "Java", "Scala", "Python" ],
   "empId" : 125
}
Copy after login

The above is the detailed content of How to serialize the order of properties using Jackson library in Java?. For more information, please follow other related articles on the PHP Chinese website!

source:tutorialspoint.com
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template