Skip to content

Why Do We Need The Jackson API

Use of Jackson API

Jackson API is a high-performance, feature-rich, open-source Java library for processing JSON. It can serialize or map Java objects to JSON and vice-versa. The Jackson Library includes three components: Jackson Databind, Core, and Annotation. Jackson Databind has inner dependencies on Jackson Core and Annotation.

Therefore, including Jackson Databind alone as a Maven dependency will include the other dependencies as well. Jackson contains a set of annotations that can be used to affect the reading and writing of Java objects from JSON.

Maven Dependency

To use Jackson Library Include this dependency in your maven project.

<dependency>
    <groupId>com.fasterxml.jackson.core</groupId>
    <artifactId>jackson-databind</artifactId>
    <version>2.13.1</version>
</dependency>

Jackson Annotations

Jackson library has several annotations, By Using these annotations in our framework we can achieve desired results. Let’s look at some of the popular annotations of Jackson Library.

@JsonProperty

This property is used to map JSON keys with the property name during serialization and deserialization. Suppose we have a variable named FolderId in our POJO class but the request and response objects have a different name as folderId. By default during serialization generated JSON key will map to the POJO field, so in this scenario, the JSON key will not find the suitable POJO field, and the script will fail.

To overcome this situation we can use @JsonProperty(value=”folderId”) to rename the field so that the JSON key will find the correct mapping in the POJO class.

JSON is:

{
    "folderId":0,
    "name": "TestFoldernew",
    "description": "FolderDescription",
}

Corresponding POJO is:

import com.fasterxml.jackson.annotation.JsonProperty;
import com.fasterxml.jackson.annotation.JsonProperty.Access;
public class FolderCreation {
@JsonProperty(value="folderId")	
private Integer FolderId;
private String name;
@JsonProperty(access=Access.READ_ONLY)	
private String description;

public CMSFolderCreation(Integer FolderId,String name,
		String description)

{
	this.FolderId=FolderId;
	this.name=name;
	this.description=description;
}

public Integer getFolderId() {
return FolderId;
}

public void setFolderId(Integer folderId) {
this.FolderId = folderId;
}

public String getName() {
return name;
}

public void setName(String name) {
this.name = name;
}

public String getDescription() {
return description;
}

public void setDescription(String description) {
this.description = description;
}
}

@JsonProperty is useful in various real-world situations while testing APIs. Suppose we are using the same model object for request and response and some fields can only be used in the request and some can only be required in response. To handle this scenario we can utilize @JsonProperty(access=Access.AccessTypes).

Fields that are annotated with @JsonProperty(access=Access.WRITE_ONLY) will only be set for deserialization(JSON to POJO object), and if we use @JsonProperty(access=Access.READ_ONLY) then fields will be read during serialization(POJO to JSON) but not written (set) during deserialization.

@JsonIgnoreProperties

This annotation is used to ignore the list of properties at the class level.

@JsonIgnoreProperties({"name", "description"})
public class FolderCreation {
private Integer FolderId;
private String name;
private String description;

At the time of serialization and deserialization mentioned properties will be ignored. Response JSON will be like {“FolderId”:12}.

This property is very useful in case unknown properties are added to the JSON. Suppose a few new fields were added to the JSON and QA was not notified of the changes, at the time of execution script will throw UnrecognizedPropertyException while parsing JSON because the POJO class does not have those fields.

To overcome this situation we can annotate our class with @JsonIgnoreProperties(ignoreUnknown=true). By using this any unknown property for which we don’t have the corresponding mapping in the POJO will be ignored.

@JsonPropertyOrder

JsonPropertOrder allows the user to order the elements of the object in an orderly fashion as per the requirement. For example, we want certain orders to be followed during serialization and deserialization.

We can achieve this as mentioned below:

@JsonPropertyOrder({"FolderId","description","name"})
public class CMSFolderCreation {

}

Response JSON:

{
     "folderId": 0
    "description": "FolderDescription",
    "name": "TestFolder",
     "contentType":"Text"
}

Payload is in the orderly fashion as annotated by the @JsonpropertyOrder and the fields which were not annotated come in the end.

@JsonInclude

This property is very useful at the time of serialization of the POJO object to JSON. It says that if the property value is null or empty then skip it. For example:

public class User {
    public String firstName = "JANE";
    public String middleName;
    public String lastName = "Doe";
}

Then without @JsonInclude annotation output will be

{"firstName":"Jane", "middleName":null, "lastName":"Doe"}

but if we annotate the middle name field with @JsonInclude(Include.NON_EMPTY) then the response would be much cleaner.

{"firstName":"Jane","lastName":"Doe"}

Jackson API has several other annotations available which can be utilized as per the requirement. It provides a high-level facade to simplify commonly used use cases. It is easy to use and requires a low memory footprint and provides clean and compact JSON results which are easy to understand.

2 thoughts on “Why Do We Need The Jackson API”

Comments are closed.