Home » Type Handling Annotations in Jackson

Type Handling Annotations in Jackson

by Online Tutorials Library

Type Handling Annotations in Jackson

Type Handling annotations are used for handling types of properties. Jackson provides more than one Type Handling annotations such as JsonTypeInfo, JsonSubTypes, and JsonTypeName. Type Handling annotation can be used at the time of serializing an object or deserializing a Json string.

Type Handling Annotations in Jackson

Let’s understand each one of them one by one with an example to understand how these annotations are used for type handling.

@JsonTypeInfo

It is one of the most used Type Handling annotations which we used at the time of serializing an object. It is used for indicating the details of what type of information to include in serialization and deserialization.

Let’s take an example to understand how the annotation is used for getting the detail of information.

JsonTypeInfoExample.java

Output:

Type Handling Annotations in Jackson

@JsonSubTypes

It is another most used THandling annotation, which we also used at the time of serializing and deserializing an object. It is used for indicating subtypes of types annotated. The @JsonSubTypes annotation is used with @JsonTypeInfo annotation.

Let’s take an example to understand the use of annotation.

JsonSubTypesExample.java

Output:

Type Handling Annotations in Jackson

@JsonTypeName

It is another important Type Handling annotation, i.e., used for setting types name to be used for annotated class.

Let’s take an example to understand how the annotation works in Java:

JsonTypeNameExample.java

Output:

Type Handling Annotations in Jackson


You may also like