Skip to main content

Fast, correct Python JSON library

Project description

orjson

orjson is a fast, correct JSON library for Python. It benchmarks as the fastest Python library for JSON and has comprehensive unit, integration, and interoperability tests.

Its serialization performance is 2x to 3x the nearest other library and 4.5x to 11.5x the standard library. Its deserialization performance is 0.95x to 1.1x the nearest other library and 1.2x to 3x the standard library.

It differs in behavior from other Python JSON libraries in supporting datetimes, not supporting subclasses without a default hook, serializing UTF-8 to bytes rather than escaped ASCII (e.g., "好" rather than "\\u597d") by default, having strict UTF-8 conformance, having strict JSON conformance on NaN/Infinity/-Infinity, having an option for strict JSON conformance on 53-bit integers, not supporting pretty printing, and not supporting all standard library options.

It supports CPython 3.6 and 3.7 and distributes wheels for Linux, macOS, and Windows. The repository and issue tracker is github.com/ijl/orjson.

Usage

Install

To install a wheel from PyPI:

pip install --upgrade orjson

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

git clone https://github.com/ijl/orjson.git && cd orjson
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: Any, default: Optional[Callable[[Any], Any]], option: Optional[int]) -> bytes: ...

dumps() serializes Python objects to JSON.

It natively serializes str, dict, list, tuple, int, float, datetime.datetime, datetime.date, datetime.time, and None instances. It supports arbitrary types through default. It does not serialize subclasses of supported types natively, but default may be used.

It accepts options via an option keyword argument. These include:

  • orjson.OPT_STRICT_INTEGER for enforcing a 53-bit limit on integers. The limit is otherwise 64 bits, the same as the Python standard library.
  • orjson.OPT_NAIVE_UTC for assuming datetime.datetime objects without a tzinfo are UTC.

To specify multiple options, mask them together, e.g., option=orjson.OPT_STRICT_INTEGER | orjson.OPT_NAIVE_UTC.

It raises JSONEncodeError on an unsupported type. This exception message describes the invalid object.

It raises JSONEncodeError on a str that contains invalid UTF-8.

It raises JSONEncodeError on an integer that exceeds 64 bits by default or, with OPT_STRICT_INTEGER, 53 bits.

It raises JSONEncodeError if a dict has a key of a type other than str.

It raises JSONEncodeError if the output of default recurses to handling by default more than five levels deep.

It raises JSONEncodeError if a tzinfo on a datetime object is incorrect.

JSONEncodeError is a subclass of TypeError. This is for compatibility with the standard library.

import orjson

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

To serialize arbitrary types, specify default as a callable that returns a supported type. default may be a function, lambda, or callable class instance.

>>> import orjson, numpy
>>> def default(obj):
        if isinstance(obj, numpy.ndarray):
            return obj.tolist()
>>> orjson.dumps(numpy.random.rand(2, 2), default=default)
b'[[0.08423896597867486,0.854121264944197],[0.8452845446981371,0.19227780743524303]]'

If the default callable does not return an object, and an exception was raised within the default function, an exception describing this is raised. If no object is returned by the default callable but also no exception was raised, it falls through to raising JSONEncodeError on an unsupported type.

The default callable may return an object that itself must be handled by default up to five levels deep before an exception is raised.

Deserialize

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

loads() deserializes JSON to Python objects.

It raises JSONDecodeError if given an invalid type or invalid JSON. This includes if the input contains NaN, Infinity, or -Infinity, which the standard library allows, but is not valid JSON.

JSONDecodeError is a subclass of ValueError. This is for compatibility with the standard library.

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}'

datetime

orjson serializes datetime.datetime objects to RFC 3339 format, a subset of ISO 8601.

datetime.datetime objects must have tzinfo set. For UTC timezones, datetime.timezone.utc is sufficient. For other timezones, tzinfo must be a timezone object from the pendulum, pytz, or dateutil libraries. For applications in which naive datetimes are known to be UTC, tzinfo may be omitted if orjson.OPT_NAIVE_UTC if specified. This does not affect datetimes with a tzinfo set.

>>> import orjson, datetime, pendulum
>>> orjson.dumps(
    datetime.datetime.fromtimestamp(4123518902), option=orjson.OPT_NAIVE_UTC
)
b'"2100-09-01T21:55:02+00:00"'
>>> orjson.dumps(
    datetime.datetime.fromtimestamp(4123518902).replace(tzinfo=datetime.timezone.utc)
)
b'"2100-09-01T21:55:02+00:00"'
>>> orjson.dumps(
    datetime.datetime(2018, 12, 1, 2, 3, 4, 9, tzinfo=pendulum.timezone('Australia/Adelaide'))
)
b'"2018-12-01T02:03:04.9+10:30"'

datetime.time objects must not have a tzinfo. datetime.date objects will always serialize.

>>> import orjson, datetime
>>> orjson.dumps(datetime.date(1900, 1, 2))
b'"1900-01-02"'
>>> orjson.dumps(datetime.time(12, 0, 15, 291290))
b'"12:00:15.291290"'

Errors with tzinfo result in JSONEncodeError being raised.

It is faster to have orjson serialize datetime objects than to do so before calling dumps(). If using an unsupported type such as pendulum.datetime, use default.

int

JSON only requires that implementations accept integers with 53-bit precision. orjson will, by default, serialize 64-bit integers. This is compatible with the Python standard library and other non-browser implementations. For transmitting JSON to a web browser or other strict implementations, dumps() can be configured to raise a JSONEncodeError on values exceeding the 53-bit range.

>>> import orjson
>>> orjson.dumps(9007199254740992)
b'9007199254740992'
>>> orjson.dumps(9007199254740992, option=orjson.OPT_STRICT_INTEGER)
JSONEncodeError: Integer exceeds 53-bit max
>>> orjson.dumps(-9007199254740992, option=orjson.OPT_STRICT_INTEGER)
JSONEncodeError: Integer exceeds 53-bit max

UTF-8

orjson raises an exception on invalid UTF-8. This is necessary because Python 3 str objects may contain UTF-16 surrogates. The standard library's json module accepts invalid UTF-8.

>>> import orjson, ujson, rapidjson, json
>>> orjson.dumps('\ud800')
JSONEncodeError: str is not valid UTF-8: surrogates not allowed
>>> ujson.dumps('\ud800')
UnicodeEncodeError: 'utf-8' codec ...
>>> rapidjson.dumps('\ud800')
UnicodeEncodeError: 'utf-8' codec ...
>>> json.dumps('\ud800')
'"\\ud800"'
>>> import orjson, ujson, rapidjson, json
>>> orjson.loads('"\\ud800"')
JSONDecodeError: unexpected end of hex escape at line 1 column 8: line 1 column 1 (char 0)
>>> ujson.loads('"\\ud800"')
''
>>> rapidjson.loads('"\\ud800"')
ValueError: Parse error at offset 1: The surrogate pair in string is invalid.
>>> json.loads('"\\ud800"')
'\ud800'

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. It is tested to not leak memory. It is tested to be correct against input from the PyJFuzz JSON fuzzer. It is tested to not crash against and not accept invalid UTF-8. There are integration tests exercising the library's use in web servers (gunicorn using multiprocess/forked workers) and when multithreaded. It also uses some tests from the ultrajson library.

Performance

Serialization and deserialization performance of orjson is better than ultrajson, rapidjson, simplejson, or json. The benchmarks are done on fixtures of real data:

  • twitter.json, 631.5KiB, results of a search on Twitter for "一", containing CJK strings, dictionaries of strings and arrays of dictionaries, indented.

  • github.json, 55.8KiB, a GitHub activity feed, containing dictionaries of strings and arrays of dictionaries, not indented.

  • citm_catalog.json, 1.7MiB, concert data, containing nested dictionaries of strings and arrays of integers, indented.

  • canada.json, 2.2MiB, coordinates of the Canadian border in GeoJSON format, containing floats and arrays, indented.

alt text alt text alt text alt text alt text alt text alt text alt text

twitter.json serialization

Library Median latency (milliseconds) Operations per second Relative (latency)
orjson 0.49 2034.9 1
ujson 1.42 703.7 2.89
rapidjson 1.57 638.5 3.19
simplejson 2.79 358 5.69
json 2.7 370.3 5.5

twitter.json deserialization

Library Median latency (milliseconds) Operations per second Relative (latency)
orjson 2.4 416.4 1
ujson 2.25 444.4 0.94
rapidjson 3.04 328.4 1.27
simplejson 2.49 401.8 1.04
json 2.82 354.3 1.17

github.json serialization

Library Median latency (milliseconds) Operations per second Relative (latency)
orjson 0.05 18299.4 1
ujson 0.14 7126.2 2.57
rapidjson 0.16 6370.2 2.87
simplejson 0.31 3274.2 5.59
json 0.27 3713 4.93

github.json deserialization

Library Median latency (milliseconds) Operations per second Relative (latency)
orjson 0.21 4873.7 1
ujson 0.22 4493.4 1.08
rapidjson 0.27 3721.3 1.31
simplejson 0.22 4593.9 1.06
json 0.24 4142.5 1.18

citm_catalog.json serialization

Library Median latency (milliseconds) Operations per second Relative (latency)
orjson 0.81 1223.7 1
ujson 2.66 376 3.27
rapidjson 2.45 408.7 3.01
simplejson 11 91 13.53
json 6.03 165.8 7.42

citm_catalog.json deserialization

Library Median latency (milliseconds) Operations per second Relative (latency)
orjson 4.6 217 1
ujson 4.49 222.8 0.98
rapidjson 5.68 175.8 1.24
simplejson 6.06 165.2 1.32
json 5.99 166.8 1.3

canada.json serialization

Library Median latency (milliseconds) Operations per second Relative (latency)
orjson 4.29 232.4 1
ujson 8.71 115.7 2.03
rapidjson 43.69 22.9 10.18
simplejson 66.56 15 15.51
json 49.27 19.9 11.48

canada.json deserialization

Library Median latency (milliseconds) Operations per second Relative (latency)
orjson 8.63 116.4 1
ujson 8.55 116.9 0.99
rapidjson 29.37 34.1 3.4
simplejson 26.56 37.6 3.08
json 27.56 36.3 3.19

This was measured using Python 3.7.2 on Linux with orjson 2.0.0, ujson 1.35, python-rapidson 0.6.3, and simplejson 3.16.0.

The results can be reproduced using the pybench and graph scripts.

License

orjson is dual licensed under the Apache 2.0 and MIT licenses. It contains tests 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

2.0.1

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-2.0.1-cp37-none-win_amd64.whl (116.3 kB view details)

Uploaded CPython 3.7Windows x86-64

orjson-2.0.1-cp37-cp37m-manylinux1_x86_64.whl (143.9 kB view details)

Uploaded CPython 3.7m

orjson-2.0.1-cp37-cp37m-macosx_10_7_x86_64.whl (135.9 kB view details)

Uploaded CPython 3.7mmacOS 10.7+ x86-64

orjson-2.0.1-cp36-none-win_amd64.whl (116.4 kB view details)

Uploaded CPython 3.6Windows x86-64

orjson-2.0.1-cp36-cp36m-manylinux1_x86_64.whl (144.0 kB view details)

Uploaded CPython 3.6m

orjson-2.0.1-cp36-cp36m-macosx_10_7_x86_64.whl (135.9 kB view details)

Uploaded CPython 3.6mmacOS 10.7+ x86-64

orjson-2.0.1-cp35-none-win_amd64.whl (117.5 kB view details)

Uploaded CPython 3.5Windows x86-64

orjson-2.0.1-cp35-cp35m-manylinux1_x86_64.whl (144.4 kB view details)

Uploaded CPython 3.5m

orjson-2.0.1-cp35-cp35m-macosx_10_7_x86_64.whl (136.7 kB view details)

Uploaded CPython 3.5mmacOS 10.7+ x86-64

File details

Details for the file orjson-2.0.1-cp37-none-win_amd64.whl.

File metadata

  • Download URL: orjson-2.0.1-cp37-none-win_amd64.whl
  • Upload date:
  • Size: 116.3 kB
  • Tags: CPython 3.7, Windows x86-64
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/1.12.1 pkginfo/1.5.0.1 requests/2.21.0 setuptools/39.0.1 requests-toolbelt/0.9.1 tqdm/4.30.0 CPython/3.7.0

File hashes

Hashes for orjson-2.0.1-cp37-none-win_amd64.whl
Algorithm Hash digest
SHA256 58ff040fe8cb1c491c95908bc7157c48d523e601a60b51d52ddf8dce1c3a9028
MD5 fae93fb4a99fb11413a1397b6e677860
BLAKE2b-256 d86c39099a92a85be70be60393b6771c4111f91ac0ec448b49b673368b61c338

See more details on using hashes here.

File details

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

File metadata

  • Download URL: orjson-2.0.1-cp37-cp37m-manylinux1_x86_64.whl
  • Upload date:
  • Size: 143.9 kB
  • Tags: CPython 3.7m
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/1.12.1 pkginfo/1.5.0.1 requests/2.21.0 setuptools/39.0.1 requests-toolbelt/0.9.1 tqdm/4.30.0 CPython/3.7.0

File hashes

Hashes for orjson-2.0.1-cp37-cp37m-manylinux1_x86_64.whl
Algorithm Hash digest
SHA256 13bcf0431e170766e7e5746738cf4d15cbe3429a6f23fc42a2178730c228222f
MD5 508f5c1cb3d4c10737e4ad6e85b5bceb
BLAKE2b-256 e1a665acf160d3d4ee4631f0e88aca36e44ec2e54fd07ee6f2f0fb09f2fcc92c

See more details on using hashes here.

File details

Details for the file orjson-2.0.1-cp37-cp37m-macosx_10_7_x86_64.whl.

File metadata

  • Download URL: orjson-2.0.1-cp37-cp37m-macosx_10_7_x86_64.whl
  • Upload date:
  • Size: 135.9 kB
  • Tags: CPython 3.7m, macOS 10.7+ x86-64
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/1.12.1 pkginfo/1.5.0.1 requests/2.21.0 setuptools/39.0.1 requests-toolbelt/0.9.1 tqdm/4.30.0 CPython/3.7.0

File hashes

Hashes for orjson-2.0.1-cp37-cp37m-macosx_10_7_x86_64.whl
Algorithm Hash digest
SHA256 549ea724e664d7d9a901f6b2991db4126879ab97b6a01010ba6e1cc607ea59b0
MD5 aac978e6fa3645066293f89fe773461b
BLAKE2b-256 24a4c438197128c38f8175752d5b391049241241909310a99094c2e0a4775242

See more details on using hashes here.

File details

Details for the file orjson-2.0.1-cp36-none-win_amd64.whl.

File metadata

  • Download URL: orjson-2.0.1-cp36-none-win_amd64.whl
  • Upload date:
  • Size: 116.4 kB
  • Tags: CPython 3.6, Windows x86-64
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/1.12.1 pkginfo/1.5.0.1 requests/2.21.0 setuptools/28.8.0 requests-toolbelt/0.9.1 tqdm/4.30.0 CPython/3.6.4

File hashes

Hashes for orjson-2.0.1-cp36-none-win_amd64.whl
Algorithm Hash digest
SHA256 eeba26ff75fedb0ba2b5bc3f6ab6e3f5cb162e62fdcf47b2f3a37b4d0a29b3ad
MD5 ad956885b5136b1fbb9d032e630352ba
BLAKE2b-256 68d4233a9d0def0e9e21dde7c1fa3320a69d353334e3f446de427e56ad11c305

See more details on using hashes here.

File details

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

File metadata

  • Download URL: orjson-2.0.1-cp36-cp36m-manylinux1_x86_64.whl
  • Upload date:
  • Size: 144.0 kB
  • Tags: CPython 3.6m
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/1.12.1 pkginfo/1.5.0.1 requests/2.21.0 setuptools/39.0.1 requests-toolbelt/0.9.1 tqdm/4.30.0 CPython/3.6.5

File hashes

Hashes for orjson-2.0.1-cp36-cp36m-manylinux1_x86_64.whl
Algorithm Hash digest
SHA256 efd5db16b2425c1f95898d0218a37d598392361b4cff08529ba98bb426fef36f
MD5 bedda78bb58a08416d452664884ba020
BLAKE2b-256 853f2193c4b1808bb56377f6c1226bbdd4c140442beffe7bec3806c27acfb9ff

See more details on using hashes here.

File details

Details for the file orjson-2.0.1-cp36-cp36m-macosx_10_7_x86_64.whl.

File metadata

  • Download URL: orjson-2.0.1-cp36-cp36m-macosx_10_7_x86_64.whl
  • Upload date:
  • Size: 135.9 kB
  • Tags: CPython 3.6m, macOS 10.7+ x86-64
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/1.12.1 pkginfo/1.5.0.1 requests/2.21.0 setuptools/39.2.0 requests-toolbelt/0.9.1 tqdm/4.30.0 CPython/3.6.5

File hashes

Hashes for orjson-2.0.1-cp36-cp36m-macosx_10_7_x86_64.whl
Algorithm Hash digest
SHA256 3795f59b5cc861de5c8031a8d13e2cd0dd940984f16b4f9a5128fb25b0947a57
MD5 2374b943098b659f8b1f19666541408d
BLAKE2b-256 c30f3705d47bccefc6c28323e1f14f799657c7e9311a5d951f591ff17e0c26fe

See more details on using hashes here.

File details

Details for the file orjson-2.0.1-cp35-none-win_amd64.whl.

File metadata

  • Download URL: orjson-2.0.1-cp35-none-win_amd64.whl
  • Upload date:
  • Size: 117.5 kB
  • Tags: CPython 3.5, Windows x86-64
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/1.13.0 pkginfo/1.5.0.1 requests/2.21.0 setuptools/28.8.0 requests-toolbelt/0.9.1 tqdm/4.31.1 CPython/3.5.4

File hashes

Hashes for orjson-2.0.1-cp35-none-win_amd64.whl
Algorithm Hash digest
SHA256 979eafe91aaaa016c482473f0922794d4d1daee8035a056b0fd939cf945e2c5a
MD5 ce9f0758b77685d6bc069c91ad185d2e
BLAKE2b-256 85cd4ffc3b23a2741b00af013db2bf9ea5a786934718bfcdcb165337f73237a3

See more details on using hashes here.

File details

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

File metadata

  • Download URL: orjson-2.0.1-cp35-cp35m-manylinux1_x86_64.whl
  • Upload date:
  • Size: 144.4 kB
  • Tags: CPython 3.5m
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/1.13.0 pkginfo/1.5.0.1 requests/2.21.0 setuptools/28.8.0 requests-toolbelt/0.9.1 tqdm/4.31.1 CPython/3.5.5

File hashes

Hashes for orjson-2.0.1-cp35-cp35m-manylinux1_x86_64.whl
Algorithm Hash digest
SHA256 c813c65be50d5732d3562a698aa44edf577ee7b2352a6e61ab990a5a31adb23d
MD5 f108c5cfcda5b4efcd1b1e44116aeea3
BLAKE2b-256 f8e6030a0cf973691e3225dddbe536522e718999111c553c9e6b2b48fc968bde

See more details on using hashes here.

File details

Details for the file orjson-2.0.1-cp35-cp35m-macosx_10_7_x86_64.whl.

File metadata

  • Download URL: orjson-2.0.1-cp35-cp35m-macosx_10_7_x86_64.whl
  • Upload date:
  • Size: 136.7 kB
  • Tags: CPython 3.5m, macOS 10.7+ x86-64
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/1.13.0 pkginfo/1.5.0.1 requests/2.21.0 setuptools/28.8.0 requests-toolbelt/0.9.1 tqdm/4.31.1 CPython/3.5.5

File hashes

Hashes for orjson-2.0.1-cp35-cp35m-macosx_10_7_x86_64.whl
Algorithm Hash digest
SHA256 4152a6ea29a8900c7feb94e18bffac764ff21b7b06a45c2699d9b5ac4cfb9781
MD5 809d07a2e1e2f450ccc814ac32fe3edb
BLAKE2b-256 d7426ecfec9c48584905e213147ab876b5918e46d892c6e639e2baf617e07b44

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