Serialize and Deserialize Booleans as Integers with Jackson
In Java, the Jackson library is the de-facto standard when it comes to processing JSON. In order to map a Boolean value to an integer value, Jackson’s well-defined defaults are not sufficient, and for that, we need to perform manual configuration.
In this section, we will understand how we can serialize Boolean values as Integers, numeric strings, and vice versa.
Let’s understand the serialization and deserialization one by one:
Serialization
As we already mentioned that Jackson’s well-defined defaults are not sufficient to map Boolean values to Integer values. So, we will perform manual configuration, i.e., Field Level Configuration and Global Configuration. Let’s understand both types of configuration one by one.
Field Level Configuration
We have one simple way of serializing Boolean into Integer by annotating our Boolean fields with @JsonFormat. We set the Shape with the Shape.Number in the following way:
Let’s take an example to understand how we can serialize Booleans into Integers.
SerializeBooleansAsIntegerExample1.java
Output:
Global Configuration
Sometimes, it is not possible to annotate each and every field. So, we use the concept of Global Configuration to configure Boolean to Integer serialization globally. Jackson allows us to globally configure @JsonFormat annotation by overriding the defaults in ObjectMapper class.
Let’s take an example to understand how to serialize Booleans into Integer by configure ObjectMapper class globally.
SerializeBooleanAsIntegerExample2.java
Output:
Deserialization
Just like serialization, we also need to perform deserialization of a string having values 0 and 1 for Boolean variables. So, we need to obtain Boolean values from Integers at the time of deserializing JSON strings into our models.
There is no need to use @JsonFormat annotation or any configuration for parsing Integers to Boolean because Jackson, by default, performs this task for us.
Let’s take an example to understand how deserialization is done:
DeserializeIntegerIntoBooleanExample.java
Output:
Numeric Strings Instead of Integers
Many times, we need to serialize/deserialize Boolean values into numeric strings. Let’s understand serialization and deserialization one by one:
Serializing into Numeric Strings
In order to serialize Booleans into Numeric strings, we need to create a custom serializer, and then we use it for serializing Booleans into Numeric strings.
Let’s first create a custom serializer, i.e., NumericStringSerializer, and then we will take an example in which we use this custom serializer for serializing Booleans into numeric strings.
NumericStringSerializer.java
SerializeBooleanToNumericString.java
Output:
Deserializing from Numeric Strings
Just like serializing, for deserializing, we also use a custom deserializer. We will create a custom deserializer, and then we will use it for deserializing Numeric strings into Booleans.
Let’s first create a custom deserializer, i.e., NumericStringDeserializer, and then we will take an example in which we use this custom deserializer for deserializing numeric strings into Booleans.
NumericStringDeserializer.java
DeserializeNumericStringToBoolean.java
Output: