Skip to content

JSON encoder and decoder for python written in C/C++

License

Notifications You must be signed in to change notification settings

MarkReedZ/mrjson

Repository files navigation

MrJSON

MrJSON is a JSON encoder and decoder written in C/C++ with bindings for Python 2.5+ and 3. I needed faster performance for long strings and floats and MrJSON benchmarks at 2-3 times faster than the fastest python parsers for my use cases.

To install it just run Pip as usual:

    $ pip install mrjson

Usage

May be used as a replacement for json

    >>> import mrjson as json
    >>> json.dumps([{"key": "value"}, 81, True])
    '[{"key":"value"},81,true]'
    >>> json.loads("""[{"key": "value"}, 81, true]""")
    [{'key': 'value'}, 81, True]

Encoder differences

ensure_ascii defaults to true in the base json module, but defaults to false here for space and performance reasons.

indent, separators, and sortKeys are not supported as pretty printing doesn't need the performance. Write an issue if you have a use case.

allow_nan is unsupported as NaN and infinity are supported by default.

Custom objects are supported if they implement an json() method returning a valid json string.

Benchmarks

Benchmark your own files as results can vary significantly:

$ python -m timeit -s "import mrjson as json;st = open('canada.json').read();" "json.loads(st)"
100 loops, best of 3: 8.05 msec per loop

$ python -m timeit -s "import json;st = open('canada.json').read();" "json.loads(st)"
10 loops, best of 3: 32.7 msec per loop

Or run bench.py to test MrJSON against some other modules. MrJSON does particularly well decoding long strings and floating point numbers thanks to intel's AVX2 instructions and Milo Yip at Tencent for publishing C++ code implementing Florian Loitsch's float to string algorithms.

Loads

Only 128 byte long strings.

Mostly floating point numbers - canada.json from The Native JSON Benchmark, the fastest C++ JSON parser comes in at 7.9 milliseconds on this machine for comparison

A single tweet from twitter - twit.json

citm_catalog.json from The Native JSON Benchmark

Dumps

Only 128 byte long strings.

Mostly floating point numbers - canada.json from The Native JSON Benchmark

A single tweet from twitter - twit.json

citm_catalog.json from The Native JSON Benchmark

About

JSON encoder and decoder for python written in C/C++

Resources

License

Stars

Watchers

Forks

Packages

No packages published