Home » Serialize and Deserialize Enums with Jackson

Serialize and Deserialize Enums with Jackson

by Online Tutorials Library

Serialize and Deserialize Enums with Jackson

In this section, we will understand how we can serialize and deserialize Java Enums by using Jackson 2. In Java, Enumeration is a list of named constants that are used to define a class type. Just like the Java class, Java Enumeration can also have methods, constructors, and instance variables. Java Enums are created by using the enum keyword.

Controlling the Enum Representation

Let’s use the enum keyword and understand how we can define Enum:

Shape.Java

Distance.Java

Serializing Enums to JSON

In order to serialize Enum, we take the help of ObjectMapper class. We use the writeValueAsString() method of ObjectMapper class for serializing Enum. If we serialize Enum by using the writeValueAsString() method, it will represent Java Enums as a simple string.

SerializeEnumExample1.java

Output:

Serialize and Deserialize Enums with Jackson

However, if we marshal Enum to a JSON Object, it will represent the Enum as a proper JSON string. We simply need to alter the Distance.java by simply using the @JsonFormat and making fields public in the following way:

Distance.java

Now, if we run SerializeEnumExample1.java class, it represents the result in the following JSON string format:

Output:

Serialize and Deserialize Enums with Jackson

Enums and @JsonValue

By using the @JsonValue annotation, we can also control the marshaling output for an enum. We simply add @JsonValue annotation on a getter function. In our Distance.java class, we add a getter method by using the @JsonValue annotation. Let’s have a look on the updated Distance.java class:

Distance.java

Now, if we run SerializeEnumExample1.java class, it represents the result in the following format:

Output:

Serialize and Deserialize Enums with Jackson

Custom Serializer for Enum

Custom Serializer is another way of controlling the marshaling output for an Enum. We use the custom serializer when we use the older version of Jackson or when we need more customization for the Enum.

Let’s first create the custom serializer for the Distance enum:

DistanceCustomSerializer.java

Now, we modify Distance.java class by using the DistanceCustomSerializer with the help of the @JsonSerialize annotation. Let’s have a look at the updated Distance.java class.

Distance.java

Now, if we run SerializeEnumExample1.java class, it represents the result in the following format:

Output:

Serialize and Deserialize Enums with Jackson

Deserialize JSON to Enum

Let’s first understand the default behavior of Jackson. By default, the Enum name is used by Jackson to deserialize from JSON.

We have the following JSON string to deserialize:

Now, we deserialize the above string into the Distance.INCH object.

Let’s understand how deserialization is done with the help of an example.

Distance.java

DeserializeEnumExample1.java

Output:

Serialize and Deserialize Enums with Jackson

Deserializing JSON String to Enum Using @JsonValue Annotation

The @JsonValue annotation is one of the annotations which we can use for both serializing and deserializing enums. Enum values are constants, and due to this, @JsonValue annotation can be used for both.

First we simply add the getter method to our Distance.java by using the @JsonValue annotation.

Distance.java

We have the following JSON string to deserialize:

Now, we deserialize the above string into the Distance.INCH object.

Let’s understand how deserialization is done with the help of an example.

DeserializeEnumExample2.java

Output:

Serialize and Deserialize Enums with Jackson

Deserializing JSON String to Enum using @JsonProperty Annotation

Another way of deserializing the JSON string to Enum is by using the @JsonProperty annotation. In order to deserialize a JSON string, we use the @JsonProperty annotation on the enumeration instance in the following way:

Distance.java

DeserializeEnumExample3.java

Output:

Serialize and Deserialize Enums with Jackson

Deserializing JSON String to Enum Using @JsonCreator Annotation

The @JsonCreator is another annotation that we can use for deserializing Enum. All the methods annotated by @JsonCreator are invoked by Jackson for getting an instance of the enclosing class. In order to deserialize the JSON String to Enum by using the @JsonCreator, we will define the forValues() factory method with the @JsonCreator annotation.

Distance.java

We have the following JSON string to deserialize:

Now, we deserialize the above string into the Distance.INCH object.

Let’s understand how deserialization is done with the help of an example.

DeserializeEnumExample4.java

Output:

Serialize and Deserialize Enums with Jackson

Deserializing JSON String to Enum using Custom Deserializer

It is the last way of deserializing JSON String to Enum, which we use when the above-discussed techniques are failed to deserialize the JSON string.

We use the custom deserializer when we use the older version of Jackson or when we have no access to Enum.

We deserialize the following JSON string by using the custom deserializer:

DistanceCustomDeserializer.java

Now, if we run DeserializeEnumExample4.java class, it represents the result in the following JSON string format:

Output:

Serialize and Deserialize Enums with Jackson


You may also like