Make Selenium Easy

REST Assured Tutorial 42 – Ignore Unknown Properties During Deserialization Using ObjectMapper – Jackson API

Introduction

As a part of End to End REST Assured Tutorial, in this post, We will learn about how unknown properties during deserialization creates a problem and how can we avoid them using ObjectMapper class of Jackson API.

Prerequisite

Ignoring unknown properties using @JsonIgnoreProperties

We can ignore unknown properties during deserialization using an annotation called @JsonIgnoreProperties provided by Jackson. We have covered this topic already. Refer that below:-

Ignore Unknown Properties During Deserialization Using @JsonIgnoreProperties

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:-

Understand the meaning of known and unknown properties

Before we go for an actual topic let’s understand the meaning of words “known” and “unknown” properties. If a property is included in a POJO class or if a property exists in POJO class then it is a known property for that POJO class. A property that is not known to a POJO class is unknown. Let’s see the behavior when a known property is missing during deserialization.

Missing known properties during deserialization

Suppose we have a POJO as below:-

Deserialize with some missing known properties

We will not include all known properties in JSON during deserialization as shown below:-

Output

In JSON string three known properties are missing i.e. age, salary, and married. After deserialization, these fields have a default value. There is no error thrown for those missing values for known properties.

Additional unknown properties during deserialization

In the previous example, we had missing known properties in our JSON. Now we will have some additional unknown properties in JSON and observe behavior during deserialization.

Deserialize with some additional unknown properties

There are some additional unknown fields that are present in JSON string i.e. fullName and eligibleForVote. When we try to deserialize above JSON string we get the output as below:-

Output

As you can see deserialization failed at the encounter of first unknown property “fullName”. It also gives a list of all known properties.

ObjectMapper configuration to ignore unknown properties

In this post, we ignored unknown properties during deserialization using @JsonIgnoreProperties which works at the class level. If you do not have access to POJO class or you are not allowed to edit POJO classes then you can go for ObjectMapper level configuration.

We need to use enum DeserializationFeature to set the value of “FAIL_ON_UNKNOWN_PROPERTIES” as false. There is no need to make any change in the POJO class.

ObjectMapper Configuration Code

Output

You can see how deserialization happened successfully with unknown properties present in the JSON string.

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 42 – Ignore Unknown Properties During Deserialization Using ObjectMapper – Jackson API

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