Tuesday 14 March 2023

Jackson: JsonAlias: use alternative names to a property during deserialization

Using @JsonAlias annotation, we can define one or more alternative names for a property, accepted during deserialization as alternative to the official name. Alias name do not have any impact in serialization process.

 


Example

public class Employee {

	@JsonAlias(value = { "fn", "fName" })
	private String firstName;

}

Either of the json property fn, fName can be used during deserialization.

 

Find the below working application.

 

Employee.java

package com.sample.app.model;

import com.fasterxml.jackson.annotation.JsonAlias;

public class Employee {

	private Integer id;

	@JsonAlias(value = { "fn", "fName" })
	private String firstName;

	@JsonAlias(value = { "ln", "lName" })
	private String lastName;

	public Integer getId() {
		return id;
	}

	public void setId(Integer 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;
	}

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

}

JsonAliasDemo.java

package com.sample.app;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.sample.app.model.Employee;

public class JsonAliasDemo {
	public static void main(String[] args) throws JsonProcessingException {
		ObjectMapper objectMapper = new ObjectMapper();

		Employee emp = new Employee();
		emp.setId(1);
		emp.setFirstName("Krishna");
		emp.setLastName("Gurram");

		String json = objectMapper.writeValueAsString(emp);
		System.out.println(json);

		String jsonInput1 = "{\"id\":1,\"fn\":\"Krishna\",\"ln\":\"Gurram\"}";
		String jsonInput2 = "{\"id\":1,\"fName\":\"Krishna\",\"ln\":\"Gurram\"}";
		String jsonInput3 = "{\"id\":1,\"fn\":\"Krishna\",\"lName\":\"Gurram\"}";
		String jsonInput4 = "{\"id\":1,\"fName\":\"Krishna\",\"lName\":\"Gurram\"}";

		Employee deserializedEmp1 = objectMapper.readValue(jsonInput1, Employee.class);
		Employee deserializedEmp2 = objectMapper.readValue(jsonInput2, Employee.class);
		Employee deserializedEmp3 = objectMapper.readValue(jsonInput3, Employee.class);
		Employee deserializedEmp4 = objectMapper.readValue(jsonInput4, Employee.class);

		System.out.println(deserializedEmp1);
		System.out.println(deserializedEmp2);
		System.out.println(deserializedEmp3);
		System.out.println(deserializedEmp4);
	}
}

Output

{"id":1,"firstName":"Krishna","lastName":"Gurram"}
Employee [id=1, firstName=Krishna, lastName=Gurram]
Employee [id=1, firstName=Krishna, lastName=Gurram]
Employee [id=1, firstName=Krishna, lastName=Gurram]
Employee [id=1, firstName=Krishna, lastName=Gurram]


 

Previous                                                 Next                                                 Home

No comments:

Post a Comment