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.

  • serializes dataclass
  • serializes datetime object, numpy.ndarry, UUID natively
  • serializes to byte rather than string
  • serializes string without escaping unicode to ASCII
  • has strict UTF-8 and JSON format conformance
  • is a lot faster for serialization and de-serialization
  • does not provide load() or dump() functions for reading from/writing to file-like objects

Let’s proceed to the next section and start installing the necessary modules


Setup

Basic Installation

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

Upgrade Existing Package

If you already have an existing orjson package and would like to upgrade it, run the following command

pip install -U orjson

Implementation

Import

Add the following import declaration at the top of your Python file.

import orjson

Serialization and De-serialization

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

Introduction to orjson
27.45 GEEK