Skip to main content

A high-performance Python JSON library that fully leverages modern processor capabilities.

Project description

ssrJSON

ssrJSON is a Python JSON library that leverages modern hardware capabilities to achieve peak performance, implemented primarily in C with some components written in C++. It offers a fully compatible interface to Python’s standard json module, making it a seamless drop-in replacement, while providing exceptional performance for JSON encoding and decoding.

ssrjson.dumps() is about 4x-26x as fast as json.dumps() (Python3.13, x86-64, AVX2). ssrjson.loads() is about 2x-8x as fast as json.loads() for str input and is about 2x-8x as fast as json.loads() for bytes input (Python3.13, x86-64, AVX2). ssrJSON also provides ssrjson.dumps_to_bytes(), which encode Python objects directly to bytes object using SIMD instructions, similar to orjson.dumps but without calling slow CPython functions to do the UTF-8 encoding. ssrJSON is faster than or nearly as fast as orjson on most benchmark cases, which means ssrJSON is the world's fastest Python JSON library at now. Typically, ssrJSON is capable of processing non-ASCII strings directly without invoking any slow CPython UTF-8 encoding and decoding interfaces, eliminating the need for intermediate representations. Furthermore, the underlying implementation leverages SIMD acceleration to optimize this process. Details of benchmarking can be found in the benchmark repository. Implementation details can be found in Implementation Details section.

The design goal of ssrJSON is to provide a straightforward and highly compatible approach to replace the inherently slower Python standard JSON encoding and decoding implementation with a significantly more efficient and high-performance alternative. If your module exclusively utilizes dumps and loads, you can replace the current JSON implementation by importing ssrJSON as import ssrjson as json. To facilitate this, ssrJSON maintains compatibility with the argument formats of json.dumps and json.loads; however, it does not guarantee identical results to the standard JSON module, as many features are either not yet supported or intentionally omitted. For further information, please refer to the section Implementation Details.

The development of ssrJSON is still actively ongoing, and some features have yet to be supported. Your code contributions are highly appreciated.

How To Install

ssrJSON requires at least SSE4.2 on x86-64 (x86-64-v2). ssrJSON does not work with other Python implementations other than CPython. Currently supported CPython versions are 3.9, 3.10, 3.11, 3.12, 3.13, 3.14.

Pre-built wheels will soon be available on PyPI. Then you can install it with

pip install ssrjson

Build From Source

Since ssrJSON utilizes LLVM's vectorization extensions, it requires compilation with Clang and cannot be compiled in GCC or MSVC environments. On Windows, clang-cl can be used for this purpose. Build can be easily done by the following commands (make sure CMake, Clang and Python are already installed)

# On Linux:
# export CC=clang
# export CXX=clang++
mkdir build
cmake -S . -B build  # On Windows, configure with `cmake -T ClangCL`
cmake --build build

Usage

Basic

>>> import ssrjson
>>> ssrjson.dumps({"key": "value"})
'{"key":"value"}'
>>> ssrjson.loads('{"key":"value"}')
{'key': 'value'}
>>> ssrjson.dumps_to_bytes({"key": "value"})
b'{"key":"value"}'
>>> ssrjson.loads(b'{"key":"value"}')
{'key': 'value'}

Indent

ssrJSON only supports encoding with indent = 2, 4 or no indent (indent=0). When indent is used, a space is inserted between each key and value.

>>> import ssrjson
>>> ssrjson.dumps({"a": "b", "c": {"d": True}, "e": [1, 2]})
'{"a":"b","c":{"d":true},"e":[1,2]}'
>>> ssrjson.dumps({"a": "b", "c": {"d": True}, "e": [1, 2]}, indent=2)
'{\n  "a": "b",\n  "c": {\n    "d": true\n  },\n  "e": [\n    1,\n    2\n  ]\n}'
>>> ssrjson.dumps({"a": "b", "c": {"d": True}, "e": [1, 2]}, indent=4)
'{\n    "a": "b",\n    "c": {\n        "d": true\n    },\n    "e": [\n        1,\n        2\n    ]\n}'
>>> ssrjson.dumps({"a": "b", "c": {"d": True}, "e": [1, 2]}, indent=3)
Traceback (most recent call last):
  File "<python-input>", line 1, in <module>
    ssrjson.dumps({"a": "b", "c": {"d": True}, "e": [1, 2]}, indent=3)
    ~~~~~~~~~~~~~^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
ValueError: indent must be 0, 2, or 4

Other Arguments

Arguments like ensure_ascii, parse_float provided by json can be recognized but ignored by design.

The functionality of object_hook in json.loads will be supported in future.

Implementation Details

The implementations of ssrJSON's dumps and loads functions are designed to perform in-place processing as much as possible, avoiding intermediate representations. The dumps function employs SIMD instructions for rapid encoding in a single step. Similarly, dumps_to_bytes uses SIMD to efficiently handle both UTF-8 encoding and JSON serialization at the same time. With minor modifications, the code used by dumps_to_bytes can also serve as a SIMD-accelerated replacement for str.encode("utf-8").

The implementation of ssrJSON's loads draws inspiration from yyjson, and also orjson's caching algorithm for short dictionary keys. When the input type is str, loads avoids any UTF-8 encoding or decoding operations on non-ASCII strings. If the input is bytes, loads utilizes a modified string decoding algorithm based on yyjson. The main control flow and number decoding of loads are also modified from yyjson.

Generally, ssrjson.dumps behaves like json.dumps with ensure_ascii=False, and ssrjson.loads behaves like json.loads.

Features

Below we explain some feature details of ssrJSON, which might be different from json module or other third-party JSON libraries.

Strings

Code points within the range [0xd800, 0xdfff] cannot be represented in UTF-8 encoding, and the standard JSON specification typically prohibits the presence of such characters. However, since Python's str type is not encoded in UTF-8, ssrJSON aims to maintain compatibility with the Python json module's behavior, while other third-party Python JSON libraries may complain about this. In contrast, for the dumps_to_bytes function, which encodes output in UTF-8, the inclusion of these characters in the input is considered invalid.

>>> s = chr(0xd800)
>>> (json.dumps(s, ensure_ascii=False) == '"' + s + '"', json.dumps(s, ensure_ascii=False))
(True, '"\ud800"')
>>> (ssrjson.dumps(s) == '"' + s + '"', ssrjson.dumps(s))
(True, '"\ud800"')
>>> ssrjson.dumps_to_bytes(s)
Traceback (most recent call last):
  File "<python-input>", line 1, in <module>
    ssrjson.dumps_to_bytes(s)
    ~~~~~~~~~~~~~~~~~~~~~~^^^
ssrjson.JSONEncodeError: Cannot encode unicode character in range [0xd800, 0xdfff] to utf-8
>>> json.loads(json.dumps(s, ensure_ascii=False)) == s
True
>>> ssrjson.loads(ssrjson.dumps(s)) == s
True

Integers

ssrjson.dumps can only handle integers that can be expressed by either uint64_t or int64_t in C.

>>> ssrjson.dumps(-(1<<63)-1)
Traceback (most recent call last):
  File "<python-input>", line 1, in <module>
    ssrjson.dumps(-(1<<63)-1)
    ~~~~~~~~~~~~~^^^^^^^^^^^^
ssrjson.JSONEncodeError: convert value to long long failed
>>> ssrjson.dumps(-(1<<63))
'-9223372036854775808'
>>> ssrjson.dumps((1<<64)-1)
'18446744073709551615'
>>> ssrjson.dumps(1<<64)
Traceback (most recent call last):
  File "<python-input>", line 1, in <module>
    ssrjson.dumps(1<<64)
    ~~~~~~~~~~~~~^^^^^^^
ssrjson.JSONEncodeError: convert value to unsigned long long failed

ssrjson.loads treats overflow integers as float objects.

>>> ssrjson.loads('-9223372036854775809')  # -(1<<63)-1
-9.223372036854776e+18
>>> ssrjson.loads('-9223372036854775808')  # -(1<<63)
-9223372036854775808
>>> ssrjson.loads('18446744073709551615')  # (1<<64)-1
18446744073709551615
>>> ssrjson.loads('18446744073709551616')  # 1<<64
1.8446744073709552e+19

Floats

For floating-point encoding, ssrJSON employs a slightly modified version of the Dragonbox algorithm. Dragonbox is a highly efficient algorithm for converting floating-point to strings, typically producing output in scientific notation. ssrJSON has partially adapted this algorithm to enhance readability by outputting a more user-friendly format when no exponent is present.

Encoding and decoding math.inf are supported. ssrjson.dumps outputs the same result as json.dumps. The input of ssrjson.loads should be "infinity" with lower or upper cases (for each character), and cannot be "inf".

>>> json.dumps(math.inf)
'Infinity'
>>> ssrjson.dumps(math.inf)
'Infinity'
>>> ssrjson.loads("[infinity, Infinity, InFiNiTy, INFINITY]")
[inf, inf, inf, inf]

The case of math.nan is similar.

>>> json.dumps(math.nan)
'NaN'
>>> ssrjson.dumps(math.nan)
'NaN'
>>> ssrjson.loads("[nan, Nan, NaN, NAN]")
[nan, nan, nan, nan]

Limitations

Please note that ssrJSON is currently in the beta stage of development.

Several commonly used features are still under development, including the serialization of subclass objects of built-in types such as dict, list, and str, the object_hook functionality, and error location reporting during decoding. ssrJSON will not support encoding and decoding of third-party data structures.

The ARM64 architecture is not yet supported but will be supported in the near future.

Contributing

Contributions are welcome! Please open issues or submit pull requests for bug fixes, performance improvements, or new features. There will soon be a development documentation.

License

This project is licensed under the MIT License. Licenses of other repositories are under licenses directory.

Acknowledgments

We would like to express our gratitude to the outstanding libraries and their authors:

  • CPython
  • yyjson: ssrJSON draws extensively from yyjson’s highly optimized implementations, including the core decoding logic, the decoding of bytes objects, and the number decoding routines.
  • orjson: ssrJSON references parts of orjson’s SIMD-based ASCII string encoding and decoding algorithms, as well as the dictionary key caching mechanism. Additionally, ssrJSON utilizes orjson’s pytest framework for testing purposes.
  • Dragonbox: ssrJSON employs Dragonbox for high-performance floating-point encoding.
  • xxHash: ssrJSON leverages xxHash to efficiently compute hash values for key caching.

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

ssrjson-0.0.1.tar.gz (352.8 kB view details)

Uploaded Source

Built Distributions

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

ssrjson-0.0.1-cp313-cp313-win_amd64.whl (634.3 kB view details)

Uploaded CPython 3.13Windows x86-64

ssrjson-0.0.1-cp313-cp313-manylinux2014_x86_64.manylinux_2_17_x86_64.whl (682.8 kB view details)

Uploaded CPython 3.13manylinux: glibc 2.17+ x86-64

ssrjson-0.0.1-cp312-cp312-win_amd64.whl (633.5 kB view details)

Uploaded CPython 3.12Windows x86-64

ssrjson-0.0.1-cp312-cp312-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (682.8 kB view details)

Uploaded CPython 3.12manylinux: glibc 2.17+ x86-64

ssrjson-0.0.1-cp311-cp311-win_amd64.whl (631.2 kB view details)

Uploaded CPython 3.11Windows x86-64

ssrjson-0.0.1-cp311-cp311-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (681.5 kB view details)

Uploaded CPython 3.11manylinux: glibc 2.17+ x86-64

ssrjson-0.0.1-cp310-cp310-win_amd64.whl (631.2 kB view details)

Uploaded CPython 3.10Windows x86-64

ssrjson-0.0.1-cp310-cp310-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (681.5 kB view details)

Uploaded CPython 3.10manylinux: glibc 2.17+ x86-64

ssrjson-0.0.1-cp39-cp39-win_amd64.whl (631.2 kB view details)

Uploaded CPython 3.9Windows x86-64

ssrjson-0.0.1-cp39-cp39-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (681.5 kB view details)

Uploaded CPython 3.9manylinux: glibc 2.17+ x86-64

File details

Details for the file ssrjson-0.0.1.tar.gz.

File metadata

  • Download URL: ssrjson-0.0.1.tar.gz
  • Upload date:
  • Size: 352.8 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.13.5

File hashes

Hashes for ssrjson-0.0.1.tar.gz
Algorithm Hash digest
SHA256 a2201c786355e28a50d3bf0716fb19fdfe09fa061c802153a264e38731313467
MD5 36840e25738ea944b1bb36236c9babd3
BLAKE2b-256 89ddd9c23801b3711297d4b5596cc0d561dcc337633f3b61e51745b9e62dcd3d

See more details on using hashes here.

File details

Details for the file ssrjson-0.0.1-cp313-cp313-win_amd64.whl.

File metadata

  • Download URL: ssrjson-0.0.1-cp313-cp313-win_amd64.whl
  • Upload date:
  • Size: 634.3 kB
  • Tags: CPython 3.13, Windows x86-64
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.13.5

File hashes

Hashes for ssrjson-0.0.1-cp313-cp313-win_amd64.whl
Algorithm Hash digest
SHA256 b9c6454565ddd5be9a089792e0f1762e17097582db0678bd4e9c95780154e11b
MD5 f0d58da1ddc81f5581d2a3a416efce2c
BLAKE2b-256 d0cca4aaae8f56bb1268dc525d8c7feac17ad843be87d8ff4cbbbbecaa4725dd

See more details on using hashes here.

File details

Details for the file ssrjson-0.0.1-cp313-cp313-manylinux2014_x86_64.manylinux_2_17_x86_64.whl.

File metadata

File hashes

Hashes for ssrjson-0.0.1-cp313-cp313-manylinux2014_x86_64.manylinux_2_17_x86_64.whl
Algorithm Hash digest
SHA256 4dd889f2a82016324c9f4ed275320d00368898f735ea4f9a5bc0c3a7a0191c8a
MD5 2e395ac28cb5a4da84d1ad12be060b14
BLAKE2b-256 1ec03c8d30530a1745e6cdf1300d11bd2e80e367e1aa4ae704d28259e4d77610

See more details on using hashes here.

File details

Details for the file ssrjson-0.0.1-cp312-cp312-win_amd64.whl.

File metadata

  • Download URL: ssrjson-0.0.1-cp312-cp312-win_amd64.whl
  • Upload date:
  • Size: 633.5 kB
  • Tags: CPython 3.12, Windows x86-64
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.13.5

File hashes

Hashes for ssrjson-0.0.1-cp312-cp312-win_amd64.whl
Algorithm Hash digest
SHA256 a115b3cee274d3df2f03997efc514f6ad92e5c44869fb81f158c25cde49d7261
MD5 c1815bb177284d14aa764c80c852063f
BLAKE2b-256 3f9f962656210430aa2f0afe6464731713ba89cd09722ebf62bda0d67e9bc958

See more details on using hashes here.

File details

Details for the file ssrjson-0.0.1-cp312-cp312-manylinux_2_17_x86_64.manylinux2014_x86_64.whl.

File metadata

File hashes

Hashes for ssrjson-0.0.1-cp312-cp312-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 9a387d9db99083779a59e4f1fcbf959b7aab902b18fd7ffaf60f91920c3bf6da
MD5 2bc42342433e37d9c09d4d28421d6019
BLAKE2b-256 52a670ee9b2fc81c657960a48b4391e4d5b9268d66887d916dd79ae78d8e766d

See more details on using hashes here.

File details

Details for the file ssrjson-0.0.1-cp311-cp311-win_amd64.whl.

File metadata

  • Download URL: ssrjson-0.0.1-cp311-cp311-win_amd64.whl
  • Upload date:
  • Size: 631.2 kB
  • Tags: CPython 3.11, Windows x86-64
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.13.5

File hashes

Hashes for ssrjson-0.0.1-cp311-cp311-win_amd64.whl
Algorithm Hash digest
SHA256 5b16970f8f21c6e784f4fa63378e65f464be2d73dab2a401b84f080eb175ed2d
MD5 c8da64a824616014cb6ff4e21bcfdaae
BLAKE2b-256 0625ce6ef065fbb3c08729ed30a60d06f43529e92898e898e93db73d8f30c38a

See more details on using hashes here.

File details

Details for the file ssrjson-0.0.1-cp311-cp311-manylinux_2_17_x86_64.manylinux2014_x86_64.whl.

File metadata

File hashes

Hashes for ssrjson-0.0.1-cp311-cp311-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 8cc1c6f329338abb13e302f1602537d9dd62d97e89e3f33df9fadd6cefc2658a
MD5 3138c6cf1987449ce1d59b4d043ca73a
BLAKE2b-256 eb7f2fb74d468cc50299e969c82f3d24efb8a09d9943053b5c66e1e43143a483

See more details on using hashes here.

File details

Details for the file ssrjson-0.0.1-cp310-cp310-win_amd64.whl.

File metadata

  • Download URL: ssrjson-0.0.1-cp310-cp310-win_amd64.whl
  • Upload date:
  • Size: 631.2 kB
  • Tags: CPython 3.10, Windows x86-64
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.13.5

File hashes

Hashes for ssrjson-0.0.1-cp310-cp310-win_amd64.whl
Algorithm Hash digest
SHA256 51e41be419e05c01170dfb84aa53e97cfce51a36634704953b56f9754f4d5339
MD5 48b53ce724f6f2ff021c1f4c5630ff59
BLAKE2b-256 20cd4f92278bac833bce52cee8b5e7f2c1784789e70bc8c9efdb0ddbb900339b

See more details on using hashes here.

File details

Details for the file ssrjson-0.0.1-cp310-cp310-manylinux_2_17_x86_64.manylinux2014_x86_64.whl.

File metadata

File hashes

Hashes for ssrjson-0.0.1-cp310-cp310-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 4138a16dd652b6b842c981a0e734e52f76d6e48ff65dc598d8b82928703c7baf
MD5 f520f3167408ab1a3592e59faae66aaa
BLAKE2b-256 c4b57d937dfd30832c030983234ce1d60ee1bbd5c29fd793e2c5278059fd3097

See more details on using hashes here.

File details

Details for the file ssrjson-0.0.1-cp39-cp39-win_amd64.whl.

File metadata

  • Download URL: ssrjson-0.0.1-cp39-cp39-win_amd64.whl
  • Upload date:
  • Size: 631.2 kB
  • Tags: CPython 3.9, Windows x86-64
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.13.5

File hashes

Hashes for ssrjson-0.0.1-cp39-cp39-win_amd64.whl
Algorithm Hash digest
SHA256 33cdb4dbad1cc68adbe0e7cd0949753ccc520b6c1e80bad52df759ae1c5ea7f7
MD5 15bd018f080926f2ffb034213bc29018
BLAKE2b-256 e27c09bf895a4b194728d87dc28868924c60ed8de126b8c7df22155d7f559477

See more details on using hashes here.

File details

Details for the file ssrjson-0.0.1-cp39-cp39-manylinux_2_17_x86_64.manylinux2014_x86_64.whl.

File metadata

File hashes

Hashes for ssrjson-0.0.1-cp39-cp39-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 93458ab8f2da2bf7b93ed4d4a1a0d9ed7424fe182be9f44e3c5ddeec3775c844
MD5 bd0b8a7385df553e3f0f3649885d9d1a
BLAKE2b-256 332a01d261305c12781430db77db9fecc1722404eb5898e6dfc11e9135d3d1fa

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