Skip to content

Latest commit

History

234 Commits

Folders and files

NameName
Last commit message
Last commit date

Repository files navigation

UltraJSON

UltraJSON is an ultra fast JSON encoder and decoder written in pure C with bindings for Python 2.5+ and 3.

For a more painless day to day C/C++ JSON decoder experience please checkout ujson4c, based on UltraJSON.

Please checkout the rest of the projects in the Ultra series:

To install it just run Pip as usual:

$ pip install ujson

Usage

May be used as a drop in replacement for most other JSON parsers for Python:

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

Encoder options

encode_html_chars

Used to enable special encoding of "unsafe" HTML characters into safer Unicode sequences. Default is false:

>>> 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:

>>> ujson.dumps(u"\xe5\xe4\xf6")
'"\\u00e5\\u00e4\\u00f6"'
>>> ujson.dumps(u"\xe5\xe4\xf6", ensure_ascii=False)
'"\xc3\xa5\xc3\xa4\xc3\xb6"'

double_precision

Controls how many decimals to encode for double or decimal values. Default is 9:

>>> ujson.dumps(math.pi)
'3.1415926536'
>>> ujson.dumps(math.pi, double_precision=1)
'3.1'
>>> ujson.dumps(math.pi, double_precision=0)
'3'
>>> ujson.dumps(math.pi, double_precision=4)
'3.1416'

Decoders options

precise_float

Set to enable usage of higher precision (strtod) function when decoding string to double values. Default is to use fast but less precise builtin functionality:

>>> ujson.loads("4.56")
4.5600000000000005
>>> ujson.loads("4.56", precise_float=True)
4.5599999999999996

Benchmarks

UltraJSON calls/sec compared to three other popular JSON parsers with performance gain specified below each.

Test machine:

Linux version 2.6.32-131.0.15.el6.x86_64

Versions:

  • ujson: 1.21
  • simplejson: 2.6.2
  • cjson: 1.05
  • yajl: 0.3.5
  • Python: Python 2.6.6 (r266:84292, Jul 20 2011, 10:22:43)
ujsonsimplejsoncjsonyajl
Array with 256 utf-8 strings
Encode4090,74899,3983,863189,86
4,5548,781,28
Decode863,29586,15201,61352,48
1,474,282,45
Medium complex object
Encode9750,371377,151512,063341,91
7,086,452,92
Decode5576,754247,163587,832850,13
1,311,551,96
Array with 256 strings
Encode17998,0112954,468715,0215924,35
1,392,071,13
Decode14540,7119696,1314908,469547,14
0,740,981,52
Array with 256 doubles
Encode2185,201466,871956,993421,10
1,491,120,64
Decode16062,018990,509743,408331,74
1,791,651,93
Array with 256 True values
Encode69767,6025202,5641145,9964330,76
2,771,701,08
Decode91416,0256439,9754918,0942786,02
1,621,662,14
Array with 256 dict{string, int} pairs
Encode11307,541830,452720,907725,56
6,184,161,46
Decode8695,947572,896076,715231,32
1,151,431,66
Dict with 256 arrays with 256 dict
Encode37,764,8810,4927,62
7,743,601,37
Decode17,7015,5611,2512,00
1,141,571,47

About

Ultra fast JSON decoder and encoder written in C with Python bindings

Resources

Stars

0 stars

Watchers

0 watching

Forks

Releases

Packages

Contributors

Languages