Package: ujson-ia
About this forkWe use this version at the Internet Archive. We have merged @vdmit11’s changes fromhttps://github.com/dignio/ultrajsonand the latest from upstream master, and may continue to make other tweaks.To install:$pipinstallujson-iaBack to your regularly scheduled readmeUltraJSON 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 checkoutujson4c, based on UltraJSON.Please checkout the rest of the projects in the Ultra series:http://github.com/esnme/ultramemcachehttp://github.com/esnme/ultramysqlUsageMay be used as a drop in replacement for most other JSON parsers for Python:>>>importujson>>>ujson.dumps([{"key":"value"},81,True])'[{"key":"value"},81,true]'>>>ujson.loads("""[{"key": "value"}, 81, true]""")[{u'key':u'value'},81,True]Encoder optionsencode_html_charsUsed to enable special encoding of “unsafe” HTML characters into safer Unicode sequences. Default isFalse:>>>ujson.dumps("<script>John&Doe",encode_html_chars=True)'"\\u003cscript\\u003eJohn\\u0026Doe"'ensure_asciiLimits 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"'escape_forward_slashesControls whether forward slashes (/) are escaped. Default isTrue:>>>ujson.dumps("http://esn.me")'"http:\/\/esn.me"'>>>ujson.dumps("http://esn.me",escape_forward_slashes=False)'"http://esn.me"'indentControls whether indention (“pretty output”) is enabled. Default is0(disabled):>>>ujson.dumps({"foo":"bar"})'{"foo":"bar"}'>>>ujson.dumps({"foo":"bar"},indent=4){"foo":"bar"}pre_encode_hookAllows to provide a custom function which is called for every encoded Python object.The hook function semantics is similar to the standard JSONEncoder.default() method,
but the pre_encode_hook() is called before any other serialization attempts, while
the default() is called when all other options didn’t work.That allows to override already exsiting behavior and define custom serialization
formats for things like dates. For example:# Default behavior: datetime is converted to timestamp
>>> ujson.dumps({"a": "foo", "b": datetime.now()})
'{"a":"foo","b":1454523657}'

# Hook is involved: the datetime object is replaced with the .isoformat() string
>>> def hook(obj):
        return obj.isoformat() if hasattr(obj, 'isoformat') else obj

>>> ujson.dumps({"a": "foo", "b": datetime.now()}, pre_encode_hook=hook)
'{"a":"foo","b":"2016-02-03T18:21:55.351081"}'The hook may be used to replace any object with any other arbitrary object before
encoding it. However, it doesn’t cancel all further encoding transformations.
For example, if you return adatetimeobject from the hook instead of a string,
it will be transformed to a timestamp.pre_encode_primitiveThe boolean flag that indicates that pre_encode_hook() should also be called
for Python objects that serialized to primitive JSON types (Number, String,
Boolean, null).Usually you don’t need to define any special serialization format for these
types, so the flag is false by default.Enabling this flag may produce huge amount of pre_encode_hook() calls (the
hook will be called for every single JSON value) and thus affect the performance.Decoders optionsprecise_floatSet 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.5599999999999996object_hookA custom Python function which is called after a JSON object is decoded.The hook semantics is similar to the standard JSONDecoder.object_hook() behavior.
You may use it to transform a dictionary (the decoded JSON object) into a more
specific object.For example:>>> def hook(obj):
        if '__complex__' in obj:
            return complex(obj['real'], obj['imag'])
        return obj

>>> ujson.loads('{"__complex__": true, "real": 1, "imag": 2}', object_hook=hook)
(1+2j)string_hookSimilar toobject_hook, but called for every decoded string.Useful for deserializing objects like dates from their textual representations, e.g.:>>> def hook(s):
        if s.startswith('__DATE'):
            return datetime.strptime(s, '__DATE: %Y-%m-%d')
        return s

>>> ujson.loads('{"a": "foo", "b": "__DATE: 2016-01-01"}', string_hook=hook)
{'a': 'foo', 'b': datetime.datetime(2016, 1, 1, 0, 0)}BenchmarksUltraJSONcalls/sec compared to three other popular JSON parsers with performance gain specified below each.Test machine:Linux 3.13.0-66-generic x86_64 #108-Ubuntu SMP Wed Oct 7 15:20:27 UTC 2015Versions:CPython 2.7.6 (default, Jun 22 2015, 17:58:13) [GCC 4.8.2]blist     : 1.3.6simplejson: 3.8.1ujson     : 1.34 (0c52200eb4e2d97e548a765d5f089858c41967b0)yajl      : 0.3.5ujsonyajlsimplejsonjsonArray with 256 doublesencode3508.195742.003232.383309.09decode25103.3711257.8311696.2611871.04Array with 256 UTF-8 stringsencode3189.712717.142006.382961.72decode1354.94630.54356.35344.05Array with 256 stringsencode18127.4712537.3912541.2320001.00decode23264.7012788.8525427.889352.36Medium complex objectencode10519.385021.293686.864643.47decode9676.535326.798515.773017.30Array with 256 True valuesencode105998.03102067.2844758.5160424.80decode163869.9678341.57110859.36115013.90Array with 256 dict{string, int} pairsencode13471.3212109.093876.408833.92decode16890.638946.0712218.553350.72Dict with 256 arrays with 256 dict{string, int} pairsencode50.2546.4513.8229.28decode33.2722.1027.9110.43Dict with 256 arrays with 256 dict{string, int} pairs, outputting sorted keysencode27.197.752.39Complex objectencode577.98387.81470.02decode496.73234.44151.00145.16Versions:CPython 3.4.3 (default, Oct 14 2015, 20:28:29) [GCC 4.8.4]blist     : 1.3.6simplejson: 3.8.1ujson     : 1.34 (0c52200eb4e2d97e548a765d5f089858c41967b0)yajl      : 0.3.5ujsonyajlsimplejsonjsonArray with 256 doublesencode3477.155732.243016.763071.99decode23625.209731.459501.579901.92Array with 256 UTF-8 stringsencode1995.892151.611771.981817.20decode1425.04625.38327.14305.95Array with 256 stringsencode25461.7512188.6413054.7614429.81decode21981.3117014.2223869.4822483.58Medium complex objectencode10821.464837.043114.044254.46decode7887.775126.674934.606204.97Array with 256 True valuesencode100452.8694639.4246657.6360358.63decode148312.6975485.9088434.91116395.51Array with 256 dict{string, int} pairsencode11698.138886.963043.696302.35decode10686.407061.775646.807702.29Dict with 256 arrays with 256 dict{string, int} pairsencode44.2634.4310.4021.97decode28.4623.9518.7022.83Dict with 256 arrays with 256 dict{string, int} pairs, outputting sorted keysencode33.606.9422.34Complex objectencode432.30351.47379.34decode434.40221.97149.57147.79