After searched in google, found that jackson has better performance than gson, i plan to replace gson with jackson in my project, but i got a diffrent result when run test code.
private static final Type PHOTOLINKS_TYPE_GSON = new TypeToken<List<Photo>>() {}.getType();
private static final Type PHOTOCAPTIONS_TYPE_GSON = new TypeToken<List<String>>() {}.getType();
Gson gson = new Gson();
private void testGson(String photoJson, String captionJson) {
GSON_MON.start();
List<Photo> photos = gson.fromJson(photoJson, PHOTOLINKS_TYPE_GSON);
List<String> photoCaptions = gson.fromJson(captionJson, PHOTOCAPTIONS_TYPE_GSON);
GSON_MON.stop();
}
TypeReference<List<Photo>> PHOTOLINKS_TYPE_JACKSON = new TypeReference<List<Photo>>(){};
TypeReference<List<String>> PHOTOCAPTIONS_TYPE_JACKSON = new TypeReference<List<String>>(){};
ObjectMapper mapper = new ObjectMapper();
private void testJackson(String photoJson, String captionJson) {
JACKSON_MON.start();
try {
List<Photo> photos = mapper.readValue(photoJson, PHOTOLINKS_TYPE_JACKSON);
List<String> photoCaptions = mapper.readValue(captionJson, PHOTOCAPTIONS_TYPE_JACKSON);
} catch (JsonParseException e) {
e.printStackTrace();
} catch (JsonMappingException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
JACKSON_MON.stop();
}
Photo is a normal class:
@JsonIgnoreProperties(ignoreUnknown = true)
private static class Photo implements Serializable {
private static final long serialVersionUID = 5645393489907650496L;
public String small;
public String middle;
public String orign;
public String caption;
public String ow;
public String oh;
}
and the photo json is something like: [{"id":"1318403074887","orign":"xxx.jpg","ow":427,"small":"xxx.jpg","middle":"xxx.jpg","oh":640},{"id":"1318403076793","orign":"xxx.jpg","ow":640,"small":"xxx.jpg","middle":"xxx.jpg","oh":480},{"id":"1318403092168","orign":"xxx.jpg","ow":425,"small":"xxx.jpg","middle":"xxx.jpg","oh":640}]
i use JAMon to moniter the perfomance, below is the result:
it seems gson is more faster than jackson, the average time of gson is about 2ms while jackson is about 16ms, does i make mistake when using jackson?
The tally for fastest library on number of files won is: GSON – 14. JSONP – 5. Jackson – 1.
GSON can use the Object definition to directly create an object of the desired type. While JSONObject needs to be parsed manually.
Both Gson and Jackson are good options for serializing/deserializing JSON data, simple to use and well documented. Advantages of Gson: Simplicity of toJson/fromJson in the simple cases. For deserialization, do not need access to the Java entities.
Easy to use − Gson API provides a high-level facade to simplify commonly used use-cases. No need to create mapping − Gson API provides default mapping for most of the objects to be serialized. Performance − Gson is quite fast and is of low memory footprint. It is suitable for large object graphs or systems.
It may be simple issue with performance monitoring: looks like you are not "warming up" JVM by running tests for long enough to let it compile byte code and so on. Typically tests need to be run at least for 5 - 10 seconds before taking measurements.
So perhaps try doing that first, see how numbers change. I bet numbers for both will increase -- it should take a fraction of a millisecond for small objects.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With