Jackson vs Gson: Edge Cases in JSON Parsing for Java Apps

JSON (Javascript Object Notation) is a collection of key-value pairs that can be easily parsed and generated by applications. It is a subset of JavaScript Programming Language Standard ECMA-262. The parsing of JSON is required in most applications, such as restful APIs or applications that need data serialization. 

In the Java ecosystem, the two most popular libraries for handling JSON data are Jackson and Gson. Both are used widely and offer unique advantages. This article uses edge-case examples to explore the features of both libraries on different parameters.

Brief Overview of Jackson and Gson

Jackson

Jackson was developed by FasterXML and is used in enterprise applications and frameworks such as Spring Boot. It offers parsing, serialization, and deserialization of JSON data. The following features make this library popular among developers:

  1. Jackson is the default JSON processing library in Spring Boot, which eliminates manual configuration in most cases.
  2. It facilitates JSON deserialization into generic types using TypeReference or JavaType.
  3. It provides different annotations to customize serialization and deserialization behavior.  For example, @JsonProperty(name) makes the mapping between the incoming key and the actual Java POJO field seamless.
  4. It provides extensive and robust support for bidirectional Databinding (JSON to POJO and vice versa), streaming API (API reads JSON into POJO), and Tree model parsing (an in-memory map of JSON objects).
  5. The Jackson library offers high performance due to minimizing memory overhead and optimizing serialization/deserialization (from JSON to POJO and vice versa).
  6. Jackson supports additional modules such as XML, YAML processing, and Kotlin, scala-specific enhancements.
  7. Annotations such as @JsonTypeInfo and @JsonSubTypes handle polymorphic types.
  8. It handles missing or additional fields in JSON data due to its backward and forward compatibility.
  9. Jackson provides support for immutable objects and classes with constructors, including those using builder patterns.
  10. The ObjectMapper class is thread-safe and, therefore, enables efficient use in multithreaded applications.

Gson

Gson was developed by Google and designed for converting JSON to Java objects (POJO) and vice versa. It is simple and ideal to use for smaller applications that need quick implementations. The open-source library offers the following key features:

  1. Gson has minimal external dependencies; therefore, it is easy to integrate.
  2. It supports nested objects and complex data types such as lists, maps, and custom classes.
  3. It can deserialize JSON into generic collections like List<T>, Map<K,V> using TypeToken.
  4. Gson Library’s JsonSerializer and JsonDeserializer interfaces allow customized implementation.
  5. The null values are excluded in the JSON output by default, and if required, null values can be included in the output.
  6. Annotations @SerializedName maps JSON keys to Java fields with different names.
  7. The Gson objects are thread-safe and, therefore, can be used in multithreaded applications.
  8. Class GsonBuilder can apply custom naming policies for fields. For example, FieldNamingPolicy.IDENTITY is the default policy, meaning the field name is unchanged.

Edge Cases Considered in This Comparison

Feature Jackson GSON

Extra Fields

Ignored by default, configurable.

Ignored by default.

Null values

Supports @JsonInclude.

Requires .serializeNulls().

Circular References

Supported using @JsonIdentityInfo.

Not supported directly.

Data Handling

Supports Java 8 Date API with modules.

Requires custom-type adapters.

Polymorphism

Built-in with @JsonTypeInfo.

Needs custom deserialization logic.

The input JSON considered for comparison with Jackson and Gson libraries is present on GitHub.

Input JSON considered for comparison with Jackson and Gson libraries

The model class representation of JSON is on GitHub.

Model class representation of JSON

Jackson Implementation

The above JSON is converted to a Java object using the Jackson libraries below:

XML

 

JSON Parsing main class using Jackson library:

Java

 

The output of the above class is as follows:

Output of the above class

Gson Implementation

The Gson dependency used to convert the above JSON to a Java object is below:

XML

 

JSON parsing using GSON library main class:

Java

 

The output of the above main class is as follows:

Output of the main class

Which One Should I Choose?

Jackson offers high performance; therefore, it must be used when projects involve complex data structures or large datasets, whereas Gson must be used when there are smaller datasets and the data structure is simple.

Conclusion

Both libraries can handle the above dataset effectively and are excellent while processing JSON parsing in JAVA. The comparison mentioned above helps one to choose the right library based on project requirements.

The code snippets mentioned above are available in the GitHub repository.

A detailed comparison between Jackson and Gson is available on Baeldung. The Jackson official Documentation offers in-depth information on Jackson’s features and configuration. Similarly, Gson Official documentation provides a detailed implementation guide.

Source:
https://dzone.com/articles/jackson-vs-gson-edge-cases-json-parsing-java