Over a million developers have joined DZone.
{{announcement.body}}
{{announcement.title}}

Comparing the JSON APIs Gson and Jackson

DZone's Guide to

Comparing the JSON APIs Gson and Jackson

Compare JSON APIs Gson and Jackson based on performance for JSON conversion wrappers and creation of lists in both APIs.

· Performance Zone
Free Resource

Evolve your approach to Application Performance Monitoring by adopting five best practices that are outlined and explored in this e-book, brought to you in partnership with BMC.

In this post we will compare two famous JSON specific API i.e. Gson and Jackson from a performance point of view:json_1

Code

Wrapper

This class is used for conversion to JSON

public class MeasurementRecord {

private String measurementId;

private long duration;

private long time;

private MeasurementType type=MeasurementType.METHOD_CALL;

/**
 * 
 * @param measurementId
 * @param duration
 * @param time
 * @param type
 */
public MeasurementRecord(String measurementId, long duration, long time,
MeasurementType type) {
super();
this.measurementId = measurementId;
this.duration = duration;
this.time = time;
this.type = type;
}
//getters and setters
}


public enum MeasurementType {

WEB_REQUEST,SQL,EXCEPTION,METHOD_CALL

}

Code for Creating List

private static List<String> getList(int iteration){
List l=new ArrayList();
for(int i=0;i<iteration;i++){
l.add(new MeasurementRecord("/test.html", 10, System.currentTimeMillis(), MeasurementType.WEB_REQUEST));
}
return l;
}

Jackson API

private static long jacksonTest(int iteration)throws Exception{
ObjectMapper mapper=new ObjectMapper();
List<String> l=getList(iteration);
long T1=System.nanoTime();
String json=mapper.writeValueAsString(l);
long T2=System.nanoTime();
return (T2-T1);
}

Gson API

private static long gsonTest(int iteration){
Gson gson = new GsonBuilder().create();
List l=getList(iteration);
long T1=System.nanoTime();
String json=gson.toJson(l);
long T2=System.nanoTime();
return (T2-T1);
}

Results

resultsgraph

Conclusion

For converting small- or medium-sized lists, Gson provides a better response compared to Jackson. For large lists, Jackson provides a better response than Gson. Based on this results one can conclude that for converting small or medium size list to JSON one can use Gson for better performance.

Learn tips and best practices for optimizing your capacity management strategy with the Market Guide for Capacity Management, brought to you in partnership with BMC.

Topics:
gson ,java ,json ,jackson ,performance

Opinions expressed by DZone contributors are their own.

THE DZONE NEWSLETTER

Dev Resources & Solutions Straight to Your Inbox

Thanks for subscribing!

Awesome! Check your inbox to verify your email so you can start receiving the latest in tech news and resources.

X

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}