Make Selenium Easy

REST Assured Tutorial 40 – @JsonIgnoreProperties Annotation Of Jackson API – Exclude Field Of Pojo From Serialization Or Deserialization or Both

Last updated on August 5th, 2020 at 09:08 am

Introduction

As a part of End to End REST Assured Tutorial, in this post, We will learn about an important annotation called @JsonIgnoreProperties of Jackson library which helps in restricting properties of a POJO class from serialization or deserialization or both at the class level or property level.

Prerequisite

Supported Posts

You must refer below these prerequisite posts to understand this concept better:-

How Getter & Setter Methods Matter For Serialization Deserialization Using POJO

@JsonIgnore Annotation Of Jackson API – Exclude Field Of Pojo From Serialization And Deserialization

Required Java Library

Since we are using Jackson API of Java for this example, make sure you have the latest dependency of Jackson Databind in your project classpath. I have used below Jackson dependency for this post:-

Limitations of @JsonIgnore

We use @JsonIgnore annotation at an individual property level or its one of the accessors to prevent a particular field from serialization and deserialization process. It has some limitations as below:-

  1. To ignore multiple properties, you need to mark each property with @JsonIgnore that makes it difficult to manage.
  2. If you mention @JsonIgnore at any one place i.e. at a property or its one of accessors then that property will be ignored from serialization and deserialization. We can not control if we want a property to be ignored by serialization or deserialization only.

These limitations can be overcome using @JsonIgnoreProperties.

Introduction of @JsonIgnoreProperties

It is an annotation that can be used to either suppress serialization of properties (during serialization) or ignore the processing of JSON properties read (during deserialization). This can be used at class level as well as at an individual property level like @JsonIgnore.

Let’s see some basic examples to understand it more clear.

Examples

Ignore multiple properties from serialization and deserialization

If we want to ignore multiple properties from both serialization and deserialization process then we can mention all property names at class level as below:-

OR

We will create a POJO class in which some fields will be ignored using above annotation.

Pojo class with @JsonIgnoreProperties
Serialization Code
Output

We have ignored properties “gender” and “fullName” which are not part of JSON output above.

Deserialization Code
Output

Similarly, during deserialization, fields “gender” and “fullName” are ignored and have default values while retrieving.

Allow fields to participate only in serialization

If we have a requirement to allow some fields of a POJO class only for serialization but not for deserialization then that we can achieve this by setting allowGetters as true for @JsonIgnoreProperties annotation. This setting will make fields read-only.

POJO with allowGetters

Note below that to add more than one elements i.e. list of properties to be ignored and value for allowGetters, you need to use with the element name.

Serialization Code
Output

You can see in the above output fields were part of serialization as getters were allowed. You can ask that I said the above fields will be read-only but I can set value by calling setter methods. Read-only means you can not set the value using deserialization. See the next code.

Deserialization code
Output

We have values for fields “Gender” and “Full name” in JSON string but while deserialization they were ignored as setters were not allowed.

Allow fields to participate only in deserialization

If we have a requirement to allow some fields of a POJO class only for deserialization but not for serialization then that we can achieve this by setting allowSetters as true for @JsonIgnoreProperties annotation. This setting will make fields write-only.

POJO with allowSetters
Serialization Code
Output
Deserialization code
Output

Since setters were allowed for ignored fields “gender” and “fullName”, they have participated in the deserialization process but not in serialization.

You can download/clone the above sample project from here.

If you have any doubt, feel free to comment below.
If you like my posts, please like, comment, share and subscribe.
#ThanksForReading
#HappyLearning

Find all Selenium related posts here, all API manual and automation related posts here, and find frequently asked Java Programs here.

Many other topics you can navigate through the menu.

Author: Amod Mahajan

My name is Amod Mahajan and I am an IT employee with 6+ years of experience in Software testing and staying in Bengaluru. My area of interest is Automation testing. I started from basics and went through so many selenium tutorials. Thanks to Mukesh Otwani as his tutorials are easy and cover basics to advance. I have habit of exploring concepts by deep diving. I used to make notes. I thought of sharing my knowledge through posts and now I am here. #KeepLearning #ShareLearning

Leave a Reply

Please wait...

Subscribe to new posts to become automation expert

Want to be notified when my new post is published? Get my posts in your inbox.

REST Assured Tutorial 40 – @JsonIgnoreProperties Annotation Of Jackson API – Exclude Field Of Pojo From Serialization Or Deserialization or Both

by Amod Mahajan time to read: 8 min
0
%d bloggers like this: