After a google search, it was found that Jackson had better performance than gson, I plan to replace gson with jackson in my project, but I got an excellent result when running the 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 - 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 json photo is something like: [{"ID": "1318403074887", "orign": "xxx.jpg", "ow": 427, "small": "xxx.jpg", "medium": "xxx.jpg", "o": 640}, {"identifier": "1318403076793", "orign": "xxx.jpg", "ow": 640, "small": "xxx.jpg", "medium ":" xxx.jpg "," oh ": 480}, {" identifier ":" 1318403092168 "," orign ":" xxx.jpg "," ow ": 425," small ":" xxx.jpg ", "middle": "xxx.jpg", "o": 640}]
I am using JAMon to monitor performance, the result is below:
- JAMon Label = jackson, Units = ms .: (LastValue = 18.0, Hits = 30.0, Avg = 18.4, Total = 552.0, Min = 13.0, Max = 37.0, Active = 0.0, Avg Active = 1.0, Max Active = 1.0)
- JAMon Label = gson, Units = ms .: (LastValue = 4.0, Hits = 30.0, Avg = 2.1666666666666665, Total = 65.0, Min = 0.0, Max = 4.0, Active = 0.0, Avg Active = 1.0, Max Active = 1.0)
- JAMon Label = jackson, Units = ms .: (LastValue = 20.0, Hits = 30.0, Avg = 15.166666666666666, Total = 455.0, Min = 12.0, Max = 25.0, Active = 0.0, Avg Active = 1.0, Max Active = 1.0)
- JAMon Label = gson, Units = ms .: (LastValue = 4.0, Hits = 30.0, Avg = 2.2, Total = 66.0, Min = 0.0, Max = 9.0, Active = 0.0, Avg Active = 1.0, Max Active = 1.0)
- JAMon Label = jackson, Units = ms .: (LastValue = 19.0, Hits = 30.0, Avg = 16.433333333333334, Total = 493.0, Min = 11.0, Max = 51.0, Active = 0.0, Avg Active = 1.0, Max Active = 1.0)
- JAMon Label = gson, Units = ms .: (LastValue = 2.0, Hits = 30.0, Avg = 1.9, Total = 57.0, Min = 0.0, Max = 6.0, Active = 0.0, Avg Active = 1.0, Max Active = 1.0)
it seems that gson is faster than Jackson, the average gson time is about 2 ms, and that of Jackson is about 16 ms, am I mistaken when using jackson?