Jackson, JSON-lib, Gson performance comparison
Recently, I did some performance optimization work. When I selected the JSON class library, I found that in addition to the commonly used JSON-lib, there is also a JSON processor, Jackson, which is said to have the fastest performance. So I used the JMeter I just learned. The class library conducts a simple performance comparison.
Jackson:http://jackson.codehaus.org/
JSON-lib:http://json-lib.sourceforge.net/
Gson:http://code.google.com/p/google-gson/
test environment:
1. Working computer: Intel dual-core E8400 with a total of 6GHz, memory 4GB, WinXP
2. JSON-lib uses the latest JDK15, GSON version is the latest v1.4, Jackson is also the latest v1.5.5, JDK-v1.6.0_20, JMeter-v2.4
3. Do not start any unrelated processes during the test. After each test is completed, close JMeter to sort out the memory, and then proceed to the next test. Each test is run 3 times and the average value is taken.
4. Converting JSON to Java Bean means converting the JSON format into a Java class. This class includes attributes of types such as Map, List, Date, Integer/Long/Double, and String. The same is true for Java Bean to Json. In addition, the two are converted to each other, and the data of each conversion is randomly generated
Test Results:
* The larger the value of throughput, the better, and the smaller the value of total time-consuming, the better
JSON to Bean, 5 concurrent threads, about 200 bytes of objects, 10 million conversions:
Jackson | JSON-lib | Gson | |
TPS | 64113.7 | 8067.4 | 13952.8 |
Total time (seconds) | 155 | 1238 | 700 |
Bean to JSON, 5 concurrent threads, about 200 bytes of objects, 10 million conversions:
Jackson | JSON-lib | Gson | |
TPS | 54802 | 15093.2 | 17308.2 |
Total time (seconds) | 181 | 661 | 560 |
JSON to Bean, 5 concurrent threads, about 2K objects, 10 million conversions:
Jackson | JSON-lib | Gson | |
TPS | 37314 | 2406.9 | 3657.50 |
Total time (seconds) | 267 | 4120 | 2720 |
Bean to JSON, 5 concurrent threads, about 2K objects, 10 million conversions:
Jackson | JSON-lib | Gson | |
TPS | 30922.2 | 4274.8 | 4977.00 |
Total time (seconds) | 322 | 2320 | 2000 |
Test summary:
1. Obviously, no matter what form of conversion, Jackson > Gson > Json-lib.
The processing power of Jackson is even about 10 times higher than that of Json-lib
2. JSON-lib seems to have stopped updating, the latest version is also based on JDK15, and the Jackson community is more active;
3. While testing the performance, the correctness of the conversion of these three class libraries was checked in a human way , and all three were 100% correct ;
4. JSON-lib is more cumbersome when converting types such as Date. For example, the following are the conversion results of the two:
JSON-lib:
{"brithday":{"date":17,"day":2,"hours":9,"minutes":24,"month":7,"seconds":26,"time":1282008266398,"timezoneOffset":-480,"year":110}}
Jackson:
{"brithday":1282008123101}
5. JSON-lib relies on 5 commons series packages and ezmorph packages , while Jackson only depends on commons-logging except its own.
6. Jackson provides complete node-based Tree Model and complete OJM data binding function.
Jackson usage example:
JacksonMapper:
Created as a hungry Chinese-style singleton mode , the core class ObjectMapper used by Jackson for conversion does not need to create a new object every time. There is a sentence on the official website: can reuse, share globally
- /**
- * @author xuanyin
- *
- */
- publicclass JacksonMapper {
- /**
- *
- */
- privatestaticfinal ObjectMapper mapper = new ObjectMapper();
- /**
- *
- */
- private JacksonMapper() {
- }
- /**
- *
- * @return
- */
- publicstatic ObjectMapper getInstance() {
- return mapper;
- }
- }
JSON to Bean:
- ......
- String json = "...";
- ObjectMapper mapper = JacksonMapper.getInstance();
- YourBean bean = mapper.readValue(json, new YourBean().getClass());
- ......
Bean to JSON:
- ......
- YourBean bean = new YourBean();
- ......
- ObjectMapper mapper = JacksonMapper.getInstance();
- StringWriter sw = new StringWriter();
- JsonGenerator gen = new JsonFactory().createJsonGenerator(sw);
- mapper.writeValue(gen, bean);
- gen.close();
- String json = sw.toString();
- ......
* Of course YourBean in the above two pieces of code can also be the basic type of Java