Jackson JSON: using @JsonPropertyOrder annotation to define properties serialization order

In the comments to a previous article a blog reader asked support for how to customize the order in which the properties of an object appear in the string resulting from its JSON serialization, using the Jackson library. In particular, in his case, he wanted to change the default order applied to the serialization of an object of a subclass that extends another class. The method we’ll see can be used, in general, when you need to define a specific order for properties serialization.

For an introduction to the Jackson library and its use you can refer to the article “JSON serialization and deserialization of JAVA objects with Jackson: a concrete example

We start to set up the scenario described by the blog reader, creating two classes ClassA and ClassB with the second one that inherits from the first. For our example we’ll use Eclipse as IDE and we’ll create a Maven project that will allow us to easily add Jackson libraries as dependencies.

Once the project is created, we add the “pojo” package in which we create the two classes:
Jackson JsonOrderProperty example POJO classes to serialize

The code of the two Java classes will be as follows:

public class ClassB {

	private String property1;
	private String property2;
	private String property3;

	public ClassB() {
		this.property1 = "Davis";
		this.property2 = "Molinari";
		this.property3 = "www.davismol.net";
	}
	
	public String getProperty1() {
		return property1;
	}
	public void setProperty1(String property1) {
		this.property1 = property1;
	}
	public String getProperty2() {
		return property2;
	}
	public void setProperty2(String property2) {
		this.property2 = property2;
	}
	public String getProperty3() {
		return property3;
	}
	public void setProperty3(String property3) {
		this.property3 = property3;
	}
}
public class ClassA extends ClassB {
	
	private int id;
	private String name;
		
	public ClassA( int id, String name) {
		super();
		this.id = id;
		this.name = name;
	}
	
	public int getId() {
		return id;
	}
	public void setId(int id) {
		this.id = id;
	}
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	
	
}

Once we have created the two classes for objects we’ll serialize, let’s add in the Maven pom.xml file the dependencies required to use the Jackson functionalities we need for this example.
In particular, we add jackson-core and jackson-databind:

	
		
			com.fasterxml.jackson.core
			jackson-core
			2.8.4
		
		
			com.fasterxml.jackson.core
			jackson-databind
			2.8.4
		
	

Jackson JsonOrderProperty example Maven dependencies
At this point we define a class that will contain the main method in which we will create an instance of the child class ClassA that inherits from ClassB and where we will serialize object, printing out the result in the output console. We call this class JSONOrderExample and its content will be as follows:

import java.io.IOException;

import com.fasterxml.jackson.core.JsonGenerationException;
import com.fasterxml.jackson.databind.JsonMappingException;
import com.fasterxml.jackson.databind.ObjectMapper;

import pojo.ClassA;

public class JSONOrderExample {

	public static void main(String[] args) {

        ObjectMapper mapper = new ObjectMapper();

        ClassA ca = new ClassA(1, "User1");

        try {
            mapper.writeValue(System.out, ca);
        }
        catch (JsonGenerationException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        catch (JsonMappingException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }
}

Now we can run the application. The result we get, as expected, is the following:

{"property1":"Davis","property2":"Molinari","property3":"www.davismol.net","id":1,"name":"User1"}

As we can see the properties are serialized with first those of the parent class followed by those of the subclass that inherits from it.
Jackson JsonOrderProperty example default result
Now our goal, as requested by the blog reader in his comment is to modify this default order and to establish a custom order so that properties of the subclass appear before those of the base class.

To do this we must use an annotation provided by Jackson:@JsonPropertyOrder

@JsonPropertyOrder is an annotation to be used at class level. It takes as property a list of fields that defines the order in which fields will appear in the string resulting from the object JSON serialization.

In our case, to get the result requested by the blog reader, we must annotate ClassA as follows:

@JsonPropertyOrder({ "id", "name", "property1", "property2", "property3" })

The complete code of the class is then::

import com.fasterxml.jackson.annotation.JsonPropertyOrder;

@JsonPropertyOrder({ "id", "name", "property1", "property2", "property3" })
public class ClassA extends ClassB {
	
	private int id;
	private String name;
		
	public ClassA( int id, String name) {
		super();
		this.id = id;
		this.name = name;
	}
	
	// GETTERS e SETTERS OMITTED
}

Jackson JsonOrderProperty example class annotation

Let’s try to run again our application. The result we get in this case is the following:

{"id":1,"name":"User1","property1":"Davis","property2":"Molinari","property3":"www.davismol.net"}

Jackson JsonOrderProperty example result with ordered properties
As we can see the result is exactly what we want; the “id” and the “name” properties of the ClassA subclass in fact appear before the properties of the base class ClassB in the string resulting from the object JSON serialization.

See also:

The complete code of this example can be downloaded here:

This entry was posted in $1$s. Bookmark the permalink.

Leave a Reply

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