Compare JSON API

Introduction

In this post we will compare   two famous JSON specific API i.e.  GSON and Jackson from 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;
       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
}

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 results graph

Conclusion

For converting small or medium size list GSON provide better response as compared to Jackson but for large size list Jackson provide some 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.

Advertisement
%d bloggers like this: