@JsonPropertyOrder Jackson Annotation

@JsonPropertyOrder annotation defines the order of properties while serializing an object. In this post, we will be discussing the @JsonPropertyOrder annotation in detail.

So, where would you need to use the @JsonPropertyOrder annotation? This annotation will be helpful in the scenario where we want a particular order of the fields. Say we have three fields – name, age, and height and we want them in the same order. How can we ensure that they will be in some particular order while serializing the object? This is where @JsonPropertyOrder comes into the picture.

@JsonPropertyOrder({"name","age","height"})
public class Person

And after the above code, the JSON keys will be shown in the same order.

{
  "name": "Codekru",
  "age": 20,
  "height": 170
}

Here is an example of @JsonPropertyOrder :

 import com.fasterxml.jackson.annotation.JsonPropertyOrder;
 import com.fasterxml.jackson.core.JsonProcessingException;
 import com.fasterxml.jackson.databind.ObjectMapper;

 @JsonPropertyOrder({ "lastName", "age", "firstName" })
 public class Person {

    private String firstName;
    private String lastName;
    private int age;

    public Person() {
        super();
    }

    public Person(String firstName, String lastName, int age) {
        super();
        this.firstName = firstName;
        this.lastName = lastName;
        this.age = age;
    }

    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 int getAge() {
        return age;
    }

    public void setAge(int age) {
        this.age = age;
    }

    public static void main(String[] args) throws JsonProcessingException {
        Person person = new Person();
        person.setFirstName("Codekru");
        person.setLastName("Startup");
        person.setAge(1);
        ObjectMapper mapper = new ObjectMapper();
        System.out.println(mapper.writerWithDefaultPrettyPrinter().writeValueAsString(person));// serializing object
    }
 }

In this example, we have used @JsonPropertyOrder annotation to define property serialization order: lastName, age, firstName.

Output –

{
  "lastName" : "Startup",
  "age" : 1,
  "firstName" : "Codekru"
}

As we can see, JSON property output is as per JsonPropertyOrder annotation.

Now, what If we defined the order of only three properties, but there were more than three fields or properties present in the class?

Only the fields mentioned in the @JsonPropertyOrder annotation will be in order, and others don’t.

import com.fasterxml.jackson.annotation.JsonPropertyOrder;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;

@JsonPropertyOrder({ "lastName", "age", "firstName" })
public class Person {

	private String firstName;
	private String lastName;
	private int age;
	private int height;
	private String hobby;

	public Person() {
		super();
	}

	public Person(String firstName, String lastName, int age) {
		super();
		this.firstName = firstName;
		this.lastName = lastName;
		this.age = age;
	}

	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 int getAge() {
		return age;
	}

	public void setAge(int age) {
		this.age = age;
	}

	public String getHobby() {
		return hobby;
	}

	public void setHobby(String hobby) {
		this.hobby = hobby;
	}

	public int getHeight() {
		return height;
	}

	public void setHeight(int height) {
		this.height = height;
	}

	public static void main(String[] args) throws JsonProcessingException {
		Person person = new Person();
		person.setFirstName("Codekru");
		person.setLastName("Startup");
		person.setAge(1);
		person.setHobby("coding");
		person.setHeight(170);
		ObjectMapper mapper = new ObjectMapper();
		System.out.println(mapper.writerWithDefaultPrettyPrinter().writeValueAsString(person));// serializing object
	}
}

Output –

{
  "lastName" : "Startup",
  "age" : 1,
  "firstName" : "Codekru",
  "height" : 170,
  "hobby" : "coding"
}

Here only the fields ( lastName, age, firstName) defined in the annotation has an order, but the remaining two will not and can be appeared in any order.

@JsonPropertyOrder – alphabetic option

@JsonPropertyOrder annotation has an attribute named alphabetic, whose default value is false. If sets to true, then the properties that are not part of the @JsonPropertyOrder annotation will be serialized in alphabetical order.

 @JsonPropertyOrder(value = { "lastName" }, alphabetic = true)
 public class Person {

    private String firstName;
    private String lastName;
    private int age;

    public Person() {
        super();
    }

    public Person(String firstName, String lastName, int age) {
        super();
        this.firstName = firstName;
        this.lastName = lastName;
        this.age = age;
    }

    // Getter - Setters

    public static void main(String[] args) throws JsonProcessingException {
        Person person = new Person();
        person.setFirstName("Codekru");
        person.setLastName("Startup");
        person.setAge(1);
        ObjectMapper mapper = new ObjectMapper();
        System.out.println(mapper.writerWithDefaultPrettyPrinter().writeValueAsString(person));// serializing object
    }
}

Output:

{
  "lastName" : "Startup",
  "age" : 1,
  "firstName" : "Codekru"
}

As we can see, lastName has appeared at the 1st position in JSON output, and the remaining two properties are in alphabetical order.

References :
https://fasterxml.github.io/jackson-annotations/javadoc/2.6/com/fasterxml/jackson/annotation/JsonPropertyOrder.html

We hope that you have liked the article. If you have any doubts or concerns, please feel free to write us in the comments or mail us at admin@codekru.com.

Other Jackson Annotations –

Liked the article? Share this on

Leave a Comment

Your email address will not be published. Required fields are marked *