JSON is a fairly common data transfer format in both Web and server development, and the performance of JSON parsing constructs is generally not a concern, except in high-performance systems. At present, there are many kinds of Java open source JSON libraries. Here, we take four common JSON libraries for performance testing and comparison, and select the most appropriate JSON library according to the test results and actual application scenarios. JSON libraries are: JSONObject, Gson, FastJson, and Jackson. A brief introduction to the identity background of the four libraries.

  • JSONObject: Json processing API provided by Android natively. At its core are the JSONObject and JSONArray classes.
  • Gson: Gson is the most full-featured Json parsing tool available. Gson was originally developed by Google for its internal needs. The application of Gson is mainly toJson and fromJson conversion functions. Before using this object conversion, you need to create the object type and its members to successfully convert the JSON string into the corresponding object. Gson is the master of JSON parsing, as it can do jSON-to-bean or bean-to-JSON conversions of complex types.
  • FastJson: FastJson is a high-performance JSON processor written in the Java language. FastJson has some problems converting complex types of beans to Json. The type of reference may appear, leading to Json conversion errors and the need to specify the reference. FastJson uses an original algorithm that makes Parse faster than any OTHER JSON library.
  • Jackson: Event-driven, just like GSON, you can create a JavaBean class that corresponds to the JSON data and parse it out with a simple operation. But unlike Gson parsing, which parses on demand, Jackson cannot parse on demand.

Choosing the right JSON library is a matter of serialization and serialization time and memory consumption, as well as the simplicity of the code.

For these three Json processing schemes, we test and compare them with real data: Serialization and deserialization of ordinary Object class and serialization and deserialization of List class are carried out in three ways respectively, and the processing magnitude is 10, 100, 1000 and 10000 respectively. The time and memory consumption of these operations are compared, and the final results are as follows:

List deserialization

List the serialization

Ordinary Object deserialization

Normal Object serialization

Memory consumption

The data in the figure above were averaged over four experiments.

It can be concluded from the above statistics that:

  • Deserialization operations: In general, JSONObject processing speed is the best, whether dealing with ordinary objects or List collections;
  • Deserialization operations: The Gson library takes more time as the processing magnitude increases;
  • Serialization: JSONObject is still the fastest, followed by FastJson, Gson and Jackson.
  • FastJson operation, processing level has little impact on memory consumption, memory consumption is relatively small;
  • When a large number of operations are performed, the memory consumption of JSONObject is significantly higher than that of the other two methods.
  • Jackson takes the longest time in serialization and deserialization and consumes the most memory, so it is not recommended.

To sum up, JSONObject should be the first choice when the magnitude of data is small in terms of memory consumption and processing speed. However, JSONObject code is complex and error-prone. When the amount of data is large, it is not recommended to use JSONObject because it consumes too much memory and is prone to cause exceptions.

For small data volumes, Gson and FastJson have similar performance, with FastJson slightly better than Gson, but FastJson has a significant advantage in memory consumption when processing large amounts of data.