[Jackson API Examples] - @JsonPropertyOrder Example - Order Of Fields

 Hi Guys,

Welcome To Java Inspires.





In this post, we will see how to work with @JsonPropertyOrder annotation from Jackson API.

About @JsonPropertyOrder

com.fasterxml.jackson.annotation.JsonPropertyOrder

Annotation that can be used to define ordering (possibly partial) to use when serializing object properties. Properties included in annotation declaration will be serialized first (in defined order), followed by any properties not included in the definition. Annotation definition will override any implicit orderings (such as guarantee that Creator-properties are serialized before non-creator properties)

Examples:

  // ensure that "id" and "name" are output before other properties
  @JsonPropertyOrder({ "id", "name" })
  // order any properties that don't have explicit setting using alphabetic order
  @JsonPropertyOrder(alphabetic=true)

This annotation may or may not have effect on deserialization: for basic JSON handling there is no effect, but for other supported data types (or structural conventions) there may be.

pom.xml

<project xmlns="http://maven.apache.org/POM/4.0.0"
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
	<modelVersion>4.0.0</modelVersion>
	<groupId>javainspires</groupId>
	<artifactId>jackson-examples</artifactId>
	<version>0.0.1-SNAPSHOT</version>

	<dependencies>
		<!-- https://mvnrepository.com/artifact/com.fasterxml.jackson.core/jackson-databind -->
		<dependency>
			<groupId>com.fasterxml.jackson.core</groupId>
			<artifactId>jackson-databind</artifactId>
			<version>2.12.2</version>
		</dependency>
	</dependencies>
</project>

Serialization Without @JsonPropertyOrder.

MainApp.java
package com.javainspires;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.SerializationFeature;

/**
 * 
 * @author #JavaInspires
 *
 */
public class MainApp {

	public static void main(String[] args) throws JsonProcessingException {

		Customer customer = new Customer();
		customer.setId(101010L);
		customer.setFirstName("Elon");
		customer.setLastName("Musk");
		customer.setAge(45);
		customer.setGender("Male");

		ObjectMapper mapper = new ObjectMapper();
		mapper.enable(SerializationFeature.INDENT_OUTPUT);

		String jsonString = mapper.writeValueAsString(customer);

		System.out.println(jsonString);

	}
}


class Customer {
	Long id;
	String firstName;
	String lastName;
	Integer age;
	String gender;

	public Customer() {
		super();
		// TODO Auto-generated constructor stub
	}

	public Long getId() {
		return id;
	}

	public void setId(Long id) {
		this.id = id;
	}

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

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

	public String getGender() {
		return gender;
	}

	public void setGender(String gender) {
		this.gender = gender;
	}

}


Output:

{
  "id" : 101010,
  "firstName" : "Elon",
  "lastName" : "Musk",
  "age" : 45,
  "gender" : "Male"
}


Serialization Without @JsonPropertyOrder.

MainApp.java
package com.javainspires;

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

/**
 * 
 * @author #JavaInspires
 *
 */
public class MainApp {

	public static void main(String[] args) throws JsonProcessingException {

		Customer customer = new Customer();
		customer.setId(101010L);
		customer.setFirstName("Elon");
		customer.setLastName("Musk");
		customer.setAge(45);
		customer.setGender("Male");

		ObjectMapper mapper = new ObjectMapper();
		mapper.enable(SerializationFeature.INDENT_OUTPUT);

		String jsonString = mapper.writeValueAsString(customer);

		System.out.println(jsonString);

	}
}

@JsonPropertyOrder({ "firstName", "lastName" })
class Customer {
	Long id;
	String firstName;
	String lastName;
	Integer age;
	String gender;

	public Customer() {
		super();
		// TODO Auto-generated constructor stub
	}

	public Long getId() {
		return id;
	}

	public void setId(Long id) {
		this.id = id;
	}

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

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

	public String getGender() {
		return gender;
	}

	public void setGender(String gender) {
		this.gender = gender;
	}

}




Output:

{
  "firstName" : "Elon",
  "lastName" : "Musk",
  "id" : 101010,
  "age" : 45,
  "gender" : "Male"
}


With and Without Annotation Outputs:


// w/o annotation
{
  "id" : 101010,
  "firstName" : "Elon",
  "lastName" : "Musk",
  "age" : 45,
  "gender" : "Male"
}
// with @JsonPropertyOrder({"firstName","lastName"})
{
  "firstName" : "Elon",
  "lastName" : "Musk",
  "id" : 101010,
  "age" : 45,
  "gender" : "Male"
}
// with @JsonPropertyOrder(alphabetic=true)
{
  "age" : 45,
  "firstName" : "Elon",
  "gender" : "Male",
  "id" : 101010,
  "lastName" : "Musk"
}


THANK YOU



Post a Comment

Previous Post Next Post