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

Download our Introduction to API Performance Testing and learn why testing your API is just as important as testing your website, and how to start today.

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.

Find scaling and performance issues before your customers do with our Introduction to High-Capacity Load Testing guide.

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