Home » Jackson Serialization order

Jackson Serialization order

Jackson Serialization order of properties

1. Overview

In this article, we will learn to control the serialization order of the object properties in Jackson. To know more about the Jackson library, refer to our articles.

2. Jackson serialization order of object properties

By default, the ordering of the fields in the serialized JSON depends on the JDK. It may be the declaration order but not guaranteed. However, you can control the serialization order in Jackson. It may be helpful in scenarios such as where you want to use a different order in your JSON.

For example, consider the below Student class.

public class Student {

	private long id;
	private String firstName;
	private long marks;
	private String lastName;

	public Student(long id, String firstName, String lastName, long marks) {
		this.id = id;
		this.firstName = firstName;
		this.lastName = lastName;
		this.marks = marks;
	}
	
	public Student() {
	}

	public long getId() {
		return id;
	}

	public String getFirstName() {
		return firstName;
	}

	public String getLastName() {
		return lastName;
	}

	public long getMarks() {
		return marks;
	}

	@Override
	public String toString() {
		return "Student [id=" + id + ", firstName=" + firstName + ", lastName=" + lastName + "]";
	}

}

The below code contains the ObjectMapper which converts Student java object to JSON string.

@Test
void serialization_default() throws JsonMappingException, JsonProcessingException {
	ObjectMapper mapper = new ObjectMapper();
	Student student = new Student(121, "Mike", "Jack", 99);

	String studentStr = mapper.writeValueAsString(student);
	System.out.println(studentStr);
}

The output JSON string is as follows:

{"id":121,"firstName":"Mike","lastName":"Jack","marks":99}

The fields in the serialized JSON are exactly in the same order as the fields of the Student class. What if you want to alter the order of properties in the serialized JSON? Let’s see the various solutions to control it.

2.1. Use @JsonPropertyOrder at class level for serialization order

2.1.1. Explicit property setting

You can use the @JsonPropertyOrder annotation at the class level to specify the order of serialization. Jackson serializes the properties in the defined order, followed by any other properties not included in the annotation definition.

For example, we have defined the lastName and firstName properties in the @JsonPropertyOrder annotation. So Jackson first serializes the lastName and firstName. Then, it serializes the remaining properties id and marks.

@JsonPropertyOrder({"lastName", "firstName"})
public class Student {
@Test
void serialization_default() throws JsonMappingException, JsonProcessingException {
	ObjectMapper mapper = new ObjectMapper();
	Student student = new Student(121, "Mike", "Jack", 99);
	String studentStr = mapper.writeValueAsString(student);

	System.out.println(studentStr);
}

If you execute the above test case now, the following result appears on the console.

{"lastName":"Jack","firstName":"Mike","id":121,"marks":99}

2.1.2. Alphabetic serialization order

If you want to follow alphabetic order, then you can specify the alphabetic property true.

@JsonPropertyOrder(alphabetic = true)
public class Student {

Jackson serializes the fields in alphabetical order. So running the test case again results in the following JSON where all the fields are in alphabetical order:

{"firstName":"Mike","id":121,"lastName":"Jack","marks":99}

2.2. Configure ObjectMapper globally

The above solution helps to specify the serialization order of field properties at the class level. However, you can configure the ObjectMapper globally.

ObjectMapper.configure(MapperFeature.SORT_PROPERTIES_ALPHABETICALLY, true)
or
ObjectMapper.enable(MapperFeature.SORT_PROPERTIES_ALPHABETICALLY)
{"firstName":"Mike","id":121,"lastName":"Jack","marks":99}

This would also result in alphabetical order.

2.3. Configure SpringBoot properties globally

Alternatively, you can configure the following property in your application.properties file.

/* json */
spring.jackson.mapper.sort_properties_alphabetically=true

/* yml */
spring:
  jackson:
    mapper:
      SORT_PROPERTIES_ALPHABETICALLY: true

2.4. Configure Jackson2ObjectMapperBuilder globally

You can configure the Jackson2ObjectMapperBuilder to sort properties alphabetically. However, spring-web dependency should be in your dependency path and added in 4.1.1.

@Bean
public Jackson2ObjectMapperBuilder objectMapperBuilder() {
      Jackson2ObjectMapperBuilder builder = new Jackson2ObjectMapperBuilder();
      builder.featuresToEnable(MapperFeature.SORT_PROPERTIES_ALPHABETICALLY);

      return builder;
}

2.5. Use @JsonProperty at field level

You can use @JsonProperty to declare the order at the field level. This @JsonProperty shows the numerical index of this property.

For example, we have defined the index for each property in the Student class. We defined the index order as marks, id, firstName, and lastName.

@JsonProperty(index=2)
private long id;

@JsonProperty(index=3)
private String firstName;

@JsonProperty(index=1)
private long marks;

@JsonProperty(index=4)
private String lastName;

So running the test case again results in the order of the numerical index:

{"marks":99,"id":121,"firstName":"Mike","lastName":"Jack"}

3. Conclusion

To sum up, we have seen the various possible ways to control the serialization order of the properties in Jackson.

Leave a Reply

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