DESCRIPTION.rst 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233
  1. UltraJSON
  2. =============
  3. .. image:: https://travis-ci.org/esnme/ultrajson.svg?branch=master
  4. :target: https://travis-ci.org/esnme/ultrajson
  5. UltraJSON is an ultra fast JSON encoder and decoder written in pure C with bindings for Python 2.5+ and 3.
  6. For a more painless day to day C/C++ JSON decoder experience please checkout ujson4c_, based on UltraJSON.
  7. .. _ujson4c: http://github.com/esnme/ujson4c/
  8. | Please checkout the rest of the projects in the Ultra series:
  9. | http://github.com/esnme/ultramemcache
  10. | http://github.com/esnme/ultramysql
  11. To install it just run Pip as usual::
  12. $ pip install ujson
  13. ============
  14. Usage
  15. ============
  16. May be used as a drop in replacement for most other JSON parsers for Python::
  17. >>> import ujson
  18. >>> ujson.dumps([{"key": "value"}, 81, True])
  19. '[{"key":"value"},81,true]'
  20. >>> ujson.loads("""[{"key": "value"}, 81, true]""")
  21. [{u'key': u'value'}, 81, True]
  22. ~~~~~~~~~~~~~~~
  23. Encoder options
  24. ~~~~~~~~~~~~~~~
  25. encode_html_chars
  26. -----------------
  27. Used to enable special encoding of "unsafe" HTML characters into safer Unicode sequences. Default is false::
  28. >>> ujson.dumps("<script>John&Doe", encode_html_chars=True)
  29. '"\\u003cscript\\u003eJohn\\u0026Doe"'
  30. ensure_ascii
  31. -------------
  32. 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::
  33. >>> ujson.dumps(u"\xe5\xe4\xf6")
  34. '"\\u00e5\\u00e4\\u00f6"'
  35. >>> ujson.dumps(u"\xe5\xe4\xf6", ensure_ascii=False)
  36. '"\xc3\xa5\xc3\xa4\xc3\xb6"'
  37. double_precision
  38. ----------------
  39. Controls how many decimals to encode for double or decimal values. Default is 9::
  40. >>> ujson.dumps(math.pi)
  41. '3.1415926536'
  42. >>> ujson.dumps(math.pi, double_precision=1)
  43. '3.1'
  44. >>> ujson.dumps(math.pi, double_precision=0)
  45. '3'
  46. >>> ujson.dumps(math.pi, double_precision=4)
  47. '3.1416'
  48. escape_forward_slashes
  49. ----------------------
  50. Controls whether forward slashes (``/``) are escaped. Default is True::
  51. >>> ujson.dumps("http://esn.me")
  52. '"http:\/\/esn.me"'
  53. >>> ujson.dumps("http://esn.me", escape_forward_slashes=False)
  54. '"http://esn.me"'
  55. indent
  56. ----------------------
  57. Controls whether indention ("pretty output") is enabled. Default is 0 (disabled)::
  58. >>> ujson.dumps({"foo": "bar"})
  59. '{"foo":"bar"}'
  60. >>> ujson.dumps({"foo": "bar"}, indent=4)
  61. {
  62. "foo":"bar"
  63. }
  64. ~~~~~~~~~~~~~~~~
  65. Decoders options
  66. ~~~~~~~~~~~~~~~~
  67. precise_float
  68. -------------
  69. 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::
  70. >>> ujson.loads("4.56")
  71. 4.5600000000000005
  72. >>> ujson.loads("4.56", precise_float=True)
  73. 4.5599999999999996
  74. ~~~~~~~~~~~~~
  75. Test machine:
  76. ~~~~~~~~~~~~~
  77. Linux 3.13.0-66-generic x86_64 #108-Ubuntu SMP Wed Oct 7 15:20:27 UTC 2015
  78. ~~~~~~~~~
  79. Versions:
  80. ~~~~~~~~~
  81. - CPython 2.7.6 (default, Jun 22 2015, 17:58:13) [GCC 4.8.2]
  82. - blist : 1.3.6
  83. - simplejson: 3.8.1
  84. - ujson : 1.34 (0c52200eb4e2d97e548a765d5f089858c41967b0)
  85. - yajl : 0.3.5
  86. +-------------------------------------------------------------------------------+------------+------------+------------+------------+
  87. | | ujson | yajl | simplejson | json |
  88. +===============================================================================+============+============+============+============+
  89. | Array with 256 doubles | | | | |
  90. +-------------------------------------------------------------------------------+------------+------------+------------+------------+
  91. | encode | 3508.19 | 5742.00 | 3232.38 | 3309.09 |
  92. +-------------------------------------------------------------------------------+------------+------------+------------+------------+
  93. | decode | 25103.37 | 11257.83 | 11696.26 | 11871.04 |
  94. +-------------------------------------------------------------------------------+------------+------------+------------+------------+
  95. | Array with 256 UTF-8 strings | | | | |
  96. +-------------------------------------------------------------------------------+------------+------------+------------+------------+
  97. | encode | 3189.71 | 2717.14 | 2006.38 | 2961.72 |
  98. +-------------------------------------------------------------------------------+------------+------------+------------+------------+
  99. | decode | 1354.94 | 630.54 | 356.35 | 344.05 |
  100. +-------------------------------------------------------------------------------+------------+------------+------------+------------+
  101. | Array with 256 strings | | | | |
  102. +-------------------------------------------------------------------------------+------------+------------+------------+------------+
  103. | encode | 18127.47 | 12537.39 | 12541.23 | 20001.00 |
  104. +-------------------------------------------------------------------------------+------------+------------+------------+------------+
  105. | decode | 23264.70 | 12788.85 | 25427.88 | 9352.36 |
  106. +-------------------------------------------------------------------------------+------------+------------+------------+------------+
  107. | Medium complex object | | | | |
  108. +-------------------------------------------------------------------------------+------------+------------+------------+------------+
  109. | encode | 10519.38 | 5021.29 | 3686.86 | 4643.47 |
  110. +-------------------------------------------------------------------------------+------------+------------+------------+------------+
  111. | decode | 9676.53 | 5326.79 | 8515.77 | 3017.30 |
  112. +-------------------------------------------------------------------------------+------------+------------+------------+------------+
  113. | Array with 256 True values | | | | |
  114. +-------------------------------------------------------------------------------+------------+------------+------------+------------+
  115. | encode | 105998.03 | 102067.28 | 44758.51 | 60424.80 |
  116. +-------------------------------------------------------------------------------+------------+------------+------------+------------+
  117. | decode | 163869.96 | 78341.57 | 110859.36 | 115013.90 |
  118. +-------------------------------------------------------------------------------+------------+------------+------------+------------+
  119. | Array with 256 dict{string, int} pairs | | | | |
  120. +-------------------------------------------------------------------------------+------------+------------+------------+------------+
  121. | encode | 13471.32 | 12109.09 | 3876.40 | 8833.92 |
  122. +-------------------------------------------------------------------------------+------------+------------+------------+------------+
  123. | decode | 16890.63 | 8946.07 | 12218.55 | 3350.72 |
  124. +-------------------------------------------------------------------------------+------------+------------+------------+------------+
  125. | Dict with 256 arrays with 256 dict{string, int} pairs | | | | |
  126. +-------------------------------------------------------------------------------+------------+------------+------------+------------+
  127. | encode | 50.25 | 46.45 | 13.82 | 29.28 |
  128. +-------------------------------------------------------------------------------+------------+------------+------------+------------+
  129. | decode | 33.27 | 22.10 | 27.91 | 10.43 |
  130. +-------------------------------------------------------------------------------+------------+------------+------------+------------+
  131. | Dict with 256 arrays with 256 dict{string, int} pairs, outputting sorted keys | | | | |
  132. +-------------------------------------------------------------------------------+------------+------------+------------+------------+
  133. | encode | 27.19 | | 7.75 | 2.39 |
  134. +-------------------------------------------------------------------------------+------------+------------+------------+------------+
  135. | Complex object | | | | |
  136. +-------------------------------------------------------------------------------+------------+------------+------------+------------+
  137. | encode | 577.98 | | 387.81 | 470.02 |
  138. +-------------------------------------------------------------------------------+------------+------------+------------+------------+
  139. | decode | 496.73 | 234.44 | 151.00 | 145.16 |
  140. +-------------------------------------------------------------------------------+------------+------------+------------+------------+
  141. ~~~~~~~~~
  142. Versions:
  143. ~~~~~~~~~
  144. - CPython 3.4.3 (default, Oct 14 2015, 20:28:29) [GCC 4.8.4]
  145. - blist : 1.3.6
  146. - simplejson: 3.8.1
  147. - ujson : 1.34 (0c52200eb4e2d97e548a765d5f089858c41967b0)
  148. - yajl : 0.3.5
  149. +-------------------------------------------------------------------------------+------------+------------+------------+------------+
  150. | | ujson | yajl | simplejson | json |
  151. +===============================================================================+============+============+============+============+
  152. | Array with 256 doubles | | | | |
  153. +-------------------------------------------------------------------------------+------------+------------+------------+------------+
  154. | encode | 3477.15 | 5732.24 | 3016.76 | 3071.99 |
  155. +-------------------------------------------------------------------------------+------------+------------+------------+------------+
  156. | decode | 23625.20 | 9731.45 | 9501.57 | 9901.92 |
  157. +-------------------------------------------------------------------------------+------------+------------+------------+------------+
  158. | Array with 256 UTF-8 strings | | | | |
  159. +-------------------------------------------------------------------------------+------------+------------+------------+------------+
  160. | encode | 1995.89 | 2151.61 | 1771.98 | 1817.20 |
  161. +-------------------------------------------------------------------------------+------------+------------+------------+------------+
  162. | decode | 1425.04 | 625.38 | 327.14 | 305.95 |
  163. +-------------------------------------------------------------------------------+------------+------------+------------+------------+
  164. | Array with 256 strings | | | | |
  165. +-------------------------------------------------------------------------------+------------+------------+------------+------------+
  166. | encode | 25461.75 | 12188.64 | 13054.76 | 14429.81 |
  167. +-------------------------------------------------------------------------------+------------+------------+------------+------------+
  168. | decode | 21981.31 | 17014.22 | 23869.48 | 22483.58 |
  169. +-------------------------------------------------------------------------------+------------+------------+------------+------------+
  170. | Medium complex object | | | | |
  171. +-------------------------------------------------------------------------------+------------+------------+------------+------------+
  172. | encode | 10821.46 | 4837.04 | 3114.04 | 4254.46 |
  173. +-------------------------------------------------------------------------------+------------+------------+------------+------------+
  174. | decode | 7887.77 | 5126.67 | 4934.60 | 6204.97 |
  175. +-------------------------------------------------------------------------------+------------+------------+------------+------------+
  176. | Array with 256 True values | | | | |
  177. +-------------------------------------------------------------------------------+------------+------------+------------+------------+
  178. | encode | 100452.86 | 94639.42 | 46657.63 | 60358.63 |
  179. +-------------------------------------------------------------------------------+------------+------------+------------+------------+
  180. | decode | 148312.69 | 75485.90 | 88434.91 | 116395.51 |
  181. +-------------------------------------------------------------------------------+------------+------------+------------+------------+
  182. | Array with 256 dict{string, int} pairs | | | | |
  183. +-------------------------------------------------------------------------------+------------+------------+------------+------------+
  184. | encode | 11698.13 | 8886.96 | 3043.69 | 6302.35 |
  185. +-------------------------------------------------------------------------------+------------+------------+------------+------------+
  186. | decode | 10686.40 | 7061.77 | 5646.80 | 7702.29 |
  187. +-------------------------------------------------------------------------------+------------+------------+------------+------------+
  188. | Dict with 256 arrays with 256 dict{string, int} pairs | | | | |
  189. +-------------------------------------------------------------------------------+------------+------------+------------+------------+
  190. | encode | 44.26 | 34.43 | 10.40 | 21.97 |
  191. +-------------------------------------------------------------------------------+------------+------------+------------+------------+
  192. | decode | 28.46 | 23.95 | 18.70 | 22.83 |
  193. +-------------------------------------------------------------------------------+------------+------------+------------+------------+
  194. | Dict with 256 arrays with 256 dict{string, int} pairs, outputting sorted keys | | | | |
  195. +-------------------------------------------------------------------------------+------------+------------+------------+------------+
  196. | encode | 33.60 | | 6.94 | 22.34 |
  197. +-------------------------------------------------------------------------------+------------+------------+------------+------------+
  198. | Complex object | | | | |
  199. +-------------------------------------------------------------------------------+------------+------------+------------+------------+
  200. | encode | 432.30 | | 351.47 | 379.34 |
  201. +-------------------------------------------------------------------------------+------------+------------+------------+------------+
  202. | decode | 434.40 | 221.97 | 149.57 | 147.79 |
  203. +-------------------------------------------------------------------------------+------------+------------+------------+------------+