It was pitted by the Json-lib library before, and the analysis was too slow. After switching to the FastJson library, the performance has made a qualitative leap. Take this to organize the various json libraries
One, Json-lib library
The oldest, most widely used, but worst-performing json library. Not recommended!
Add dependency:
<dependency>
<groupId>net.sf.json-lib</groupId>
<artifactId>json-lib</artifactId>
<version>2.4</version>
<classifier>jdk15</classifier>
</dependency>
Instructions:
- Convert java objects (including list, map) to json string: String jsonStr=JSONObject.fromObject(bean).toString()
- Convert json string to java object: Bean bean=(Bean) JSONObject.toBean(JSONObject.fromObject(jsonStr), Bean.class)
Two, Gson library
The most complete json parser, produced by Google
Need to rely on:
<dependency>
<groupId>com.google.code.gson</groupId>
<artifactId>gson</artifactId>
<version>2.8.6</version>
</dependency>
Instructions:
- Convert java objects (including list, map) to json string: String jsonStr=new Gson().toJson(bean);
- Convert json string to java object: Bean bean=new Gson().fromJson(jsonStr, Bean.class)
- json字符串转list对象:List<Bean> beanList =new Gson().fromJson(jsonStr, new TypeToken<List<Bean>>(){}.getType());
- json字符串转map对象:Map<String,String>map=new Gson().fromJson(str, new TypeToken<HashMap<String,String>>(){}.getType());
Three, Jackson library (springboot project can be used directly, no need to cite the package)
The most popular json library. The default json library of springMVC is jackson.
Need to rely on:
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
<version>2.10.1</version>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-core</artifactId>
<version>2.10.1</version>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-annotations</artifactId>
<version>2.10.1</version>
</dependency>
Instructions:
- Convert java objects (including list, map) to json string: String jsonStr=new ObjectMapper().writeValueAsString(bean);
- Convert json string to java object: Bean bean=new ObjectMapper().readValue(jsonStr, Bean.class)
- json string to list object: List<Bean> beanList = new ObjectMapper().readValue(jsonStr, new TypeReference<ArrayList<Bean>>() {});//TypeReference anonymous inner class, indicating the object type
- json字符串转map对象:Map<String,String>map=new ObjectMapper().readValue(jsonStr, new TypeReference<HashMap<String,String>>(){});
Four, FastJson library (recommended)
The json library produced by Ali has a very fast parsing speed. But the vulnerability has been exposed, just use the latest version.
Add dependency:
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>fastjson</artifactId>
<version>1.2.62</version>
</dependency>
Instructions:
- Convert java objects (including list, map) to json string: String jsonStr=JSON.toJSONString(bean);
- Convert json string to java object: Bean bean=JSONObject.parseObject(jsonStr, Bean.class)
- Convert json string to list object: List<Bean> beanList =JSONObject.parseArray(jsonStr, Bean.class);
- Convert json string to map object: Map<String,String>map=JSONObject.parse(jsonStr);
Five, comparison of json libraries
I don’t have time to write, borrow the net map to compare the performance of each json library
The serialization performance is as follows:
The deserialization performance is as follows:
In summary:
When the amount of data is small: Gson is fast
When the amount of data is large: FastJson is fast
Regardless of the amount of data: Jackson is the most stable
Ranking:
FastJson> Jackson> Gson> Json-lib
Test it again when you have time