Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Jackson JSON Parser performance

Multiple posts on the Internet point toward Jackson as having better parsing performance than GSON, granting somewhere in the neighborhood of 20-30% speed improvement.

  • http://rick-hightower.blogspot.com/2014/04/new-json-serialization-benchmark.html
  • http://tuhrig.de/jaxb-vs-gson-and-jackson/
  • http://java.dzone.com/articles/be-lazy-productive-android

Pulling out our GSON parser and replacing with Jackson resulted in a 7x slowdown in my project, with latency of over 300 ms per invocation. The same parse job on GSON takes less than 50 ms.

I went through the list of "gotchas" on the Jackson Wiki, but nothing there stood out as a red flag.

  • http://wiki.fasterxml.com/JacksonDataBinding

For example, I'm not recreating my ObjectMapper, and I'm using ObjectReader to read all the JSON. Here is some sample code:

public class JsonParser {
    @Nonnull
    private final ObjectMapper objectMapper;

    public JsonParser() {
        final ObjectMapper objectMapper = new ObjectMapper();
        objectMapper.setDateFormat(DateFormatUtil.getGmtIso8601DateFormat());

        SimpleModule simpleModule = new SimpleModule();
        objectMapper.registerModule(simpleModule);
        this.objectMapper = objectMapper;
    }

    public <T> T fromJson(InputStream inputStream, Class<T> clazz) throws IOException {
        ObjectReader reader = objectMapper.reader(clazz);
        return reader.readValue(inputStream);
    }
}

The object above gets created once and is used for the duration of the app to translate JSON into POJOs. A sample POJO can be seen here:

@JsonSerialize(include= Inclusion.NON_NULL)
@JsonIgnoreProperties(ignoreUnknown = true)
public class ActivityEntity {
    public ActivityObjectEntity actor;
    public ActivityObjectEntity object;
    public ActivityObjectEntity provider;
    public ActivityObjectEntity target;
    public ActivityObjectEntity generator;
    public String content;
    public String title;
    public String verb;
    public String url;
    public Date published;
    public Date updated;
    // other properties omitted ...
}

What is being serialized is actually a list of the above items.

Here are my sample trace view windows from each run. Note, this is not an anomaly. I consistently get the same order of performance out of both Gson and Jackson parsing the same dataset.

Comparison was with Jackson 2.4.2 and Gson 2.2.4

traceview output from Jacksontraceview output from Gson

like image 822
markshiz Avatar asked Oct 08 '14 15:10

markshiz


People also ask

What is the best JSON parser for Java?

Jackson JSON Java Parser is very popular and used in Spring framework too. Java JSON Processing API is not very user friendly and doesn’t provide features for automatic transformation from Json to Java object and vice versa. Luckily we have some alternative APIs that we can use for JSON processing.

What is the difference between jsongenerator and jsonparser?

JsonGenerator is used to write JSON while JsonParser is used to parse a JSON file. To demonstrate both reading and writing of JSON data in one program, I have created two static methods, createJSON () and parseJSON ().

How do Gson and Jackson compare for JSON data binding in Java?

In this article, we'll compare the Gson and Jackson APIs for serializing and deserializing JSON data to Java objects and vice-versa. Gson and Jackson are complete libraries offering JSON data-binding support for Java. Each are actively developed open-source projects which offer to handle of complex data types and support for Java Generics.

What are the advantages of using Jackson JSON Java API?

Jackson JSON Java API also provide streaming support that is helpful in working with large json data because it reads the whole file as tokens and uses less memory. The only problem with streaming API is that we need to take care of all the tokens while parsing the JSON data.


Video Answer


1 Answers

Code looks correct, and even at its worst, Jackson should be no slower than Gson; and certainly not by multiples of anything.

If you were able to get a profiler snapshot for call stack (for running deserialization continuously for 10+ seconds), that would probably point out where excess time is spent, and could help figure out the culprit.

I would still double-check that JsonParser is not inadvertently constructed multiple times: one tricky case is for example via frameworks like Jersey (directly or via DropWizard) that may construct resources multiple times unless told to construct and use singleton instances. I say this because symptoms just fit this case well, not because I doubt that you hadn't done due diligence.

like image 52
StaxMan Avatar answered Oct 10 '22 12:10

StaxMan