README 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316
  1. Lua CJSON v1.0.3
  2. ================
  3. Lua CJSON is covered by the MIT license. See the file "LICENSE" for
  4. details.
  5. Lua CJSON provides fast JSON parsing and encoding support for Lua.
  6. Features:
  7. - 10x to 20x quicker (or more) than the fastest pure Lua JSON modules.
  8. - Full support for JSON with UTF-8, including decoding surrogate
  9. pairs.
  10. - Optional run-time support for common exceptions to the JSON
  11. specification (NaN, Inf,..).
  12. Caveats:
  13. - UTF-16 and UTF-32 are not supported.
  14. - Multiple OS threads within a single Lua state are not currently
  15. supported. However, this is an extremely uncommon configuration due
  16. to performance limitations.
  17. To obtain the latest version of Lua CJSON visit:
  18. http://www.kyne.com.au/~mark/software/lua-cjson.php
  19. Feel free to email me if you have any patches, suggestions, or
  20. comments.
  21. - Mark Pulford <[email protected]>
  22. Installing
  23. ==========
  24. Build requirements:
  25. - Lua (http://www.lua.org/)
  26. Or:
  27. - LuaJIT (http://www.luajit.org/)
  28. There are 3 build methods available:
  29. - Gmake: POSIX, OSX
  30. - RPM: Various Linux distributions
  31. - LuaRocks (http://www.luarocks.org/): POSIX, OSX, Windows
  32. Gmake
  33. -----
  34. Review and update the included Makefile to suit your platform. Next,
  35. build and install the module:
  36. # gmake
  37. # gmake install
  38. OR
  39. # cp cjson.so [your_module_directory]
  40. Note: Some Solaris platforms are missing isinf(). You can work around
  41. this bug by adding -DMISSING_ISINF to CFLAGS in the Makefile.
  42. RPM
  43. ---
  44. RPM-based Linux distributions should be able to create a package using
  45. the included RPM spec file. Install the "rpm-build" package, or
  46. similar, then:
  47. # rpmbuild -tb lua-cjson-1.0.3.tar.gz
  48. LuaRocks
  49. --------
  50. LuaRocks (http://luarocks.org/) can be used to install and manage Lua
  51. modules on a wide range of platforms (including Windows).
  52. Extract the Lua CJSON source package into a directory and run:
  53. # cd lua-cjson-1.0.3; luarocks make
  54. See the LuaRocks documentation for further details.
  55. Lua CJSON API
  56. =============
  57. Synopsis
  58. --------
  59. require "cjson"
  60. -- Or:
  61. local cjson = require "cjson"
  62. -- Translate Lua value to/from JSON
  63. text = cjson.encode(value)
  64. value = cjson.decode(text)
  65. -- Get and/or set CJSON configuration
  66. setting = cjson.refuse_invalid_numbers([setting])
  67. depth = cjson.encode_max_depth([depth])
  68. convert, ratio, safe = cjson.encode_sparse_array([convert[, ratio[, safe]]])
  69. keep = cjson.encode_keep_buffer([keep])
  70. Encoding
  71. --------
  72. json_text = cjson.encode(value)
  73. cjson.encode() will serialise the following types:
  74. * number, string, table, boolean, lightuserdata (NULL) or nil
  75. The remaining Lua types cannot be serialised:
  76. * thread, userdata, lightuserdata (non-NULL), function
  77. Numbers are encoded using the standard Lua number format.
  78. ASCII 0 - 31, double-quote, forward-slash, black-slash and ASCII 127
  79. are escaped when encoding strings. Other octets are passed
  80. transparently. It is expected the application will perform UTF-8 error
  81. checking if required.
  82. A Lua table will only be recognised as an array if all keys are type
  83. "number" and are positive integers (>0). Otherwise CJSON will encode
  84. the table as a JSON object.
  85. CJSON will also recognise and handle sparse arrays. Missing entries
  86. will be encoded as "null". Eg:
  87. { [3] = "data" }
  88. becomes:
  89. [null,null,"data"]
  90. Note: standards compliant JSON must be encapsulated in either an
  91. object ({}) or an array ([]). You must pass a table to cjson.encode()
  92. if you want to generate standards compliant JSON output.
  93. By default, errors will be raised for:
  94. - Excessively sparse arrays (see below)
  95. - More than 20 nested tables
  96. - Invalid numbers (NaN, Infinity)
  97. These defaults can be changed with:
  98. - cjson.encode_sparse_array()
  99. - cjson.encode_max_depth()
  100. - cjson.refuse_invalid_numbers()
  101. Example:
  102. data_obj = { true, { foo = "bar" } }
  103. data_json = cjson.encode(data_obj)
  104. Decoding
  105. --------
  106. value = cjson.decode(json_text)
  107. cjson.decode() will deserialise any UTF-8 JSON string into a Lua data
  108. structure. It can return any of the types that cjson.encode()
  109. supports.
  110. UTF-16 and UTF-32 JSON strings are not supported.
  111. CJSON requires that NULL (\0) and double quote (\") are escaped within
  112. strings. All escape codes will be decoded and other characters will be
  113. passed transparently. UTF-8 characters are not validated during
  114. decoding and should be checked elsewhere if required.
  115. JSON "null" will be converted to a NULL lightuserdata value. This can
  116. be compared with cjson.null for convenience.
  117. By default, invalid numbers (NaN, Infinity, Hexidecimal) will be
  118. decoded.
  119. Example:
  120. data_json = '[ true, { "foo": "bar" } ]'
  121. data_obj = cjson.decode(data_json)
  122. Invalid numbers
  123. ---------------
  124. setting = cjson.refuse_invalid_numbers([setting])
  125. -- "setting" must be on of:
  126. -- false, "encode", "decode", "both", true
  127. CJSON considers numbers which are outside the JSON specification to be
  128. "invalid". Eg:
  129. - Infinity
  130. - NaN
  131. - Hexadecimal numbers
  132. By default CJSON will decode "invalid" numbers, but will refuse to
  133. encode them.
  134. This setting can be configured separately for encoding and/or
  135. decoding:
  136. - Enabled: an error will be generated if an invalid number is found.
  137. - Disabled (encoding): NaN and Infinity can be encoded.
  138. - Disabled (decoding): All numbers supported by strtod(3) will be
  139. parsed.
  140. Sparse arrays
  141. -------------
  142. convert, ratio, safe = cjson.encode_sparse_array([convert[, ratio[, safe]]])
  143. -- "convert" must be a boolean. Default: false.
  144. -- "ratio" must be a positive integer (>0). Default: 2
  145. -- "safe" must be a positive integer (>0). Default: 10
  146. A Lua array is sparse if it is missing a value for at least 1 index.
  147. Lua CJSON encodes missing values as "null". Eg:
  148. Lua array: { [3] = "sparse" }
  149. JSON array: [null,null,"sparse"]
  150. CJSON detects excessively sparse arrays by comparing the number of
  151. items in a Lua array with the maximum index. In particular:
  152. maximum index > safe AND maximum index > array_items * ratio
  153. By default, attempting to encode excessively sparse arrays will
  154. generate an error.
  155. If "convert" is set to "true", excessively sparse arrays will be
  156. encoded as a JSON object:
  157. Lua array: { [1000] = "excessively sparse" }
  158. JSON array: {"1000":"excessively sparse"}
  159. Setting "ratio" to 0 disables checking for excessively sparse arrays.
  160. Nested tables
  161. -------------
  162. depth = cjson.encode_max_depth([depth])
  163. -- "depth" must be a positive integer (>0).
  164. By default, CJSON will reject data structure with more than 20 nested
  165. tables.
  166. This check is used to prevent a nested data structure from crashing
  167. the application. Eg:
  168. a = {}; b = { a }; a[1] = b
  169. Number precision
  170. ----------------
  171. precision = cjson.encode_number_precision([precision])
  172. -- "precision" must be between 1 and 14 (inclusive)
  173. By default CJSON will output 14 significant digits when converting a
  174. number to text.
  175. Reducing number precision to 3 can improve performance of number
  176. heavy conversions by up to 50%.
  177. Persistent encoding buffer
  178. --------------------------
  179. keep = cjson.keep_encode_buffer([keep])
  180. -- "keep" must be a boolean
  181. By default, CJSON will reuse the JSON encoding buffer to improve
  182. performance. The buffer will grow to the largest size required and is
  183. not freed until CJSON is garbage collected. Setting this option to
  184. "false" will cause the buffer to be freed after each call to
  185. cjson.encode().
  186. Lua / JSON limitations and CJSON
  187. ================================
  188. Null handling
  189. -------------
  190. Lua CJSON decodes JSON "null" as a Lua lightuserdata NULL pointer.
  191. CJSON provides "cjson.null" as a convenience for comparison.
  192. Table keys
  193. ----------
  194. JSON object keys must be strings - other types are not supported. Lua
  195. CJSON will convert numeric keys to a string, and other non-string
  196. types will generate an error.
  197. JSON object keys are always be decoded as Lua strings.
  198. If all Lua table keys are numbers (not strings), Lua CJSON will
  199. encode the table as a JSON array. See "Sparse arrays" above for
  200. more details.
  201. Metamethods
  202. -----------
  203. Lua CJSON does not use metamethods when serialising tables.
  204. - next() is used to iterate over tables.
  205. - rawget() is used when iterating over arrays.
  206. Functions, Userdata, Threads
  207. ----------------------------
  208. Lua CJSON will generate an error if asked to serialise Lua functions,
  209. userdata, lightuserdata or threads.
  210. References
  211. ==========
  212. - http://tools.ietf.org/html/rfc4627
  213. - http://www.json.org/