RapidJSON is an extremely fast C++ JSON serialization library.
We do not support legacy Python versions, you will need to upgrade to Python 3 to use this library.
Latest version documentation is automatically rendered by Read the Docs.
First install python-rapidjson
:
$ pip install python-rapidjson
RapidJSON tries to be compatible with the standard library json
module so it should be a drop in replacement. 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}
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
Note
The --recursive
option is needed because we use a submodule to include RapidJSON sources. Alternatively you can do a plain clone
immediately followed by a git submodule update --init
.
Alternatively, if you already have (a compatible version of) RapidJSON includes around, you can compile the module specifying their location with the option --rj-include-dir
, for example:
$ python3 setup.py build --rj-include-dir=/usr/include/rapidjson
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.
serialize | ujson | simplejson | json | yajl |
---|---|---|---|---|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
256 doubles array |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
deserialize | ujson | simplejson | json | yajl |
---|---|---|---|---|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
To run these tests yourself, clone the repo and run:
$ tox -e py34 -- -m benchmark --compare-other-engines
Without the option --compare-other-engines
it will focus only on RapidJSON
. This is particularly handy coupled with the compare past runs functionality of pytest-benchmark
:
$ tox -e py34 -- -m benchmark --benchmark-autosave
# hack, hack, hack!
$ tox -e py34 -- -m benchmark --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, use the option --benchmark-json
so that the the results are written in the specified filename the run the benchmark-tables.py
script giving that filename as the only argument:
$ tox -e py36 -- -m benchmark --compare-other-engines --benchmark-json=comparison.json
$ python3 benchmark-tables.py comparison.json
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 byRapidJSON
so it isn't a high priority. We do supportindent
kwarg that would get you nice looking JSON anyways.- Coercing keys when dumping.
json
will turnTrue
into'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 coercing.