Home » Serialize and Deserialize Specific Fields in Jackson

Serialize and Deserialize Specific Fields in Jackson

by Online Tutorials Library

Serialize and Deserialize Specific Fields in Jackson

In this section, we will explore various ways through which we can control whether the field is serialized/deserialized or not. Jackson provides the following ways to make a field as serialized or deserialized.

  1. By making a field public
  2. By using the getter method (Both serializable and de-serializable)
  3. By using the setter method (Deserializable Only)
  4. By making all fields globally Serializable
  5. Rename the property on Serialization/Deserialization

Let’s understand each one of them one by one:

1. By making a field Public

It is one of the simplest ways by which we can make a field serialize and de-serialize. In this way, we simply make the field public for this. We will declare a class with a public, a protected and a private field.

By default, only the available field will be serialized to JSON.

Let’s take an example to understand how we can make a field serializable and de-serializable by making it public.

SerializeAndDeserializeExample1.java

Output:

Serialize and Deserialize Specific Fields in Jackson

2. By using the getter method

In the previous method, we marked the field public to make it serializable and de-serializable. Most of the time, we need to serialize or deserialize a non-public field or, can say, package-private and private field. In order to serialize/deserialize a non-public field, we simply use the getter for the field.

Let’s take an example to understand how we can serialize a non-public field.

SerializeAndDeserializeExample2.java

Output:

Serialize and Deserialize Specific Fields in Jackson

3. By using the setter method

The getter method is used for both serializable and de-serializable, whereas the setter method is used for only de-serializable. We use the setter method in the following way:

Let’s take an example to understand how we can use the setter method to make a non-public field de-serializable.

SerializeAndDeserializeExample3.java

Output:

Serialize and Deserialize Specific Fields in Jackson

4. By making all the fields globally serializable

In some cases, we cannot directly modify the source code, so we need a way through which Jackson can deal with private fields from outside. This global configuration will be done at the ObjectMapper level. We simply turn on the AutoDetect function to use it for serialization.

We turn on the AutoDetect function in the following way:

Let’s take an example to understand how we can use AutoDetect function to make all fields serializable.

SerializeAndDeserializeExample4.java

Output:

Serialize and Deserialize Specific Fields in Jackson

5. Rename the name of a property on Serialization/Deserialization

In the above examples, we have controlled over the fields that are going to serialize or deserialize. Going beyond it, we can also have control over the way a field maps to JSON and back.

In order to change the field name, we use the @JsonProperty annotation. In the annotation constructor, we pass the name of the property.

Let’s take an example to understand how we can use @JsonProperty annotation to change the name of the field for serialization.

SerializeAndDeserializeExample5.java

Output:

Serialize and Deserialize Specific Fields in Jackson


You may also like