Skip to main content

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

Project description

ssrJSON

[![PyPI - Version](https://img.shields.io/pypi/v/ssrjson)](https://pypi.org/project/ssrjson/) [![PyPI - Wheel](https://img.shields.io/pypi/wheel/ssrjson)](https://pypi.org/project/ssrjson/)

A SIMD boosted high-performance and correct Python JSON library that fully leverages modern processor capabilities.

Introduction

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.

How Fast is ssrJSON?

TL;DR: ssrJSON is faster than or nearly as fast as orjson (which announces itself as the fastest Python library for JSON) on most benchmark cases.

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. 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. If you wish to run the benchmark tests yourself, you can execute the following commands:

pip install ssrjson-benchmark
python -m ssrjson_benchmark

This will generate a PDF report of the results. If you choose to, you may submit this report to the benchmark repository, allowing others to view the performance metrics of ssrJSON on your device.

Design Goal

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 intentionally omitted or not yet supported. For further information, please refer to the section Implementation Details.

Current Status

ssrJSON is currently operational, although some potentially useful features have yet to be implemented. The development of ssrJSON is still actively ongoing, and your code contributions are highly appreciated.

How To Install

Pre-built wheels are available on PyPI.

pip install ssrjson

Note: ssrJSON requires at least SSE4.2 on x86-64 (x86-64-v2). ssrJSON does not work with Python implementations other than CPython. Currently supported CPython versions are 3.9, 3.10, 3.11, 3.12, 3.13, 3.14. For Python 3.14, you need to build it from source.

Build From Source

Since ssrJSON utilizes Clang's vector extensions, it requires compilation with Clang and cannot be compiled in GCC or pure 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]}'
>>> print(ssrjson.dumps({"a": "b", "c": {"d": True}, "e": [1, 2]}, indent=2))
{
  "a": "b",
  "c": {
    "d": true
  },
  "e": [
    1,
    2
  ]
}
>>> print(ssrjson.dumps({"a": "b", "c": {"d": True}, "e": [1, 2]}, indent=4))
{
    "a": "b",
    "c": {
        "d": true
    },
    "e": [
        1,
        2
    ]
}
>>> 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 Supported by Python's json

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 some 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 serialization of subclasses of str, the object_hook functionality, and error location reporting during decoding. Additionally, ssrJSON will not support encoding or 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.3.tar.gz (362.7 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.3-cp313-cp313-win_amd64.whl (635.2 kB view details)

Uploaded CPython 3.13Windows x86-64

ssrjson-0.0.3-cp313-cp313-manylinux2014_x86_64.manylinux_2_17_x86_64.whl (680.9 kB view details)

Uploaded CPython 3.13manylinux: glibc 2.17+ x86-64

ssrjson-0.0.3-cp312-cp312-win_amd64.whl (634.7 kB view details)

Uploaded CPython 3.12Windows x86-64

ssrjson-0.0.3-cp312-cp312-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (679.6 kB view details)

Uploaded CPython 3.12manylinux: glibc 2.17+ x86-64

ssrjson-0.0.3-cp311-cp311-win_amd64.whl (632.1 kB view details)

Uploaded CPython 3.11Windows x86-64

ssrjson-0.0.3-cp311-cp311-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (677.9 kB view details)

Uploaded CPython 3.11manylinux: glibc 2.17+ x86-64

ssrjson-0.0.3-cp310-cp310-win_amd64.whl (632.2 kB view details)

Uploaded CPython 3.10Windows x86-64

ssrjson-0.0.3-cp310-cp310-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (677.9 kB view details)

Uploaded CPython 3.10manylinux: glibc 2.17+ x86-64

ssrjson-0.0.3-cp39-cp39-win_amd64.whl (632.2 kB view details)

Uploaded CPython 3.9Windows x86-64

ssrjson-0.0.3-cp39-cp39-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (677.9 kB view details)

Uploaded CPython 3.9manylinux: glibc 2.17+ x86-64

File details

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

File metadata

  • Download URL: ssrjson-0.0.3.tar.gz
  • Upload date:
  • Size: 362.7 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.3.tar.gz
Algorithm Hash digest
SHA256 5d0c98fc9ed41d4347ea9893dc198356741a4a4c572f9fd19ca0d0f345abc1ee
MD5 9be4880cc69b2ccd956671a778d20974
BLAKE2b-256 c011e201e2cc5d5706ba9d13949e34363c7157a727b6d3066bf63e3dab076795

See more details on using hashes here.

File details

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

File metadata

  • Download URL: ssrjson-0.0.3-cp313-cp313-win_amd64.whl
  • Upload date:
  • Size: 635.2 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.3-cp313-cp313-win_amd64.whl
Algorithm Hash digest
SHA256 e5c969d7cf33bb3af96ee4c2eae11472bb59ea318442f8f25dcb8e15d809b2c1
MD5 e69bec6e3611f198b5f16414e1877c5e
BLAKE2b-256 b91ceb146aa55ec989fe844d989cb2584ca53e4f8e6fbd1554095018dac3c18e

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for ssrjson-0.0.3-cp313-cp313-manylinux2014_x86_64.manylinux_2_17_x86_64.whl
Algorithm Hash digest
SHA256 741af462e8793a517095c8dcf64a2fd951b7587ad968b6b796b4ddc9104e193a
MD5 db511cfa538ccdc5dbb4973e0b924805
BLAKE2b-256 56819b442b8a7ea5020fcb8ecbece4154981b88131e3f138c66937f24f68082e

See more details on using hashes here.

File details

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

File metadata

  • Download URL: ssrjson-0.0.3-cp312-cp312-win_amd64.whl
  • Upload date:
  • Size: 634.7 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.3-cp312-cp312-win_amd64.whl
Algorithm Hash digest
SHA256 ec091fb8d967966cdd73e20430c1318abccaeae63e7f81a08d1e6a35be0d06e6
MD5 71d8fe38099e1deab91560baa9b01c44
BLAKE2b-256 e4b63fa683087139b57a47ee25a11c08d9caecc9cdd095e56ac675b327ba4c92

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for ssrjson-0.0.3-cp312-cp312-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 10a4399eecc9c97b22b9fc0233338dcf464e66b244b5aa73e501ac1d0268a5a9
MD5 f539ad848e50a2aba70278e0fd8a2dcc
BLAKE2b-256 e1e7d5909bf8ddc9fc6d042220cb94f1e3c5d60be7da2919748c2de4a56e6177

See more details on using hashes here.

File details

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

File metadata

  • Download URL: ssrjson-0.0.3-cp311-cp311-win_amd64.whl
  • Upload date:
  • Size: 632.1 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.3-cp311-cp311-win_amd64.whl
Algorithm Hash digest
SHA256 6ef56a4b279f8289f735a017f8aa0f911409f46add33df06040363d4ee96864a
MD5 ea1ace7b3e9a0ef8ef24b8f944540c4b
BLAKE2b-256 69e04bdafeeedbf1bd348ba759fa59b3acd67025180a21bc93b9d33cd5ca533e

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for ssrjson-0.0.3-cp311-cp311-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 fe9e1e5a646d68cce2e0a631460902d22042f1b050fa898fcc0b37ebc65ada97
MD5 5d720fbab2b97ccb7e8dccb8f5d9dfab
BLAKE2b-256 36d647606406d678490591c03d57c0eb145b7c1265ab5668d3b6fa8c743d7aa5

See more details on using hashes here.

File details

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

File metadata

  • Download URL: ssrjson-0.0.3-cp310-cp310-win_amd64.whl
  • Upload date:
  • Size: 632.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.3-cp310-cp310-win_amd64.whl
Algorithm Hash digest
SHA256 ad816a00b5c1a0e13daef47dce007d94872089f20d953de32f9cde1472ca7100
MD5 b5fc8ef2af0649604af2fd6872452b9f
BLAKE2b-256 551f6eddfcb124aed35e251c035ac48855df7b3a9a5b2a1907155a09b445db64

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for ssrjson-0.0.3-cp310-cp310-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 75acb8b460f4c364138513973a547dd896eb48be8ec81e4c75e9be700b18897f
MD5 299374616fad2850fd99f865742eeebc
BLAKE2b-256 fe10fcfdc9aab3f1dfa2e3b7a787436f549e635f82818ca18db52f5555bbb90a

See more details on using hashes here.

File details

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

File metadata

  • Download URL: ssrjson-0.0.3-cp39-cp39-win_amd64.whl
  • Upload date:
  • Size: 632.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.3-cp39-cp39-win_amd64.whl
Algorithm Hash digest
SHA256 c9aaf9e493ac703824ec72c2bb4ad089dd405c034da33e94b921cd7936d3ecf8
MD5 5768778f01bd4a27d839fb2e0becd28b
BLAKE2b-256 85f40d0fb3dd670813b3d96749fa96b89d48c39403ecd7e041957645b5422162

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for ssrjson-0.0.3-cp39-cp39-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 ddf4b57cb1f793a570f83897e9c6e8c1403c75f39044bf900a9db9224a56d07f
MD5 131b3c5c9eb1c13489aad666c5e72c81
BLAKE2b-256 2e79f26c8576ea30ea5c71957cb924e16fe85c63fe52976ce12ba01e05349e93

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