Third-party JSON libraries worth learning in Python

PHPz
Release: 2023-04-11 20:49:07
forward
2130 people have browsed it

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.

Third-party JSON libraries worth learning in Python

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 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:

1. Serialization

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:

Third-party JSON libraries worth learning in Python

2. Deserialization

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:

Third-party JSON libraries worth learning in Python

3. Rich option option

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:

Third-party JSON libraries worth learning in Python

(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:

Third-party JSON libraries worth learning in Python

(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:

Third-party JSON libraries worth learning in Python

##(4) OPT_SERIALIZE_NUMPY

An 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:

Third-party JSON libraries worth learning in Python

(5) OPT_SERIALIZE_UUID

In 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:

Third-party JSON libraries worth learning in Python

(6) OPT_SORT_KEYS

By matching the parameter option=orjson.OPT_SORT_KEYS , the serialized results can be automatically sorted by key:

Third-party JSON libraries worth learning in Python

(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:

Third-party JSON libraries worth learning in Python

4. Add custom processing strategies for dataclass and datetime

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:

Third-party JSON libraries worth learning in Python

Similarly, for datetime type data, we can also cooperate with OPT_PASSTHROUGH_DATETIME and custom default functions to implement date custom format conversion:

Third-party JSON libraries worth learning in Python

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!

Related labels:
source:51cto.com
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template