Skip to main content

Python wrapper around rapidjson

Project description

Authors:

Ken Robbins <ken@kenrobbins.com>; Lele Gaifax <lele@metapensiero.it>

License:

MIT License

Status:

Build status Documentation status

RapidJSON is an extremely fast C++ JSON parser and serialization library: this module wraps it into a Python 3 extension, exposing its serialization/deserialization (to/from either bytes, str or file-like instances) and JSON Schema validation capabilities.

Latest version documentation is automatically rendered by Read the Docs.

Getting Started

First install python-rapidjson:

$ pip install python-rapidjson

or, if you prefer Conda:

$ conda install -c conda-forge python-rapidjson

Basic usage looks like this:

>>> import rapidjson
>>> data = {'foo': 100, 'bar': 'baz'}
>>> rapidjson.dumps(data)
'{"bar":"baz","foo":100}'
>>> rapidjson.loads('{"bar":"baz","foo":100}')
{'bar': 'baz', 'foo': 100}
>>>
>>> class Stream:
...   def write(self, data):
...      print("Chunk:", data)
...
>>> rapidjson.dump(data, Stream(), chunk_size=5)
Chunk: b'{"foo'
Chunk: b'":100'
Chunk: b',"bar'
Chunk: b'":"ba'
Chunk: b'z"}'

Development

If you want to install the development version (maybe to contribute fixes or enhancements) you may clone the repository:

$ git clone --recursive https://github.com/python-rapidjson/python-rapidjson.git

A set of makefiles implement most common operations, such as build, check and release; see make help output for a list of available targets.

Performance

python-rapidjson tries to be as performant as possible while staying compatible with the json module.

The following tables show a comparison between this module and other libraries with different data sets. Last row (“overall”) is the total time taken by all the benchmarks.

Each number show the factor between the time taken by each contender and python-rapidjson (in other words, they are normalized against a value of 1.0 for python-rapidjson): the lower the number, the speedier the contender.

In bold the winner.

Serialization

serialize

dumps()[1]

Encoder()[2]

dumps(n)[3]

Encoder(n)[4]

ujson[5]

simplejson[6]

stdlib[7]

yajl[8]

100 arrays dict

1.00

0.99

0.63

0.63

0.88

3.97

2.24

1.39

100 dicts array

1.00

1.04

0.73

0.70

0.87

5.79

2.31

1.36

256 Trues array

1.00

1.08

1.23

1.09

1.35

3.55

2.31

1.32

256 ascii array

1.00

1.01

1.04

1.03

0.51

1.17

1.04

1.12

256 doubles array

1.00

1.02

1.06

1.02

7.45

8.18

7.69

4.34

256 unicode array

1.00

0.88

0.88

0.88

0.55

0.74

0.67

0.51

complex object

1.00

1.02

0.81

0.79

1.13

3.98

2.84

2.21

composite object

1.00

1.00

0.69

0.65

0.98

3.12

1.99

2.00

overall

1.00

0.99

0.63

0.63

0.88

3.97

2.24

1.39

Deserialization

deserialize

loads()[9]

Decoder()[10]

loads(n)[11]

Decoder(n)[12]

ujson

simplejson

stdlib

yajl

100 arrays dict

1.00

1.00

0.90

0.90

0.96

1.40

1.12

1.15

100 dicts array

1.00

1.04

0.87

0.88

0.94

1.95

1.61

1.25

256 Trues array

1.00

1.24

1.17

1.25

1.16

2.09

1.87

1.86

256 ascii array

1.00

1.03

1.04

1.05

1.41

1.31

1.24

1.49

256 doubles array

1.00

1.01

0.19

0.19

0.44

0.91

0.85

0.56

256 unicode array

1.00

1.01

1.01

1.01

1.27

5.46

6.17

3.04

complex object

1.00

1.02

0.78

0.79

0.99

1.60

1.19

1.22

composite object

1.00

1.01

0.80

0.81

0.76

2.06

1.38

1.29

overall

1.00

1.00

0.90

0.90

0.96

1.41

1.13

1.15

DIY

To run these tests yourself, clone the repo and run:

$ make benchmarks

or

$ make benchmarks-other

The former will focus only on RapidJSON and is particularly handy coupled with the compare past runs functionality of pytest-benchmark:

$ make benchmarks PYTEST_OPTIONS=--benchmark-autosave
# hack, hack, hack!
$ make benchmarks PYTEST_OPTIONS=--benchmark-compare=0001

----------------------- benchmark 'deserialize': 18 tests ------------------------
Name (time in us)                                                            Min…
----------------------------------------------------------------------------------
test_loads[rapidjson-256 Trues array] (NOW)                         5.2320 (1.0)
test_loads[rapidjson-256 Trues array] (0001)                        5.4180 (1.04)

To reproduce the tables above run make benchmarks-tables

Incompatibility

Here are things in the standard json library supports that we have decided not to support:

separators argument

This is mostly used for pretty printing and not supported by RapidJSON so it isn’t a high priority. We do support indent kwarg that would get you nice looking JSON anyways.

Coercing keys when dumping

json will stringify a True dictionary key as "true" if you dump it out but when you load it back in it’ll still be a string. We want the dump and load to return the exact same objects so we have decided not to do this coercion.

Changes

0.4.2 (2018-01-09)

  • Fix precision handling of DM_UNIX_TIME timestamps

0.4.1 (2018-01-08)

  • Fix memory leaks in Decoder() and Encoder() classes, related to bad handling of PyObject_GetAttr() result value

  • Fix compatibility with Python 3.7a

0.4.0 (2018-01-05)

  • Implemented the streaming interface, see load() and dump() (issue #80)

    Backward incompatibility: now the flags arguments on all the functions are keyword only, to mimic stdlib’s json style

0.3.2 (2017-12-21)

0.3.1 (2017-12-20)

  • Fix Travis CI recipe to accomodate MacOS

0.3.0 (2017-12-20)

  • Fix compilation on MacOS (issue #78)

  • Handle generic iterables (PR #89)

    Backward incompatibility: the dumps() function and the Encoder() constructor used to accept a max_recursion_depth argument, to control the maximum allowed nesting of Python structures; since the underlying function is now effectively recursive, it has been replaced by the generic sys.setrecursionlimit() mechanism

0.2.7 (2017-12-08)

  • Restore compatibility with Python < 3.6

0.2.6 (2017-12-08)

  • Fix memory leaks when using object_hook/start_object/end_object

0.2.5 (2017-09-30)

  • Fix bug where error handling code could raise an exception causing a confusing exception to be returned (PR #82)

  • Fix bug where loads’s object_hook and dumps’s default arguments could not be passed None explicitly (PR #83)

  • Fix crash when dealing with surrogate pairs (issue #81)

0.2.4 (2017-09-17)

  • Fix compatibility with MacOS/clang

0.2.3 (2017-08-24)

  • Limit the precision of DM_UNIX_TIME timestamps to six decimal digits

0.2.2 (2017-08-24)

  • Nothing new, attempt to fix production of Python 3.6 binary wheels

0.2.1 (2017-08-24)

  • Nothing new, attempt to fix production of Python 3.6 binary wheels

0.2.0 (2017-08-24)

  • New parse_mode option, implementing relaxed JSON syntax (issue #73)

  • New Encoder and Decoder, implementing a class-based interface

  • New Validator, exposing the underlying JSON schema validation (issue #71)

0.1.0 (2017-08-16)

  • Remove beta status

0.1.0b4 (2017-08-14)

  • Make execution of the test suite on Appveyor actually happen

0.1.0b3 (2017-08-12)

  • Exclude CI configurations from the source distribution

0.1.0b2 (2017-08-12)

  • Fix Powershell wheel upload script in appveyor configuration

0.1.0b1 (2017-08-12)

  • Compilable with somewhat old g++ (issue #69)

  • Backward incompatibilities:

    • all DATETIME_MODE_XXX constants have been shortened to DM_XXX DATETIME_MODE_ISO8601_UTC has been renamed to DM_SHIFT_TO_UTC

    • all UUID_MODE_XXX constants have been shortened to UM_XXX

  • New option DM_UNIX_TIME to serialize date, datetime and time values as UNIX timestamps targeting issue #61

  • New option DM_NAIVE_IS_UTC to treat naïve datetime and time values as if they were in the UTC timezone (also for issue #61)

  • New keyword argument number_mode to use underlying C library numbers

  • Binary wheels for GNU/Linux and Windows on PyPI (one would hope: this is the reason for the beta1 release)

0.0.11 (2017-03-05)

  • Fix a couple of refcount handling glitches, hopefully targeting issue #48.

0.0.10 (2017-03-02)

  • Fix source distribution to contain all required stuff (PR #64)

0.0.9 (2017-03-02)

0.0.8 (2016-12-09)

Project details


Download files

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

Source Distribution

python-rapidjson-0.4.2.tar.gz (180.0 kB view details)

Uploaded Source

Built Distributions

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

python_rapidjson-0.4.2-cp36-cp36m-win_amd64.whl (120.1 kB view details)

Uploaded CPython 3.6mWindows x86-64

python_rapidjson-0.4.2-cp36-cp36m-win32.whl (104.7 kB view details)

Uploaded CPython 3.6mWindows x86

python_rapidjson-0.4.2-cp36-cp36m-manylinux1_x86_64.whl (1.1 MB view details)

Uploaded CPython 3.6m

python_rapidjson-0.4.2-cp36-cp36m-manylinux1_i686.whl (1.0 MB view details)

Uploaded CPython 3.6m

python_rapidjson-0.4.2-cp36-cp36m-macosx_10_6_intel.whl (353.5 kB view details)

Uploaded CPython 3.6mmacOS 10.6+ Intel (x86-64, i386)

python_rapidjson-0.4.2-cp35-cp35m-win_amd64.whl (120.1 kB view details)

Uploaded CPython 3.5mWindows x86-64

python_rapidjson-0.4.2-cp35-cp35m-win32.whl (104.7 kB view details)

Uploaded CPython 3.5mWindows x86

python_rapidjson-0.4.2-cp35-cp35m-manylinux1_x86_64.whl (1.1 MB view details)

Uploaded CPython 3.5m

python_rapidjson-0.4.2-cp35-cp35m-manylinux1_i686.whl (1.0 MB view details)

Uploaded CPython 3.5m

python_rapidjson-0.4.2-cp35-cp35m-macosx_10_6_intel.whl (353.5 kB view details)

Uploaded CPython 3.5mmacOS 10.6+ Intel (x86-64, i386)

python_rapidjson-0.4.2-cp34-cp34m-win_amd64.whl (113.2 kB view details)

Uploaded CPython 3.4mWindows x86-64

python_rapidjson-0.4.2-cp34-cp34m-win32.whl (102.7 kB view details)

Uploaded CPython 3.4mWindows x86

python_rapidjson-0.4.2-cp34-cp34m-manylinux1_x86_64.whl (1.1 MB view details)

Uploaded CPython 3.4m

python_rapidjson-0.4.2-cp34-cp34m-manylinux1_i686.whl (1.1 MB view details)

Uploaded CPython 3.4m

python_rapidjson-0.4.2-cp34-cp34m-macosx_10_6_intel.whl (351.9 kB view details)

Uploaded CPython 3.4mmacOS 10.6+ Intel (x86-64, i386)

File details

Details for the file python-rapidjson-0.4.2.tar.gz.

File metadata

File hashes

Hashes for python-rapidjson-0.4.2.tar.gz
Algorithm Hash digest
SHA256 cbdabf7369d67865024c69c546fb7733ba275c10fd3971c35477d96cb77a3d72
MD5 badfd01e954e8ea9b9359fa71ec2d00d
BLAKE2b-256 8b0f58f554a7ae16cc72b737993be71422f2f9c952eb3f8765e8a25ff394df15

See more details on using hashes here.

File details

Details for the file python_rapidjson-0.4.2-cp36-cp36m-win_amd64.whl.

File metadata

File hashes

Hashes for python_rapidjson-0.4.2-cp36-cp36m-win_amd64.whl
Algorithm Hash digest
SHA256 6e5a1abce6ce9aad419ef14743bfa42ce6f607ab266fbabfb09bc691a97def35
MD5 fd22ae2d45568f4ac6fc0ec51d8a3e97
BLAKE2b-256 6fd3f488eaf0b8fabd13a818acadd393f9b91459d8adc2eb58426a6c0618df25

See more details on using hashes here.

File details

Details for the file python_rapidjson-0.4.2-cp36-cp36m-win32.whl.

File metadata

File hashes

Hashes for python_rapidjson-0.4.2-cp36-cp36m-win32.whl
Algorithm Hash digest
SHA256 ff2a12c701daf63ec3ca106ef232749c726bc33d4c663ca91b61c063f2b1b2c8
MD5 8f2345b8bc7b56f4b5f59a24b0baf449
BLAKE2b-256 c31ff582fead530193a0ccc8e6ea859655e43dda0bd564745be204808bc40d82

See more details on using hashes here.

File details

Details for the file python_rapidjson-0.4.2-cp36-cp36m-manylinux1_x86_64.whl.

File metadata

File hashes

Hashes for python_rapidjson-0.4.2-cp36-cp36m-manylinux1_x86_64.whl
Algorithm Hash digest
SHA256 b978fc3669c1aa6814c8e94872b5b4dba938e35d0696db6de181d677d2c6c79f
MD5 6399af8da53ef8006ead01dec6d0448f
BLAKE2b-256 8ee3e490a367c790aa705d48ddbee904eb1ebb0d223e887ec269a07b55c12710

See more details on using hashes here.

File details

Details for the file python_rapidjson-0.4.2-cp36-cp36m-manylinux1_i686.whl.

File metadata

File hashes

Hashes for python_rapidjson-0.4.2-cp36-cp36m-manylinux1_i686.whl
Algorithm Hash digest
SHA256 8dfbe773856c61ef50b6a84e823d016a8495da52f66d513b69d278d01d977639
MD5 90378077c62dc4855effc17106134230
BLAKE2b-256 f5240a18fdc76f0626053db4bcb297371a8f5ebb74c0826e9e32f105c8534773

See more details on using hashes here.

File details

Details for the file python_rapidjson-0.4.2-cp36-cp36m-macosx_10_6_intel.whl.

File metadata

File hashes

Hashes for python_rapidjson-0.4.2-cp36-cp36m-macosx_10_6_intel.whl
Algorithm Hash digest
SHA256 729eedd3cecc4c3654142e7e323818fcc092572fb44f2ad619dbe2e7c8b7aad7
MD5 5aa9c8a5bb222b897a686d7b664b67ff
BLAKE2b-256 fbf21e231e35696fa77397cf49e012e39e9b0d491cfb3f06171a23ed22eaaa88

See more details on using hashes here.

File details

Details for the file python_rapidjson-0.4.2-cp35-cp35m-win_amd64.whl.

File metadata

File hashes

Hashes for python_rapidjson-0.4.2-cp35-cp35m-win_amd64.whl
Algorithm Hash digest
SHA256 fe9745fd6751f749c62c792fa16f0aa136afdba93536d82e66b7ddc3520769da
MD5 115df1aa4f05d20b4ac0552294697f00
BLAKE2b-256 e5095c9d72dd6f296939e1bc637f4425a07084593e3cd3b226f31212c7a73371

See more details on using hashes here.

File details

Details for the file python_rapidjson-0.4.2-cp35-cp35m-win32.whl.

File metadata

File hashes

Hashes for python_rapidjson-0.4.2-cp35-cp35m-win32.whl
Algorithm Hash digest
SHA256 4a7c80c345531dfab43ff42a48f731063f78d69ec8ebc3c5d2bab7f329e07cf4
MD5 b6922d5a7a6e650e51d3cdcd2aa1c6fc
BLAKE2b-256 73f6f13c5cbdc45a878b0662e4e963cea31b07b93e7d06a85b5b09f05d4f56b6

See more details on using hashes here.

File details

Details for the file python_rapidjson-0.4.2-cp35-cp35m-manylinux1_x86_64.whl.

File metadata

File hashes

Hashes for python_rapidjson-0.4.2-cp35-cp35m-manylinux1_x86_64.whl
Algorithm Hash digest
SHA256 6566fe945c9cafe30be2958e8643528ed1f5608a4b316e4218d02a9e9bfeed0b
MD5 df85001f31b3c608b4e72a5636a47c75
BLAKE2b-256 7f350c07e90a05d576a2689af6bffaf40c8e0f64f18bf398163ac66af342e200

See more details on using hashes here.

File details

Details for the file python_rapidjson-0.4.2-cp35-cp35m-manylinux1_i686.whl.

File metadata

File hashes

Hashes for python_rapidjson-0.4.2-cp35-cp35m-manylinux1_i686.whl
Algorithm Hash digest
SHA256 e0f9d3e871333d5336b142e65bedb2dff4639a81c689051f5ffb0886e3601d03
MD5 ecbb404446ffa9980e8b72900710bd21
BLAKE2b-256 24d87e7f81b9726f8ce46cdb5f6c0bf6f4c19a68ef98240f2e7975436019bafd

See more details on using hashes here.

File details

Details for the file python_rapidjson-0.4.2-cp35-cp35m-macosx_10_6_intel.whl.

File metadata

File hashes

Hashes for python_rapidjson-0.4.2-cp35-cp35m-macosx_10_6_intel.whl
Algorithm Hash digest
SHA256 974230e96ddfc00fd163a7e78d7bf69336fbe1ff715fa3b9a5a2683f94d5b8e5
MD5 a9d6aa51ad6355fe1a7af6187bc1a8cc
BLAKE2b-256 f5e6509e2cae9b7dbaa095f3c9154adbdc535e1b3fc10a684cd1fa08174c3e9c

See more details on using hashes here.

File details

Details for the file python_rapidjson-0.4.2-cp34-cp34m-win_amd64.whl.

File metadata

File hashes

Hashes for python_rapidjson-0.4.2-cp34-cp34m-win_amd64.whl
Algorithm Hash digest
SHA256 47374baec2ff65b285eea6c6a827a5b7da745f94c24e5ebe604e2570f8aa1ba5
MD5 c2f0034ccc9672c576a1fd2a6d6677fc
BLAKE2b-256 d35e77d7107320f53ba91cf8f08f07684b6502ebfc6540549ca4b1d4b074215f

See more details on using hashes here.

File details

Details for the file python_rapidjson-0.4.2-cp34-cp34m-win32.whl.

File metadata

File hashes

Hashes for python_rapidjson-0.4.2-cp34-cp34m-win32.whl
Algorithm Hash digest
SHA256 2d423b149db0595c1cf6751e11070b96a4dc480a07bc56fe6d61b0054c654e58
MD5 fc8153e0d8a223a954f090908ce00e1e
BLAKE2b-256 cf7de2649f0f01b8fe84384a6331f28f720b1ff65c6e61526a5647615f0ec3f0

See more details on using hashes here.

File details

Details for the file python_rapidjson-0.4.2-cp34-cp34m-manylinux1_x86_64.whl.

File metadata

File hashes

Hashes for python_rapidjson-0.4.2-cp34-cp34m-manylinux1_x86_64.whl
Algorithm Hash digest
SHA256 41abd59f99875f35be0b92dc32b4ab3bf881e872e95c244b1643092d128d969e
MD5 79a8287cf4dea9dfe9e61554af9eeb95
BLAKE2b-256 b674b4343db05909b584faf0dfff1278b6da8b3be48baf8b4dedb66ab593415b

See more details on using hashes here.

File details

Details for the file python_rapidjson-0.4.2-cp34-cp34m-manylinux1_i686.whl.

File metadata

File hashes

Hashes for python_rapidjson-0.4.2-cp34-cp34m-manylinux1_i686.whl
Algorithm Hash digest
SHA256 6dd780fbcb777814359323d71f95fc13ec05685759fc95c3511e9cacb682a9dc
MD5 6fe6e265758ec22780bf4678268b503c
BLAKE2b-256 66f465767d5cc3971226b36af033428d32b3992f45e27ae986d9fdd64e5c66e6

See more details on using hashes here.

File details

Details for the file python_rapidjson-0.4.2-cp34-cp34m-macosx_10_6_intel.whl.

File metadata

File hashes

Hashes for python_rapidjson-0.4.2-cp34-cp34m-macosx_10_6_intel.whl
Algorithm Hash digest
SHA256 5893bf89dad5b39a0fd4e99ce358a92caa39c84e896957f0a099f5945aefb7cb
MD5 f45e82f3e062c6cf818fff4a580431e4
BLAKE2b-256 449fdc2627540c1904cbcebfc0c5b92a0f08e46263a6f3621a9e3e737be55d8f

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