Skip to main content

Fast Python JSON library

Project description

orjson

orjson is a fast JSON library for Python. It benchmarks as the fastest Python library for JSON serialization, about twice as fast or more as the nearest other library, with deserialization performance slightly worse to similar to the fastest library.

It supports CPython 3.5, 3.6, and 3.7. It is not intended as a drop-in replacement for the standard library's json module.

Usage

Install

To install a manylinux wheel from PyPI:

pip install --upgrade orjson

To build a release wheel from source, assuming a Rust nightly toolchain and Python environment:

git checkout https://github.com/ijl/orjson.git && cd orjson
git submodule init && git submodule update
pip install --upgrade pyo3-pack
pyo3-pack build --release --strip --interpreter python3.7

There is no runtime dependency other than a manylinux environment (i.e., deploying this does not require Rust or non-libc type libraries.)

Serialize

def dumps(obj: Union[str, bytes, dict, list, tuple, int, float, None]) -> bytes: ...

dumps() serializes Python objects to JSON.

It has no options, does not support hooks for custom objects, and does not support subclasses.

It raises TypeError on an unsupported type or a number that is too large. The error message describes the invalid object.

import orjson

try:
    val = orjson.dumps(...)
except TypeError:
    raise

Deserialize

def loads(obj: Union[bytes, str]) -> Union[dict, list, int, float, str]: ...

loads() deserializes JSON to Python objects.

It raises orjson.JSONDecodeError on invalid input.

import orjson

try:
    val = orjson.loads(...)
except orjson.JSONDecodeError:
    raise

Comparison

There are slight differences in output between libraries. The differences are not an issue for interoperability. Note orjson returns bytes. Its output is slightly smaller as well.

>>> import orjson, ujson, rapidjson, json
>>> data = {'bool': True, '🐈':'哈哈', 'int': 9223372036854775807, 'float': 1.337e+40}
>>> orjson.dumps(data)
b'{"bool":true,"\xf0\x9f\x90\x88":"\xe5\x93\x88\xe5\x93\x88","int":9223372036854775807,"float":1.337e40}'
>>> ujson.dumps(data)
'{"bool":true,"\\ud83d\\udc08":"\\u54c8\\u54c8","int":9223372036854775807,"float":1.337000000000000e+40}'
>>> rapidjson.dumps(data)
'{"bool":true,"\\uD83D\\uDC08":"\\u54C8\\u54C8","int":9223372036854775807,"float":1.337e+40}'
>>> json.dumps(data)
'{"bool": true, "\\ud83d\\udc08": "\\u54c8\\u54c8", "int": 9223372036854775807, "float": 1.337e+40}'

Testing

The library has comprehensive tests. There are unit tests against the roundtrip, jsonchecker, and fixtures files of the nativejson-benchmark repository. It is tested to not crash against the Big List of Naughty Strings. There are integration tests exercising the library's use in web servers (uwsgi and gunicorn, using multiprocess/forked workers) and when multithreaded. It also uses some tests from the ultrajson library.

Performance

Serialization performance of orjson is better than ultrajson, rapidjson, or json. Deserialization performance is worse to about the same as ultrajson.

alt text alt text alt text

canada.json deserialization

Library Median (milliseconds) Operations per second Relative (latency)
orjson 8.72 114.8 1.25
ujson 6.95 138.4 1
rapidjson 27.75 36 3.99
json 27.22 36.6 3.92

canada.json serialization

Library Median (milliseconds) Operations per second Relative (latency)
orjson 5.12 195.4 1
ujson 8.19 122.1 1.6
rapidjson 44.48 22.5 8.69
json 46.85 21.3 9.16

citm_catalog.json deserialization

Library Median (milliseconds) Operations per second Relative (latency)
orjson 6.06 164.9 1.12
ujson 5.4 185.2 1
rapidjson 7.26 137.6 1.35
json 7.49 132.2 1.39

citm_catalog.json serialization

Library Median (milliseconds) Operations per second Relative (latency)
orjson 1.02 980.6 1
ujson 2.53 394.4 2.49
rapidjson 2.37 421.9 2.33
json 5.32 188 5.22

twitter.json deserialization

Library Median (milliseconds) Operations per second Relative (latency)
orjson 2.98 335.3 1.25
ujson 2.39 419.3 1
rapidjson 3.12 318.8 1.31
json 3.12 318.8 1.31

twitter.json serialization

Library Median (milliseconds) Operations per second Relative (latency)
orjson 0.55 1815.1 1
ujson 1.46 684.9 2.65
rapidjson 1.55 643.9 2.82
json 2.18 458.7 3.95

This was measured using orjson 1.0.0 on Python 3.7.1. The above can be reproduced using the pybench and graph scripts.

License

orjson is dual licensed under the Apache 2.0 and MIT licenses. It contains code from the hyperjson and ultrajson libraries. It is implemented using the serde_json and pyo3 libraries.

Project details


Release history Release notifications | RSS feed

This version

1.1.0

Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distributions

No source distribution files available for this release.See tutorial on generating distribution archives.

Built Distributions

If you're not sure about the file name format, learn more about wheel file names.

orjson-1.1.0-cp37-cp37m-manylinux1_x86_64.whl (129.0 kB view details)

Uploaded CPython 3.7m

orjson-1.1.0-cp36-cp36m-manylinux1_x86_64.whl (129.1 kB view details)

Uploaded CPython 3.6m

orjson-1.1.0-cp35-cp35m-manylinux1_x86_64.whl (129.1 kB view details)

Uploaded CPython 3.5m

File details

Details for the file orjson-1.1.0-cp37-cp37m-manylinux1_x86_64.whl.

File metadata

  • Download URL: orjson-1.1.0-cp37-cp37m-manylinux1_x86_64.whl
  • Upload date:
  • Size: 129.0 kB
  • Tags: CPython 3.7m
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/1.12.1 pkginfo/1.4.2 requests/2.20.1 setuptools/40.5.0 requests-toolbelt/0.8.0 tqdm/4.28.1 CPython/3.7.1

File hashes

Hashes for orjson-1.1.0-cp37-cp37m-manylinux1_x86_64.whl
Algorithm Hash digest
SHA256 32830217b74420dde167e200f5a8c5a3009358d8093b2f754a8b8d3a13cff6bd
MD5 989be4c774a61373e387c29dea908718
BLAKE2b-256 2e9efd9ed15a3d63fbd35eb4dc243227998fbe00dc0a3c87167b9265bf4a8e45

See more details on using hashes here.

File details

Details for the file orjson-1.1.0-cp36-cp36m-manylinux1_x86_64.whl.

File metadata

  • Download URL: orjson-1.1.0-cp36-cp36m-manylinux1_x86_64.whl
  • Upload date:
  • Size: 129.1 kB
  • Tags: CPython 3.6m
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/1.12.1 pkginfo/1.4.2 requests/2.20.1 setuptools/40.5.0 requests-toolbelt/0.8.0 tqdm/4.28.1 CPython/3.6.7

File hashes

Hashes for orjson-1.1.0-cp36-cp36m-manylinux1_x86_64.whl
Algorithm Hash digest
SHA256 71fdf59963978472cce22a7aa9b793b1cc1f4844119ba67dfdbc159b8ded180d
MD5 baccfa2dee30753c22343566e0d7dfaa
BLAKE2b-256 31b921779d8b57e2cf4f7ec2402a0b22b77387eaad5dd0283323111f5c6e153e

See more details on using hashes here.

File details

Details for the file orjson-1.1.0-cp35-cp35m-manylinux1_x86_64.whl.

File metadata

  • Download URL: orjson-1.1.0-cp35-cp35m-manylinux1_x86_64.whl
  • Upload date:
  • Size: 129.1 kB
  • Tags: CPython 3.5m
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/1.12.1 pkginfo/1.4.2 requests/2.20.1 setuptools/40.0.0 requests-toolbelt/0.8.0 tqdm/4.28.1 CPython/3.5.6

File hashes

Hashes for orjson-1.1.0-cp35-cp35m-manylinux1_x86_64.whl
Algorithm Hash digest
SHA256 39e8e3849c547f5a5fe7f3ff07b037db1d5a8e6d565bd75c71d1221333755736
MD5 9fbe9cedbc0c6aeb940a54559ec96255
BLAKE2b-256 34a65d5d50049d0da044175a0c62ecb9fa2dd21067ff2ca1132c77b483bb5ef2

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page