Jackson Performance JSON Parser

Several Internet posts point to Jackson as a better syntax performance than GSON, providing somewhere around 20-30% speed improvement.

Pulling out our GSON parser and replacing it, Jackson led to a slowdown of 7 times in my project with a delay of more than 300 ms per call. The same parsing action on GSON takes less than 50 ms.

I looked at the gotchas list on Wiki Jackson, but nothing stood out like a red flag.

For example, I do not recreate my ObjectMapper , and I use ObjectReader to read all JSON. Here is a 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 is created once and is used throughout the application to translate JSON to POJO. An example of 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 serialized is actually a list of the above items.

Here are my examples of trace windows from each run. Notice this is not an anomaly. I sequentially get the same performance order as Gson and Jackson by analyzing the same dataset.

The comparison was with Jackson 2.4.2 and Gson 2.2.4

traceview output from jacksontraceview output from gson

+6
source share
1 answer

The code looks right, and even in the worst case, Jackson should be no slower than Gson; and of course nothing is multiple.

If you were able to get a profiler snapshot for the call stack (for continuous deserialization for 10+ seconds), this will probably indicate where the extra time has been spent, and can help figure out the culprit.

I would still double check that JsonParser not created unintentionally several times: one difficult case is, for example, through frameworks such as Jersey (directly or through DropWizard) that can create resources several times, if not to create and use singleton instances, I say this because the symptoms are just right for this case, and not because I doubt that you have performed due diligence.

+2
source

Source: https://habr.com/ru/post/976445/


All Articles