2021-09-20 12:30:37 +02:00
|
|
|
# UltraJSON
|
|
|
|
|
|
|
|
[![PyPI version](https://img.shields.io/pypi/v/ujson.svg?logo=pypi&logoColor=FFE873)](https://pypi.org/project/ujson)
|
|
|
|
[![Supported Python versions](https://img.shields.io/pypi/pyversions/ujson.svg?logo=python&logoColor=FFE873)](https://pypi.org/project/ujson)
|
|
|
|
[![PyPI downloads](https://img.shields.io/pypi/dm/ujson.svg)](https://pypistats.org/packages/ujson)
|
|
|
|
[![GitHub Actions status](https://github.com/ultrajson/ultrajson/workflows/Test/badge.svg)](https://github.com/ultrajson/ultrajson/actions)
|
|
|
|
[![codecov](https://codecov.io/gh/ultrajson/ultrajson/branch/main/graph/badge.svg)](https://codecov.io/gh/ultrajson/ultrajson)
|
|
|
|
[![DOI](https://zenodo.org/badge/1418941.svg)](https://zenodo.org/badge/latestdoi/1418941)
|
|
|
|
[![Code style: Black](https://img.shields.io/badge/code%20style-Black-000000.svg)](https://github.com/psf/black)
|
|
|
|
|
|
|
|
UltraJSON is an ultra fast JSON encoder and decoder written in pure C with bindings for
|
2021-11-22 12:51:22 +01:00
|
|
|
Python 3.7+.
|
2021-09-20 12:30:37 +02:00
|
|
|
|
|
|
|
Install with pip:
|
|
|
|
|
|
|
|
```sh
|
|
|
|
python -m pip install ujson
|
|
|
|
```
|
|
|
|
|
|
|
|
## Usage
|
|
|
|
|
|
|
|
May be used as a drop in replacement for most other JSON parsers for Python:
|
|
|
|
|
|
|
|
```pycon
|
|
|
|
>>> import ujson
|
|
|
|
>>> ujson.dumps([{"key": "value"}, 81, True])
|
|
|
|
'[{"key":"value"},81,true]'
|
|
|
|
>>> ujson.loads("""[{"key": "value"}, 81, true]""")
|
|
|
|
[{'key': 'value'}, 81, True]
|
|
|
|
```
|
|
|
|
|
|
|
|
### Encoder options
|
|
|
|
|
|
|
|
#### encode_html_chars
|
|
|
|
|
|
|
|
Used to enable special encoding of "unsafe" HTML characters into safer Unicode
|
|
|
|
sequences. Default is `False`:
|
|
|
|
|
|
|
|
```pycon
|
|
|
|
>>> ujson.dumps("<script>John&Doe", encode_html_chars=True)
|
|
|
|
'"\\u003cscript\\u003eJohn\\u0026Doe"'
|
|
|
|
```
|
|
|
|
|
|
|
|
#### ensure_ascii
|
|
|
|
|
|
|
|
Limits output to ASCII and escapes all extended characters above 127. Default is `True`.
|
|
|
|
If your end format supports UTF-8, setting this option to false is highly recommended to
|
|
|
|
save space:
|
|
|
|
|
|
|
|
```pycon
|
|
|
|
>>> ujson.dumps("åäö")
|
|
|
|
'"\\u00e5\\u00e4\\u00f6"'
|
|
|
|
>>> ujson.dumps("åäö", ensure_ascii=False)
|
|
|
|
'"åäö"'
|
|
|
|
```
|
|
|
|
|
|
|
|
#### escape_forward_slashes
|
|
|
|
|
|
|
|
Controls whether forward slashes (`/`) are escaped. Default is `True`:
|
|
|
|
|
|
|
|
```pycon
|
|
|
|
>>> ujson.dumps("http://esn.me")
|
|
|
|
'"http:\\/\\/esn.me"'
|
|
|
|
>>> ujson.dumps("http://esn.me", escape_forward_slashes=False)
|
|
|
|
'"http://esn.me"'
|
|
|
|
```
|
|
|
|
|
|
|
|
#### indent
|
|
|
|
|
|
|
|
Controls whether indentation ("pretty output") is enabled. Default is `0` (disabled):
|
|
|
|
|
|
|
|
```pycon
|
|
|
|
>>> ujson.dumps({"foo": "bar"})
|
|
|
|
'{"foo":"bar"}'
|
|
|
|
>>> print(ujson.dumps({"foo": "bar"}, indent=4))
|
|
|
|
{
|
|
|
|
"foo":"bar"
|
|
|
|
}
|
|
|
|
```
|
|
|
|
|
|
|
|
## Benchmarks
|
|
|
|
|
|
|
|
*UltraJSON* calls/sec compared to other popular JSON parsers with performance gain
|
|
|
|
specified below each.
|
|
|
|
|
|
|
|
### Test machine
|
|
|
|
|
2023-06-01 21:13:29 +02:00
|
|
|
Linux 5.15.0-1037-azure x86_64 #44-Ubuntu SMP Thu Apr 20 13:19:31 UTC 2023
|
2021-09-20 12:30:37 +02:00
|
|
|
|
|
|
|
### Versions
|
|
|
|
|
2023-06-01 15:59:05 +02:00
|
|
|
- CPython 3.11.3 (main, Apr 6 2023, 07:55:46) [GCC 11.3.0]
|
2023-06-01 21:13:29 +02:00
|
|
|
- ujson : 1.36.dev558
|
|
|
|
- orjson : 3.9.0
|
2023-06-01 15:59:05 +02:00
|
|
|
- simplejson : 3.19.1
|
|
|
|
- json : 2.0.9
|
|
|
|
|
|
|
|
| | ujson | orjson | simplejson | json |
|
|
|
|
|-------------------------------------------------------------------------------|-----------:|-----------:|-----------:|-----------:|
|
|
|
|
| Array with 256 doubles | | | | |
|
2023-06-01 21:13:29 +02:00
|
|
|
| encode | 16,268 | 67,856 | 4,113 | 4,144 |
|
|
|
|
| decode | 20,055 | 72,031 | 10,059 | 10,158 |
|
2023-06-01 15:59:05 +02:00
|
|
|
| Array with 256 UTF-8 strings | | | | |
|
2023-06-01 21:13:29 +02:00
|
|
|
| encode | 2,468 | 24,117 | 2,337 | 2,437 |
|
|
|
|
| decode | 2,472 | 2,860 | 364 | 1,393 |
|
2023-06-01 15:59:05 +02:00
|
|
|
| Array with 256 strings | | | | |
|
2023-06-01 21:13:29 +02:00
|
|
|
| encode | 37,144 | 110,032 | 15,895 | 18,304 |
|
|
|
|
| decode | 25,211 | 59,879 | 32,200 | 32,933 |
|
2023-06-01 15:59:05 +02:00
|
|
|
| Medium complex object | | | | |
|
2023-06-01 21:13:29 +02:00
|
|
|
| encode | 6,501 | 38,501 | 3,025 | 4,415 |
|
|
|
|
| decode | 9,518 | 18,114 | 5,901 | 7,286 |
|
2023-06-01 15:59:05 +02:00
|
|
|
| Array with 256 True values | | | | |
|
2023-06-01 21:13:29 +02:00
|
|
|
| encode | 83,830 | 324,140 | 65,395 | 66,498 |
|
|
|
|
| decode | 162,760 | 281,594 | 118,120 | 125,927 |
|
2023-06-01 15:59:05 +02:00
|
|
|
| Array with 256 dict{string, int} pairs | | | | |
|
2023-06-01 21:13:29 +02:00
|
|
|
| encode | 9,265 | 58,508 | 2,295 | 5,504 |
|
|
|
|
| decode | 12,911 | 20,799 | 7,110 | 9,592 |
|
2023-06-01 15:59:05 +02:00
|
|
|
| Dict with 256 arrays with 256 dict{string, int} pairs | | | | |
|
2023-06-01 21:13:29 +02:00
|
|
|
| encode | 35 | 232 | 7 | 19 |
|
|
|
|
| decode | 31 | 34 | 18 | 22 |
|
2023-06-01 15:59:05 +02:00
|
|
|
| Dict with 256 arrays with 256 dict{string, int} pairs, outputting sorted keys | | | | |
|
2023-06-01 21:13:29 +02:00
|
|
|
| encode | 27 | | 6 | 20 |
|
2023-06-01 15:59:05 +02:00
|
|
|
| Complex object | | | | |
|
2023-06-01 21:13:29 +02:00
|
|
|
| encode | 448 | | 341 | 396 |
|
|
|
|
| decode | 468 | 591 | 162 | 266 |
|
2023-06-01 15:59:05 +02:00
|
|
|
|
|
|
|
Above metrics are in call/sec, larger is better.
|
2022-02-17 20:38:09 +01:00
|
|
|
|
|
|
|
## Build options
|
|
|
|
|
|
|
|
For those with particular needs, such as Linux distribution packagers, several
|
|
|
|
build options are provided in the form of environment variables.
|
|
|
|
|
|
|
|
### Debugging symbols
|
|
|
|
|
|
|
|
#### UJSON_BUILD_NO_STRIP
|
|
|
|
|
|
|
|
By default, debugging symbols are stripped on Linux platforms. Setting this
|
|
|
|
environment variable with a value of `1` or `True` disables this behavior.
|
|
|
|
|
|
|
|
### Using an external or system copy of the double-conversion library
|
|
|
|
|
|
|
|
These two environment variables are typically used together, something like:
|
|
|
|
|
|
|
|
```sh
|
|
|
|
export UJSON_BUILD_DC_INCLUDES='/usr/include/double-conversion'
|
|
|
|
export UJSON_BUILD_DC_LIBS='-ldouble-conversion'
|
|
|
|
```
|
|
|
|
|
|
|
|
Users planning to link against an external shared library should be aware of
|
|
|
|
the ABI-compatibility requirements this introduces when upgrading system
|
|
|
|
libraries or copying compiled wheels to other machines.
|
|
|
|
|
|
|
|
#### UJSON_BUILD_DC_INCLUDES
|
|
|
|
|
|
|
|
One or more directories, delimited by `os.pathsep` (same as the `PATH`
|
|
|
|
environment variable), in which to look for `double-conversion` header files;
|
|
|
|
the default is to use the bundled copy.
|
|
|
|
|
|
|
|
#### UJSON_BUILD_DC_LIBS
|
|
|
|
|
|
|
|
Compiler flags needed to link the `double-conversion` library; the default
|
|
|
|
is to use the bundled copy.
|