JSON parsing framework

JSON is a fairly common data transmission format in both web development and server development. Generally, we do not need to care too much about the performance of JSON parsing and construction, unless it is a system with high performance requirements. 
At present, there are many kinds of open source JSON libraries for Java. Let's take three commonly used JSON libraries for performance test comparison, and analyze how to choose the most suitable JSON library according to the actual application scenarios according to the test results. 
The four JSON libraries are: Gson, FastJson, Jackson, Json-lib. 
Briefly introduce the identity background of the four class libraries.

  • Gson (project address: https://github.com/google/gson ). Gson is currently the most fully functional Json parsing artifact. Gson was originally developed by Google in response to Google's internal needs, but since the first version was publicly released in May 2008, it has been used by many companies or users. The application of Gson mainly consists of two conversion functions, toJson and fromJson, which have no dependencies, do not require additional jars with exceptions, and can run directly on the JDK. Before using this object conversion, you need to create the type of the object and its members to successfully convert the JSON string into the corresponding object. As long as there are get and set methods in the class, Gson can completely convert complex types of json to bean or bean to json. It is an artifact of JSON parsing.
  • FastJson (project address: https://github.com/alibaba/fastjson ). Fastjson is a high-performance JSON processor written in Java language, developed by Alibaba. No dependencies, no extra jars are required, and it can run directly on the JDK. FastJson will have some problems in Bean conversion Json of complex types, there may be reference types, resulting in errors in Json conversion, and references need to be formulated. FastJson uses an original algorithm to increase the speed of parse to the extreme, surpassing all json libraries.
  • Jackson (project address: https://github.com/FasterXML/jackson ). Compared with the json-lib framework, Jackson relies on fewer jar packages, is easy to use and has relatively high performance. Moreover, the Jackson community is relatively active, and the update speed is relatively fast. Jackson will have problems with json conversion beans of complex types, and some collection Map and List conversion will have problems. Jackson converts Json for complex types of beans, and the converted json format is not the standard Json format.
  • Json-lib (project address: http://json-lib.sourceforge.net/index.html ). The beginning of json-lib is also the most widely used json parsing tool. The disadvantage of json-lib is that it depends on many third-party packages, including commons-beanutils.jar, commons-collections-3.2.jar, commons-lang- 2.6.jar, commons-logging-1.1.1.jar, ezmorph-1.0.6.jar, for complex type conversion, json-lib has defects for converting json to bean, for example, one class will appear in another class list or map collection, json-lib will have problems converting from json to bean. json-lib cannot meet the needs of the current Internet in terms of function and performance.

Choosing a suitable JSON library requires consideration of several aspects:

  1. String parsing into JSON performance
  2. String parsing into JavaBean performance
  3. JavaBean construct JSON performance
  4. Collection Construction JSON Performance
  5. Ease of use

The first four are actually considered from the perspective of JSON parsing and construction performance, and the last one is about ease of use, which is actually a problem for developers to consider, if the API of the library is difficult to use, or It is very complicated, so it is not recommended to use it, after all, the performance difference of JSON parsing is not big. The following test results are for four JSON strings of different orders of magnitude, and the first four performances mentioned above are tested respectively. The results are as follows: 
write picture description here
Json-lib is OOM when the data volume is 10W, and the memory cannot be opened to 1G, so directly Pass . 
As can be seen from the above chart:

  1. Parse strings into JavaBeans: FastJson is preferred when the amount of data is small, and Jackson is used when the amount of data is large. However, Jackson cannot stack a collection of objects for parsing, and can only convert it into a collection of Maps, which is better handled by Gson and FastJson.
  2. Parsing strings into JSON: FastJson is preferred when the amount of data is small, and Jackson is used when the amount of data is large.
  3. JavaBean constructs JSON: choose Gson when the amount of data is small, and use Jackson when the amount of data is large.
  4. Collection constructs JSON: first Jackson, second Fastjson.

The above is an analysis of the four JSON class libraries from the perspective of performance. From the perspective of ease of use, FastJson's API design is the simplest and most convenient to use, and four operations can be completed directly by using the two static methods of JSON; while Gson Both Jackson and Jackson need a new object. Although this object can be reused, a global variable needs to be used to save the amount of changes in actual use. At the same time, the API design is not well understood. For FastJson, the complex API is Because it supports streaming parsing, it is suitable for a large number of complex operations on JSON, but in practical applications, operations on JSON are simply parsed into JavaBeans, and then JavaBeans can be serialized into JSON strings, with few complex operations. 
Let's start from my own actual application scenario and consider how to choose a suitable JSON class library. 
Application scenario: The game server basically parses the JSON format string sent by the client into JavaBean, and then converts the encapsulated instruction into a JSON string and returns it to the client. Considering that JavaBean is converted to JSON and collection is converted to JSON The performance difference, so directly use the collection to convert to JSON, avoid using JavaBean. 
Considering that the above scenario is suitable for JSON string parsing using FastJson, Jackson converts the collection into a JSON format string. 
Just taste it, welcome criticism.

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=326474403&siteId=291194637