overview

Why Integrate

JSON has gradually become a trend to replace XML as a cross-platform data exchange format in current development (such as the RestFul specification). I believe that more and more developers are using the JSON format when providing external interfaces.

It is undeniable that there are many excellent JSON frameworks available now. I often use packages like Alibaba’s FastJSON and Jackson, which are very good and have outstanding performance and simple usability. Hutool initially did not intend to develop its own JSON, but during the encapsulation of various tools, it was found that JSON has become indispensable. Therefore, the official JSON parsing of json.org was incorporated and modified. During the modification process, we actively absorbed the advantages of other libraries, optimized member methods, abstracted interfaces and classes, and ultimately formed Hutool-json.

Introduction

Hutool-json has only two core classes:

  • JSONObject
  • JSONArray This is similar to other JSON packages. At the same time, a JSONUtil utility class is provided to simplify various operations and conversions for JSON.

In addition to the core classes, some auxiliary classes are provided to implement specific functions:

  • JSONSupport Bean class inherits this object to seamlessly convert to JSON or a JSON string. At the same time, it implements the toString() method to output the current object as a JSON string.
  • XML Provides fast conversion between JSON and XML, and JSONUtil has corresponding static encapsulations.
  • JSON Interface class implemented by both JSONObject and JSONArray. The JSONUtil.parse method returns this object by default (because it is unknown whether it is a JSON object or a JSON array), and then the object type can be converted based on the actual type.

Similar to FastJSON, JSONObject implements the Map interface, and JSONArray implements the List interface, so we can use familiar APIs to operate on JSON.

In JSON, Hutool encapsulates getXXX methods to support most built-in type value retrieval operations. For example:

JSONObject json1 = JSONUtil.createObj();
json1.getStr("key");
json1.getInt("key");
json1.getLong("key");
json1.getDouble("key");
json1.getBigDecimal("key");

The addition of these member methods can save a lot of type conversion code and greatly improve the convenience of JSON operations.