Close

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

[Last Updated: Aug 11, 2020]

@JsonPropertyOrder annotation is used to specify the ordering of the serialized properties.

Example

POJO

@JsonPropertyOrder({"name", "phoneNumber","email", "salary", "id" })
public class Employee {
  private String id;
  private String name;
  private int salary;
  private String phoneNumber;
  private String email;
    .............
}

Serializing to JSON

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"}

Without @JsonPropertyOrder

If we don't use @JsonPropertyOrder annotation on Employee class, then output will be:

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"}

Alphabetical ordering

@JsonPropertyOrder#alphabetic attribute can be used to order elements alphabetically. Here's the example:

@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 on properties

@JsonPropertyOrder can be used on properties, mostly to support alphabetic ordering of Map entries. If this annotation is also used on class level at the same time, then properties ones will individually override the class one. Here's an example:

@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"}

Example Project

Dependencies and Technologies Used:

  • jackson-databind 2.9.6: General data-binding functionality for Jackson: works on core streaming API.
  • JDK 10
  • Maven 3.5.4

@JsonPropertyOrder Example Select All Download
  • jackson-json-property-order
    • src
      • main
        • java
          • com
            • logicbig
              • example
                • Employee.java

    See Also