README 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259
  1. Lua CJSON v1.0.2
  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. - Optionally supports common JSON extensions (NaN, Inf,..).
  11. Caveats:
  12. - UTF-16 and UTF-32 are not supported.
  13. - Multiple OS threads within a single Lua state are not currently
  14. supported. However, this is an extremely uncommon configuration due
  15. to performance limitations.
  16. To obtain the latest version of Lua CJSON visit:
  17. http://www.kyne.com.au/~mark/software/lua-cjson.php
  18. Feel free to email me if you have any patches, suggestions, or comments.
  19. - Mark Pulford <[email protected]>
  20. Installing
  21. ==========
  22. Build requirements:
  23. - Lua (http://www.lua.org/)
  24. Or:
  25. - LuaJIT (http://www.luajit.org/)
  26. There are 3 build methods available:
  27. - Gmake: POSIX, OSX
  28. - RPM: Some Linux distributions
  29. - LuaRocks (http://www.luarocks.org/): POSIX, OSX, Windows
  30. Gmake
  31. -----
  32. Review and update the included Makefile to suit your platform. Then:
  33. # gmake
  34. # gmake install
  35. OR
  36. # cp cjson.so [your_module_directory]
  37. RPM
  38. ---
  39. Linux distributions using RPM should be able to build a package with
  40. the following command:
  41. # rpmbuild -tb lua-cjson-1.0.2.tar.gz
  42. LuaRocks
  43. --------
  44. Extract the source package into a directory and run:
  45. # cd lua-cjson-1.0.2; luarocks make
  46. Lua CJSON API
  47. =============
  48. Synopsis
  49. --------
  50. require "cjson"
  51. -- Or:
  52. local cjson = require "cjson"
  53. -- Translate Lua value to/from JSON
  54. text = cjson.encode(value)
  55. value = cjson.decode(text)
  56. -- Get and/or Set CJSON configuration
  57. setting = cjson.refuse_invalid_numbers([setting])
  58. depth = cjson.encode_max_depth([depth])
  59. convert, ratio, safe = cjson.encode_sparse_array([convert[, ratio[, safe]]])
  60. keep = cjson.encode_keep_buffer([keep])
  61. Encoding
  62. --------
  63. json_text = cjson.encode(value)
  64. cjson.encode() will serialise the following types:
  65. * number, string, table, boolean, lightuserdata (NULL) or nil
  66. The remaining Lua types cannot be serialised:
  67. * thread, userdata, lightuserdata (non-NULL), function
  68. Numbers are encoded using the standard Lua number format.
  69. ASCII 0 - 31, double-quote, forward-slash, black-slash and ASCII 127
  70. are escaped when encoding strings. Other octets are passed
  71. transparently. It is expected the application will perform UTF-8 error
  72. checking if required.
  73. If a Lua table only contains positive integer keys (>0) it is encoded
  74. as an array, otherwise it will be encoded as an object.
  75. A Lua table will only recognised as an array if all keys are type
  76. "number", and are positive integers (>0). Otherwise CJSON will encode
  77. the table as a JSON object.
  78. CJSON will also recognise and handle sparse arrays. Missing entries
  79. will be encoded as "null". Eg:
  80. { [3] = "data" }
  81. becomes:
  82. [ null, null, "data" ]
  83. Note: standards compliant JSON must be encapsulated in either an
  84. object ({}) or an array ([]). Hence you must pass a table to
  85. cjson.encode() if you want to generate standards compliant JSON
  86. output.
  87. By default, errors will be raised for:
  88. - Excessively sparse arrays (see below)
  89. - More than 20 nested tables
  90. - Invalid numbers (NaN, Infinity)
  91. These defaults can be changed with:
  92. - cjson.encode_sparse_array()
  93. - cjson.encode_max_depth()
  94. - cjson.refuse_invalid_numbers()
  95. Example:
  96. data_obj = { true, { foo = "bar" } }
  97. data_json = cjson.encode(data_obj)
  98. Decoding
  99. --------
  100. value = cjson.decode(json_text)
  101. cjson.decode() will deserialise any UTF-8 JSON string into a Lua data
  102. structure. It can return any of the types that cjson.encode()
  103. supports.
  104. UTF-16 and UTF-32 JSON strings are not supported.
  105. CJSON only requires that NULL (\0) and double quote (\") are escaped
  106. within strings. All other octets will be passed transparently. UTF-8
  107. characters are not validated and should be checked elsewhere if
  108. desired.
  109. JSON "null" will be converted to a NULL lightuserdata value. This can
  110. be compared with cjson.null for convenience.
  111. By default, invalid numbers (NaN, Infinity, Hex) will be decoded
  112. correctly.
  113. Example:
  114. data_json = '[ true, { "foo": "bar" } ]'
  115. data_obj = cjson.decode(data_json)
  116. Invalid numbers
  117. ---------------
  118. setting = cjson.refuse_invalid_numbers([setting])
  119. -- "setting" must be on of:
  120. -- false, "encode", "decode", "both", true
  121. CJSON considers numbers which are outside the JSON specification to be
  122. "invalid". Eg:
  123. - Infinity
  124. - NaN
  125. - Hexadecimal numbers
  126. This setting can be configured separately for encoding and/or
  127. decoding:
  128. - Enabled: an error will be generated if an invalid number is found.
  129. - Disabled (encoding): NaN and Infinity can be encoded.
  130. - Disabled (decoding): All numbers supported by strtod(3) will be
  131. parsed.
  132. Sparse arrays
  133. -------------
  134. convert, ratio, safe = cjson.encode_sparse_array([convert[, ratio[, safe]]])
  135. -- "convert" must be a boolean. Default: false.
  136. -- "ratio" must be a positive integer (>0). Default: 2
  137. -- "safe" must be a positive integer (>0). Default: 10
  138. CJSON detects excessively sparse arrays by comparing the number of
  139. items in an array with the maximum index. An excessively sparse array
  140. is defined as:
  141. max_index > safe AND max_index > items * ratio
  142. Eg:
  143. { [1000] = "excessively sparse array" }
  144. Setting "ratio" to 0 disables checking for excessively sparse arrays.
  145. When "convert" is enabled, CJSON will encode excessively sparse arrays
  146. as JSON objects.
  147. Nested tables
  148. -------------
  149. depth = cjson.encode_max_depth([depth])
  150. -- "depth" must be a positive integer (>0).
  151. By default, CJSON will reject data structure with more than 20 nested
  152. tables.
  153. This check is used to prevent a nested data structure from crashing
  154. the application. Eg:
  155. a = {}; b = { a }; a[1] = b
  156. Number precision
  157. ----------------
  158. precision = cjson.encode_number_precision([precision])
  159. -- "precision" must be between 1 and 14 (inclusive)
  160. By default CJSON will use up to 14 digits for precision when
  161. converting a number to text.
  162. Reducing number precision to 3 can improve performance of number
  163. heavy conversions by up to 50%.
  164. Persistent encoding buffer
  165. -------------------------
  166. keep = cjson.keep_encode_buffer([keep])
  167. -- "keep" must be a boolean
  168. By default, CJSON will reuse the JSON encoding buffer to improve
  169. performance. The buffer will grow to the largest size required and is
  170. not freed until CJSON is garbage collected. Setting this option to
  171. "false" will cause the buffer to be freed after each call to
  172. cjson.encode().
  173. References
  174. ==========
  175. - http://tools.ietf.org/html/rfc4627
  176. - http://www.json.org/