Javatpoint Logo
Javatpoint Logo

Jackson Vs. Gson

Jackson and Gson both are the libraries/APIs. Both the libraries work with JSON data. These are also used to serialize and deserialize the JSON data to Java objects. Both the libraries are the complete libraries for Java offering JSON data-binding support and being able to handle the complex data types. These libraries provide support for Java generics.

Jackson and Gson both can deserialize JSON data without making any changes in the entity class. In order to use the Jackson and Gson in the code, we need to add the following dependency in our pom.xml file.

Gson Dependency

Jackson Dependency

Now, let's understand how we can use Jackson and Gson libraries to serialize and deserialize the JSON data.

Gson Serialization

There are two ways to serialize Java objects into JSON, i.e., Simple Serialization(by using the toJson() method) and Custom Serialization(by using the custom serializer).

In Simple Serialization, we use the toJson() method of the Gson library to serialize a Java object. We use the toJson() method in the following way:

Let's take an example to understand how we use it for serializing a specific Java object.

GsonSerializationExample1.java

Output:

Jackson Vs. Gson

However, we can also serialize Java objects by using a custom serializer. By using the custom serializer, we can modify the standard behavior. We can handle the null values, use an output formatter with html, add a new output, or exclude properties from the output.

Let's create serializer for the College.java which we later use for serializing.

CollegeGsonSerializer.java

Now, we use the above serializer for serializing the College class.

GsonSerializationExample2.java

Output:

Jackson Vs. Gson

Jackson Serialization

Just like Gson, we can also serialize Java objects into JSON by using Jackson in two ways, i.e., Simple Serialization(by using the writeValueAsString() method) and Custom Serialization(by using the custom serializer).

In Simple Serialization, we use the writeValueAsString() method of Jackson library to serialize a Java object. We use the writeValueAsString() method in the following way:

Let's take an example to understand how we use it for serializing a specific Java object.

JacksonSerializationExample1.java

Output:

Jackson Vs. Gson

Just like Gson Custom serialization, we can perform Jackson Custom serialization too by simply creating a serializer. Let's create a custom serializer for the College.java, which we later use for serializing by using Jackson.

CollegeJacksonSerializer.java

Now, we use the above serializer for serializing the College class.

JacksonSerializationExample2.java

Output:

Jackson Vs. Gson

Gson Deserialization

Deserialization is another important concept for converting JSON strings into Java objects. We implement the toString() method in both the entity class for illustrating the output. We can also deserialize JSON strings into Java objects in two ways, i.e., Simple Deserialization and Custom Deserialization.

Let's take an example of Simple Deserialization to understand how we can deserialize the JSON string into Java object.

GsonDeserializationExample1.java

Output:

Jackson Vs. Gson

In Custom Deserialization, we use the custom deserializer. It allows us to modify the standard deserializer behavior. We use the deserializer to reflect the correct time zone for the Last Date To Apply.

Let's take another example of Custom Deserialization to deserialize the JSON string into Java object.

JobPositionGsonDeserializer.java

GsonDeserializationExample2.java

Output:

Jackson Vs. Gson

Jackson Deserialization

Just like Gson, we can deserialize the JSON string in two ways, i.e., Simple Deserialization and Custom Deserialization. Let's take an example of each one to understand how we deserialize JSON strings with/without a deserializer by using Jackson.

JacksonDeserializationExample1.java

Output:

Jackson Vs. Gson

JacksonDeserializationExample2.java

Output:

Jackson Vs. Gson





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