The topic for today is on optimizing JSON serialization and de-serialization in your Python application. If you often use the built-in json
module and are looking to improve the performance or transaction per second for your application, you should consider using orjson
module instead. Based on the official documentation, orjson
is
“… a fast and correct Python JSON library supporting dataclasses, datetimes, and numpy natively.”
Let’s have a look at the advantages and disadvantages as compared to other Python JSON libraries.
load()
or dump()
functions for reading from/writing to file-like objectsLet’s proceed to the next section and start installing the necessary modules
It is highly recommended to create a virtual environment before you continue with the installation. You can easily install orjson
via pip install
. Run the following command at your terminal:
pip install orjson
If you already have an existing orjson
package and would like to upgrade it, run the following command
pip install -U orjson
Add the following import declaration at the top of your Python file.
import orjson
Let’s have a look at the following example which serialize and de-serialize strings, list and dictionary.
data = {
"emoji_tears": "😂",
"emoji_clock": "⏰",
"integer": 123,
"float": 10.4,
"boolean": False,
"list": ["element1", "element2"],
"dict": {"key1": "value1", "key2": "value2"},
"russian": "Привет",
"chinese": "您好",
"japanese": "こんにちは"
}
# serialize, returns byte instead of string
json_byte = orjson.dumps(data)
# de-serialize
orjson.loads(json_byte)
For serialization, you can specify the following input parameters:
default
— A callable that returns a supported type. Can be used to serialize a subclass or arbitrary types. Besides, you can enforce a rule to handle unsupported date type by raising an exception such as TypeError
.option
— To modify how a data is serialize via integer constant in orjson
.#programming #devops #python #json #python-dataclass