Deserialize json with Java parameterized constructor
Tags: blogs, jackson, java, json
Published: 2019-07-24 07:08:45

---
title: Deserialize json with Java parameterized constructor
published: true
tags: blogs,JACKSON,JAVA,JSON
canonical_url: https://jsblogs.github.io/blogs/2016/09/12/Deserialize-json-with-Java-parameterized-constructor
---

Hi Guys in my previous blog [JSON deserialize generic types using Gson and Jackson](/blogs/2016/09/07/JSON-deserialize-generic-types-using-Gson-and-Jackson) I talked about how you can deserialize json in Java Generics. Now in this blog post we are going to learn how to deserialize json into Java class which doesn’t have default constructor. For this blog I am using Jackson library.

Almost all frameworks requires default/no-argument constructor in your class. Because these frameworks use [reflection](https://docs.oracle.com/javase/tutorial/reflect/) to create objects by invoking default constructor. But if there is no default constructor present in class then its hard to instantiate using reflection. Let’s assume we have a class with no-args constructor (only a parameterized constructor present) and we want to deserialize it. 
{% gist https://gist.github.com/jeetmp3/7eff6ff303b58894715878833c381f24 file=UserProfile.java %}

And a json file {% gist https://gist.github.com/jeetmp3/7eff6ff303b58894715878833c381f24 file=UserProfile.json %}

There are two ways in Jackson to deserialize this type of classes.

1. Custom deserializer
2. Mixin Annotations

### 1. Custom Deserializer

In Custom Deserializer you will create a class and extend it with `com.fasterxml.jackson.databind.JsonDeserializer` and override its abstract method `deserialize()`. This class gives you full control, you’ll get json in `com.fasterxml.jackson.core.JsonParser`. Now you can map json properties with class properties. In my case I am creating `UserProfileDeserializer`. {% gist https://gist.github.com/jeetmp3/7eff6ff303b58894715878833c381f24 file=UserProfileDeserializer.java %}

Now we need to register above deserializer in `com.fasterxml.jackson.databind.ObjectMapper`. So that, while deserializing UserProfile class it use `UserProfileDeserializer`. {% gist https://gist.github.com/jeetmp3/7eff6ff303b58894715878833c381f24 file=UserProfileDeserializerDemo.java %}

> In above code we are adding our Deserializer in `com.fasterxml.jackson.databind.module.SimpleModule` and that module is registered in `com.fasterxml.jackson.databind.ObjectMapper`. Similarly, you can register deserializers for other classes too.

### 2. Mixin Annotations

Jackson provides another way of doing this by using _ **Mixin Annotations** _. You can read about Jackson Mixin [here](https://github.com/FasterXML/jackson-docs/wiki/JacksonMixInAnnotations). In our class `UserProfile` there is no default constructor, it has a parameterized constructor. Let’s checkout how to create mixin for our class. {% gist https://gist.github.com/jeetmp3/7eff6ff303b58894715878833c381f24 file=UserProfileMixin.java %}

That’s it !!

> We have created a constructor same as in `UserProfile` class and marked it with `com.fasterxml.jackson.annotation.JsonCreator` annotation. This will tell `com.fasterxml.jackson.databind.ObjectMapper` this is how target class (`UserProfile`) constructor looks and constructor parameters are marked with `com.fasterxml.jackson.annotation.JsonProperty` annotation.

Now checkout how to register this mixin class in `com.fasterxml.jackson.databind.ObjectMapper` {% gist https://gist.github.com/jeetmp3/7eff6ff303b58894715878833c381f24 file=UserProfileMixinDemo.java %}

Enjoy !!!! ☺☺