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.
Join the DZone community and get the full member experience.
Join For FreeIn this post we will compare two famous JSON specific API i.e. Gson and Jackson from a performance point of view:
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
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.
JSON
Gson
Jackson (API)
Opinions expressed by DZone contributors are their own.
Comments