11# orjson
22
33orjson is a fast JSON library for Python. It benchmarks as the fastest Python
4- library for JSON serialization, with 1.6x to 2.6x the performance of the nearest
5- other library, and deserialization performance of 0.95x to 1.2x
6- the nearest other library.
4+ library for JSON. Its serialization performance is 2x to 3x the nearest
5+ other library and 4.5x to 11.5x the standard library. Its deserialization
6+ performance is 1.05x to 1.2x the nearest other library and 1.2x to 4x
7+ the standard library.
78
8- It supports CPython 3.5, 3.6, and 3.7. It is not intended
9- as a drop-in replacement for the standard library's json module.
9+ It supports CPython 3.5, 3.6, and 3.7. Its API is a subset of the
10+ API of the standard library's json module.
1011
1112## Usage
1213
@@ -23,7 +24,6 @@ and Python environment:
2324
2425``` sh
2526git clone --recurse-submodules https://github.com/ijl/orjson.git && cd orjson
26- virtualenv .venv && source .venv/bin/activate
2727pip install --upgrade pyo3-pack
2828pyo3-pack build --release --strip --interpreter python3.7
2929```
@@ -40,13 +40,13 @@ def dumps(obj: Union[str, bytes, dict, list, tuple, int, float, None]) -> bytes:
4040` dumps() ` serializes Python objects to JSON.
4141
4242It has no options, does not support hooks for custom objects, and does not
43- support subclasses. It supports 64-bit integers and 64-bit floats, which
44- is the same as the standard library ` json ` module.
43+ support subclasses.
4544
4645It raises ` TypeError ` on an unsupported type. This exception message
4746describes the invalid object.
4847
49- It raises ` TypeError ` on an integer that is too large.
48+ It raises ` TypeError ` on an integer that exceeds 64 bits. This is the same
49+ as the standard library's ` json ` module.
5050
5151It raises ` TypeError ` if a ` dict ` has a key of a type other than ` str ` .
5252
@@ -107,102 +107,118 @@ roundtrip, jsonchecker, and fixtures files of the
107107[ nativejson-benchmark] ( https://github.com/miloyip/nativejson-benchmark )
108108repository. It is tested to not crash against the
109109[ Big List of Naughty Strings] ( https://github.com/minimaxir/big-list-of-naughty-strings ) .
110- There are integration tests exercising the library's use in web
111- servers (uwsgi and gunicorn, using multiprocess/forked workers) and when
110+ It is tested to not leak memory. It is tested to be correct against
111+ input from the PyJFuzz JSON fuzzer. There are integration tests
112+ exercising the library's use in web servers (uwsgi and gunicorn,
113+ using multiprocess/forked workers) and when
112114multithreaded. It also uses some tests from the ultrajson library.
113115
114116## Performance
115117
116- Serialization performance of orjson is better than ultrajson, rapidjson, or
117- json. Deserialization performance is better to about the same as ultrajson.
118+ Serialization and deserialization performance of orjson is better than
119+ ultrajson, rapidjson, or json. The benchmarks are done on fixtures of real data:
120+
121+ * twitter.json, 631.5KiB, results of a search on Twitter for "一", containing
122+ CJK strings, dictionaries of strings and arrays of dictionaries, indented.
123+
124+ * github.json, 55.8KiB, a GitHub activity feed, containing dictionaries of
125+ strings and arrays of dictionaries, not indented.
126+
127+ * citm_catalog.json, 1.7MiB, concert data, containing nested dictionaries of
128+ strings and arrays of integers, indented.
129+
130+ * canada.json, 2.2MiB, coordinates of the Canadian border in GeoJSON
131+ format, containing floats and arrays, indented.
118132
119133![ alt text] ( doc/twitter_serialization.png " twitter.json serialization ")
120134![ alt text] ( doc/twitter_deserialization.png " twitter.json deserialization ")
121- ![ alt text] ( doc/citm_catalog_serialization.png " citm_catalog.json serialization ")
122- ![ alt text] ( doc/citm_catalog_deserialization.png " citm_catalog.json deserialization ")
123135![ alt text] ( doc/github_serialization.png " github.json serialization ")
124136![ alt text] ( doc/github_deserialization.png " github.json deserialization ")
137+ ![ alt text] ( doc/citm_catalog_serialization.png " citm_catalog.json serialization ")
138+ ![ alt text] ( doc/citm_catalog_deserialization.png " citm_catalog.json deserialization ")
125139![ alt text] ( doc/canada_serialization.png " canada.json serialization ")
126140![ alt text] ( doc/canada_deserialization.png " canada.json deserialization ")
127141
128- #### canada .json deserialization
142+ #### twitter .json serialization
129143
130- | Library | Median (milliseconds) | Operations per second | Relative (latency) |
131- | -----------| -------------------------| -------------------------| ----------------------|
132- | orjson | 7.59 | 131.8 | 1 |
133- | ujson | 7.26 | 133.5 | 0.96 |
134- | rapidjson | 26.72 | 37.4 | 3.52 |
135- | json | 26.78 | 37.3 | 3.53 |
144+ | Library | Median latency (milliseconds) | Operations per second | Relative (latency) |
145+ | -----------| --------------------------------- | -------------------------| ----------------------|
146+ | orjson | 0.48 | 2077.6 | 1 |
147+ | ujson | 1.48 | 664.6 | 3.09 |
148+ | rapidjson | 1.59 | 626.5 | 3.32 |
149+ | json | 2.24 | 443.9 | 4.68 |
136150
137- #### canada .json serialization
151+ #### twitter .json deserialization
138152
139- | Library | Median (milliseconds) | Operations per second | Relative (latency) |
140- | -----------| -------------------------| -------------------------| ----------------------|
141- | orjson | 4.99 | 200.3 | 1 |
142- | ujson | 8.16 | 122.5 | 1.64 |
143- | rapidjson | 43.27 | 23.1 | 8.67 |
144- | json | 48.15 | 20.8 | 9.65 |
153+ | Library | Median latency (milliseconds) | Operations per second | Relative (latency) |
154+ | -----------| --------------------------------- | -------------------------| ----------------------|
155+ | orjson | 2.38 | 418.8 | 1 |
156+ | ujson | 2.67 | 373 | 1.12 |
157+ | rapidjson | 2.78 | 359.5 | 1.16 |
158+ | json | 2.77 | 359.7 | 1.16 |
145159
146- #### citm_catalog.json deserialization
160+ #### github.json serialization
161+
162+ | Library | Median latency (milliseconds) | Operations per second | Relative (latency) |
163+ | -----------| ---------------------------------| -------------------------| ----------------------|
164+ | orjson | 0.06 | 17745 | 1 |
165+ | ujson | 0.14 | 7107.1 | 2.49 |
166+ | rapidjson | 0.16 | 6253.9 | 2.86 |
167+ | json | 0.25 | 3972.5 | 4.49 |
147168
148- | Library | Median (milliseconds) | Operations per second | Relative (latency) |
149- | -----------| -------------------------| -------------------------| ----------------------|
150- | orjson | 5.05 | 198.2 | 1 |
151- | ujson | 6.2 | 161.2 | 1.23 |
152- | rapidjson | 6.57 | 152.2 | 1.3 |
153- | json | 6.62 | 151.1 | 1.31 |
169+ #### github.json deserialization
170+
171+ | Library | Median latency (milliseconds) | Operations per second | Relative (latency) |
172+ | -----------| ---------------------------------| -------------------------| ----------------------|
173+ | orjson | 0.2 | 4929.7 | 1 |
174+ | ujson | 0.22 | 4605.2 | 1.08 |
175+ | rapidjson | 0.24 | 4166.5 | 1.19 |
176+ | json | 0.24 | 4150.8 | 1.19 |
154177
155178#### citm_catalog.json serialization
156179
157- | Library | Median (milliseconds) | Operations per second | Relative (latency) |
158- | -----------| -------------------------| -------------------------| ----------------------|
159- | orjson | 1 | 997.4 | 1 |
160- | ujson | 2.54 | 394.1 | 2.53 |
161- | rapidjson | 2.38 | 419.5 | 2.38 |
162- | json | 5.26 | 190 | 5.25 |
180+ | Library | Median latency (milliseconds) | Operations per second | Relative (latency) |
181+ | -----------| --------------------------------- | -------------------------| ----------------------|
182+ | orjson | 0.76 | 1302 | 1 |
183+ | ujson | 2.58 | 387.2 | 3.38 |
184+ | rapidjson | 2.37 | 421.1 | 3.11 |
185+ | json | 5.41 | 184.4 | 7.09 |
163186
164- #### github .json deserialization
187+ #### citm_catalog .json deserialization
165188
166- | Library | Median (milliseconds) | Operations per second | Relative (latency) |
167- | -----------| -------------------------| -------------------------| ----------------------|
168- | orjson | 0.23 | 4310.6 | 1 |
169- | ujson | 0.23 | 4414.3 | 0.98 |
170- | rapidjson | 0.23 | 4229.4 | 1 |
171- | json | 0.23 | 4176.3 | 1 |
189+ | Library | Median latency (milliseconds) | Operations per second | Relative (latency) |
190+ | -----------| --------------------------------- | -------------------------| ----------------------|
191+ | orjson | 4.28 | 233.1 | 1 |
192+ | ujson | 5.06 | 197.2 | 1.18 |
193+ | rapidjson | 5.82 | 171.7 | 1.36 |
194+ | json | 5.81 | 171.8 | 1.36 |
172195
173- #### github .json serialization
196+ #### canada .json serialization
174197
175- | Library | Median (milliseconds) | Operations per second | Relative (latency) |
176- | -----------| -------------------------| -------------------------| ----------------------|
177- | orjson | 0.06 | 16357.9 | 1 |
178- | ujson | 0.13 | 7531.2 | 2.17 |
179- | rapidjson | 0.16 | 6362.9 | 2.57 |
180- | json | 0 .23 | 4242.5 | 3.8 |
198+ | Library | Median latency (milliseconds) | Operations per second | Relative (latency) |
199+ | -----------| --------------------------------- | -------------------------| ----------------------|
200+ | orjson | 4.04 | 247.7 | 1 |
201+ | ujson | 8.43 | 118.6 | 2.09 |
202+ | rapidjson | 43.93 | 22.7 | 10.88 |
203+ | json | 47 .23 | 21.1 | 11.7 |
181204
182- #### twitter .json deserialization
205+ #### canada .json deserialization
183206
184- | Library | Median (milliseconds) | Operations per second | Relative (latency) |
185- | -----------| -------------------------| -------------------------| ----------------------|
186- | orjson | 2.6 | 385.5 | 1 |
187- | ujson | 2.98 | 336.5 | 1.15 |
188- | rapidjson | 2.84 | 339.1 | 1.09 |
189- | json | 2.84 | 345.9 | 1.09 |
207+ | Library | Median latency (milliseconds) | Operations per second | Relative (latency) |
208+ | -----------| --------------------------------- | -------------------------| ----------------------|
209+ | orjson | 6.69 | 147.6 | 1 |
210+ | ujson | 7.17 | 139.4 | 1.07 |
211+ | rapidjson | 26.77 | 37.4 | 4 |
212+ | json | 26.59 | 37.6 | 3.97 |
190213
191- #### twitter.json serialization
192214
193- | Library | Median (milliseconds) | Operations per second | Relative (latency) |
194- | -----------| -------------------------| -------------------------| ----------------------|
195- | orjson | 0.56 | 1790 | 1 |
196- | ujson | 1.44 | 693.9 | 2.58 |
197- | rapidjson | 1.57 | 636.1 | 2.82 |
198- | json | 2.21 | 452 | 3.96 |
215+ This was measured using orjson 1.3.0 on Python 3.7.2 and Linux.
199216
200- This was measured using orjson 1.2.0 on Python 3.7.1 and Linux. The above can be
201- reproduced using the ` pybench ` and ` graph ` scripts.
217+ The results can be reproduced using the ` pybench ` and ` graph ` scripts.
202218
203219## License
204220
205221orjson is dual licensed under the Apache 2.0 and MIT licenses. It contains
206- code from the hyperjson and ultrajson libraries. It is implemented using
222+ tests from the hyperjson and ultrajson libraries. It is implemented using
207223the [ serde_json] ( https://github.com/serde-rs/json ) and
208224[ pyo3] ( https://github.com/PyO3/pyo3 ) libraries.
0 commit comments