在这篇文章中,我们将从性能的角度比较两个著名的 JSON 特定 API,即 Gson 和 Jackson:
代码
包装纸
此类用于转换为 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
}
创建列表的代码
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
}
杰克逊API
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
}
开发者接口
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
}
结果
结论
对于中小型列表的转换,与 Jackson 相比,Gson 提供了更好的响应。对于大型列表,Jackson 提供了比 Gson 更好的响应。基于这一结果,我们可以得出结论,为了将中小型列表转换为 JSON,可以使用 Gson 以获得更好的性能。