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.
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