Connexion: Use orjson instead of json for JSON serialization

Created on 13 May 2020  路  1Comment  路  Source: zalando/connexion

Description

The authors of orjson describe it like this:

orjson is a fast, correct JSON library for Python. It benchmarks as the fastest Python library for JSON and is more correct than the standard json library or other third-party libraries.

Based on the benchmarks the authors have shared, orjson is 5 to 50 times faster than the standard json library.

Expected behaviour

Add an option to use orjson instead of json.

Actual behaviour

json is the only option for JSON serialization.

Most helpful comment

Comparing the standard Jsonifier used by Connexion and orjson in an example API call in my application, I see the difference in performance can be extreme, 570 times faster:

FlaskApi._serialize_data():

# 2434078 bytes, total time 8ms
body = orjson.dumps(data, option=orjson.OPT_NAIVE_UTC | orjson.OPT_NON_STR_KEYS)

# 3899087 bytes, total time 4565ms
body = cls.jsonifier.dumps(data)

To validate the output, I used json.loads() and verified that it creates identical objects. It does.

>All comments

Comparing the standard Jsonifier used by Connexion and orjson in an example API call in my application, I see the difference in performance can be extreme, 570 times faster:

FlaskApi._serialize_data():

# 2434078 bytes, total time 8ms
body = orjson.dumps(data, option=orjson.OPT_NAIVE_UTC | orjson.OPT_NON_STR_KEYS)

# 3899087 bytes, total time 4565ms
body = cls.jsonifier.dumps(data)

To validate the output, I used json.loads() and verified that it creates identical objects. It does.

Was this page helpful?
0 / 5 - 0 ratings