In our daily use of Python, we often use json format to store some data, especially in web development. However, Python's native json library has poor performance and few functions, and can only cope with simple and lightweight json data storage and conversion needs.
The third-party json library orjson I want to introduce to you in this article has a performance advantage of several times to dozens of times in various public benchmark performance tests. It compresses other Python libraries such as json, ujson, rapidjson, simplejson, etc., and has many additional functions. Let’s take a look at its common methods~
orjson supports all 3.7 to 3.10 For 64-bit version of Python, the orjson version demonstrated in this article is 3.7.0. You can directly use pip install -U orjson to complete the installation. Let's demonstrate the common methods in orjson:
Similar to the native json library, we can use orjson.dumps() to serialize Python objects into JSON data , note that the slight difference is that the result of orjson serialization is not str type but bytes type. In the following example, we serialize a list containing 10 million simple dictionary elements. The difference between orjson and json libraries The time-consuming comparison is as follows:
The process of converting JSON data into Python objects is called deserialization, using orjson .loads() operates and accepts common types such as bytes and str. Based on the previous examples, we add deserialization examples:
In the serialization operation of orjson, many additional functions can be configured through the parameter option. Commonly used ones are:
(1) OPT_INDENT_2
By configuring option= orjson.OPT_INDENT_2, we can add a 2-space indent beautification effect to the serialized JSON result to make up for the lack of parameter indent:
(2) OPT_OMIT_MICROSECONDS
orjson.dumps() can directly convert date and time objects in standard libraries such as datetime and time in Python into corresponding strings. This is something that the native json library cannot do. By configuring option= orjson.OPT_OMIT_MICROSECONDS, you can omit the millisecond part of the suffix of the conversion result:
(3) OPT_NON_STR_KEYS
When the object to be serialized has a non-numeric type When using keys, orjson will throw a TypeError by default. In this case, you need to configure option=orjson.OPT_NON_STR_KEYS to force the conversion of these keys into character types:
##(4) OPT_SERIALIZE_NUMPYAn important feature of orjson is that it can convert complex objects containing data structure objects in numpy into arrays in JSON with compatibility, just with option=orjson.OPT_SERIALIZE_NUMPY: (5) OPT_SERIALIZE_UUIDIn addition to automatically serializing numpy objects, orjson also supports conversion of UUID objects. In versions before orjson 3.0, option=orjson is required. .OPT_SERIALIZE_UUID, and the 3.X version demonstrated in this article does not require additional configuration parameters: (6) OPT_SORT_KEYSBy matching the parameter option=orjson.OPT_SORT_KEYS , the serialized results can be automatically sorted by key: (7) Combine multiple options when your serialization operation needs to involve When using multiple option functions, you can use the | operator to combine multiple option parameters:When the objects you need to serialize involve dataclass custom data structures, you can cooperate with orjson. OPT_PASSTHROUGH_DATACLASS, and then pass the default parameter into a custom processing function to achieve more free data conversion logic. For example, in the following simple example, we can use this feature to desensitize the original data:
Similarly, for datetime type data, we can also cooperate with OPT_PASSTHROUGH_DATETIME and custom default functions to implement date custom format conversion:
For more features of orjson, please go to the official warehouse https://github.com/ijl/orjson to learn more.
The above is the detailed content of Third-party JSON libraries worth learning in Python. For more information, please follow other related articles on the PHP Chinese website!