Javatpoint Logo
Javatpoint Logo

Deserialization Annotations in Jackson

Deserialization annotations are used when we deserialize JSON string into an Object. Jackson library provides several deserialization annotations such as @JsonCreator, @JacksonInject, @JsonAnySetter, etc. These annotations are mostly used in setter

Deserialization Annotations in Jackson

Let's understand each one of them one by one with an example.

@JsonCreator

@JsonCreator annotation is one of the most used deserialization annotations used to fine-tune the factory method or constructor, i.e., used in deserialization. We also use @JsonProperty annotation to use @JsonCreator annotation.

Let's take an example to understand how we use it with @JsonProperty annotation.

JsonCreatorExample.java

Output:

Deserialization Annotations in Jackson

@JacksonInject

Another important deserialization annotation is used when we inject property value instead of parsing it from Json input.

Let's seean example in which we will use @JacksonInject annotation to insert a value into an object rather than parsing from the Json.

JacksonInjectExample.java

Output:

Deserialization Annotations in Jackson

@JsonAnySetter

@JsonAnySetter is another important deserialization annotation that we use at the time of deserializing the Json. It is similar to @JsonAnyGetter annotation.

The only difference between both of them is that @JsonAnySetter allows the setter method for returning a Map which we used for deserializing, whereas @JsonAnyGetter allows a getter method for the same purpose, but the returned map is used for serializing. The returned map is used for deserializing JSON's additional properties in the same way as other properties.

Let's take an example to understand the working of the annotation.

JsonAnySetterExample.java

Output 1:

Deserialization Annotations in Jackson

If we don't use @JsonAnySetter annotation, our code will throw UnrecognizedPropertyException for Id, Name, and Email fields.

Output 2:

Deserialization Annotations in Jackson

When we add @JsonAnySetter annotation in a setter method, our code will execute without any error.

@JsonSetter

@JsonSetter is another deserialization annotation that we also use at the time of deserializing the Json. It is similar to @JsonGetter annotation.

The only difference between both of them is that @JsonGetter is used for serializing JSON's additional properties, whereas @JsonSetter is used for deserializing JSON's additional properties.

Let's take an example to understand the working of the annotation.

JsonSetterExample.java

Output:

Deserialization Annotations in Jackson

If we don't use @JsonSetter annotation, our code will throw UnrecognizedPropertyException for facId, facName, and facEmail fields.

Output 2:

Deserialization Annotations in Jackson

When we add @JsonSetter annotation in a setter method, our code will execute without any error.

@JsonDeserialize

It is an important deserialization annotation that we used to define a custom de-serializer for unmarshalling the json object.

Let's take an example to understand how it helps in de-serializing Json.

JsonDeserializerExample.java

Output:

Deserialization Annotations in Jackson

@JsonEnumDefaultValue

It is an important deserialization annotation that we use when we work with Enum. It is mainly used when we need to deserialize an unknown enum value using a default value.

Let's take an example to understand how it helps in deserializing Json.

JsonDeserializerExample.java

Output:

Deserialization Annotations in Jackson





Youtube For Videos Join Our Youtube Channel: Join Now

Feedback


Help Others, Please Share

facebook twitter pinterest

Learn Latest Tutorials


Preparation


Trending Technologies


B.Tech / MCA