Javatpoint Logo
Javatpoint Logo

Serialization Annotations in Jackson

Serialization annotations are used when we serialize Java objects into a Json string. Jackson library provides several serialization annotations such as @JsonSerialize, @JacksonGetter, @JsonAnyGetter, etc.

Serialization Annotations in Jackson

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

@JsonAnyGetter

@JsonAnyGetter is one of the most important annotations used for serializing JSON's additional properties in the same way as the other properties are serialized. The annotation allows the getter method for returning a Map which we then used for serializing. JsonAnyGetter annotation is used for serialization, which means it comes in the category of Serialization Annotation.

Let's take two examples to understand the @JsonAnyGetter annotation. In the first example, we will convert Java Object into JSON without using annotation. In the second example, we do it by using annotation to understand the use of @JsonAnyGetter annotation.

JsonAnyGetterExample1.java

Output:

Serialization Annotations in Jackson

JsonAnyGetterExample2.java

Output:

Serialization Annotations in Jackson

Explanation:

The output comes with a facultyData header in the first example because we don't use the @JsonAnyGetter annotation. In the second example, the output comes with only Id, Name, and Email values. There is no facultyData header in output because we use @JsonAnyGetter annotation there.

@JsonGetter

@JsonGetter is another most important annotation used for serializing JSON's additional properties. It is similar to the @JsonProperty annotation that allows a specific method to be marked as a getter method. @JsonGetter annotation is also used for serialization, so it comes in Serialization Annotation.

Let's take two examples to understand the @JsonGetter annotation. In the first example, we will convert Java Object into JSON without using annotation. In the second example, we do it by using annotation to understand the use of @JsonGetter annotation.

JsonGetterExample1.java

Output:

Serialization Annotations in Jackson

JsonGetterExample2.java

Output:

Serialization Annotations in Jackson

Explanation

In the first example, each variable name starts with a faculty prefix because we don't use the @JsonGetter annotation. The variable names start without prefix(faculty) values in the second example.

@JsonPropertyOrder

@JsonPropertyOrder is another important annotation of Jackson. It is used to preserve a specific order when serializing a JSON object. It is also used for serialization, so it also comes in Serialization Annotation.

Let's take two examples to understand the @JsonPropertyOrder annotation. Logics and functionality of both the code are similar. The only difference between both of them is that in the first code, we don't use @JsonPropertyOrder, and in the second one, we use it.

JsonPropertyOrderExample1.java

Output:

Serialization Annotations in Jackson

JsonPropertyOrderExample2.java

Output:

Serialization Annotations in Jackson

Explanation

In the first example, the output comes with the default order of the fields because we don't use @JsonPropertyOrder. In the second example, the output comes with the order of fields that we define in @JsonPropertyOrder. So, JsonProprtyOrder annotation is used to get the result field in the specified order.

@JsonRawValue

@JsonRawValue is another important annotation of Jackson used in serializing an object. It is used for serializing the text without escaping or without any decoration. It also comes in Serialization Annotations.

Let's take examples to understand the use of @JsonRawValue annotation in which we write the code with and without @JsonRawValue annotation.

JsonRawValueExample1.java

Output:

Serialization Annotations in Jackson

JsonRawValueExample2.java

Output:

Serialization Annotations in Jackson

Explanation

In the first example, the value of json field comes with decoration because we don't use @JsonRawValue annotation. Whereas, in the second example, the value of json field comes without escaping or decoration because we use @JsonRawValue annotation. So, the values come without a slash(/).

@JsonValue

@JsonValue is one of the most used and most important annotations which is used for serializing a single object by using its single method.

Let's take an example to understand how the annotation is used to serialize an object:

JsonValueExample1.java

Output:

Serialization Annotations in Jackson

@JsonRootName

@JsonRootName annotation is one most important annotations used for indicating the name of the POJO that should be serialized. In simple words, it is used for wrapping an object for serializing with a top-level element. It also comes in the category of Serialization Annotation.

We pass the name as a parameter to the annotation. We also use the "WRAP_ROOT_VALUE", I.e., the feature of the SerializationFeature enum. We enable it for wrapping the route value within a single property JSON object where the key is the route name.

Let's take an example of @JsonRootName to understand how it works:

JsonRootNameExample.java

Output:

Serialization Annotations in Jackson

@JsonSerialize

@JsonSerialize is one of the most used annotations while serializing a Java object. It is used for defining a custom serializer for marshaling the Json Object.

Let's take an example to understand how it helps in serializing an Object.

JsonSerializerExample.java

Output:

Serialization 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