Quantcast
Channel: Baeldung
Viewing all articles
Browse latest Browse all 4535

Jackson vs Gson: A Quick Look At Performance

$
0
0

I usually post about Jackson and JSON stuff on Twitter - you can follow me there:

1. Overview

In this quick article we’ll run some quick tests and determine the performance of two popular JSON processing libraries: Gson and Jackson.

We’ll be running benchmarking for both JSON-to-Java and Java-to-JSON conversion tasks with both very simple and moderately complex input.

The performance characteristics of the libraries are similar in some cases and very different in others, and of course, picking the right one for a given application can have a drastic impact on its performance.

2. Project Setup

First, let’s add the Maven dependencies for both Gson and Jackson, as shown below.

2.1. Gson Dependency

Gson is an open-source Java library for processing JSON. It provides toJson() and fromJson() methods to convert Java objects to JSON and vice-versa:

<dependency>
    <groupId>com.google.code.gson</groupId>
    <artifactId>gson</artifactId>
    <version>2.6.2</version>
</dependency>

2.2. Jackson Dependency

Jackson is another open-source Java library for processing JSON:

<dependency>
    <groupId>com.fasterxml.jackson.core</groupId>
    <artifactId>jackson-databind</artifactId>
    <version>2.7.2</version>
</dependency>

3. Basic Comparison

Comparison Notes Jackson Gson
Thread Safety Configuring an instance is not synchronized or thread-safe , but once it is created, operation is fully thread-safe and synchronized. Refer JacksonFAQThreadSafety. Gson instance is immutable, so thread-safe.
Null References – Generated Json Jackson includes null references, so does include them in the generated json, thereby generating longer strings, class can be annotated with @JsonInclude(Include.NON_NULL), or the ObjectMapper can be configured to Include.NON_NULL Gson skips null reference fields, and they will not appear in generated json string
Exception Handling Jackson requires explicit handling of the possible checked JsonParseException, JsonMappingException exceptions. Gson does not need checked exception handling.
Additional Elements Jackson instead throws an UnrecognizedPropertyException. For additional attributes handling incase of Jackson, class can be annotated with @JsonIgnoreProperties(ignoreUnknown=true), or the ObjectMapper can be configured to not FAIL_ON_UNKNOWN_PROPERTIES. There are more ways of doing this, please refer JacksonHowToIgnoreUnknown. Gson ignores extra JSON attributes present in string, that do not have matching Java fields
Github URL https://github.com/FasterXML/jackson-databind https://github.com/google/gson

4. The Scenarios and the Input Data

We will use two different structures in our benchmarks: a simple object and a moderately complex object.

The simple object will have one level of nesting: CustomerPortfolioSimple – has a List of Customer.

The more complex object will have more than one level of nesting:

5. Doing the Conversions

5.1. Converting Java Object to JSON String

Using Gson:

private static String generateJson(CustomerPortfolioComplex customerPortfolioComplex) {
    Gson gson = new Gson();
    return gson.toJson(customerPortfolioComplex);
}

Using Jackson:

private static String generateJson(CustomerPortfolioComplex customerPortfolioComplex)
  throws JsonProcessingException {
    ObjectMapper mapper = new ObjectMapper();
    return mapper.writeValueAsString(customerPortfolioComplex);
}

5.2. JSON String to Map

Using Gson:

private static void parseJsonToMap(String jsonStr) {
    Gson gson = new Gson();
    Map parsedMap = gson.fromJson(jsonStr , Map.class); 
}

Using Jackson:

private static void parseJsonToMap(String jsonStr) 
  throws JsonParseException , JsonMappingException , IOException { 
    ObjectMapper mapper = new ObjectMapper(); 
    Map parsedMap = mapper.readValue(jsonStr , Map.class); 
}

5.3. JSON String to Java Object

Using Gson:

private static void parseJsonToActualObject(String jsonStr) {
    Gson gson = new Gson();
    CustomerPortfolioComplex customerPortfolioComplex = 
      gson.fromJson(jsonStr , CustomerPortfolioComplex.class);
}

Using Jackson:

private static void parseJsonToActualObject(String jsonStr)
  throws JsonParseException , JsonMappingException, IOException {
    ObjectMapper mapper = new ObjectMapper();
    CustomerPortfolioComplex customerPortfolioComplex = 
      mapper.readValue(jsonStr , CustomerPortfolioComplex.class);
}

We can see that there is no difference in the number of lines of code written to use both the libraries. Gson uses the Gson object to parse/transform objects and JSON strings, whereas Jackson uses ObjectMapper.

6. Benchmarking

For the benchmarks, we ran tests for both 10 and 50 iterations for JSON string length of sizes 10 KB, 100 MB and 250 MB.

Actions Performed

  • Java Object to JSON String
  • JSON String to Map
  • JSON String back to Java Object

6.1. Result Data

The tables below report the time in seconds recorded for above operations on both the simple and complex object graphs:

Jackson vs Gson - Performance Numbers

From the data, it is clear that for the 10KB tests, Gson performed significantly better than Jackson. However, in the 100MB and 250MB tests, Jackson performed better.

6.2. Benchmark Conclusions

We ran two iterations of 10 and 50 each on file sizes of 10 KB, 100 MB and 250 MB for both simple and complex object graphs.

For small amounts of data, GSON seems to be a winner by a good margin.

From more complex object graphs – it looks that as the amount of data increases, GSON performance dips compared to Jackson.

Additionally, for the conversion of a JSON string to a Java Map, we can see the substantial difference in the performance of two tools. GSON seems to be in good competition with Jackson, but with huge data files, Jackson performs better.

7. Conclusion

Choosing the right JSON processing library for your application will, in most cases, not be solely based on performance characteristics. However, performance is worth considering as part of that determination.

Our conclusions are simple:

  • If your application works with complex object graphs, Jackson is the stronger performer
  • If your application makes use of simple objects, Gson is extremely efficient

And, of course, this review of these libraries’ performance is simply a point-in-time comparison. There are frequent new releases of both Gson and Jackson with continuous performance improvements. If the performance of JSON parsing is critical to your application, you should continually evaluate the newest releases.

Also note that this is was a rather quick and simple test, not an extensive benchmark of the two libraries.

The complete set of code used for this article can be found in a GitHub project.

I usually post about Jackson and JSON stuff on Twitter - you should follow me there:



Viewing all articles
Browse latest Browse all 4535

Trending Articles