README 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211
  1. Lua CJSON v1.0
  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.
  15. To obtain the latest version of Lua CJSON visit:
  16. http://www.kyne.com.au/~mark/software/lua-cjson.php
  17. Feel free to email me if you have any patches, suggestions, or comments.
  18. - Mark Pulford <[email protected]>
  19. Installing
  20. ==========
  21. Build requirements:
  22. - Lua (http://www.lua.org/)
  23. Or:
  24. - LuaJIT (http://www.luajit.org/)
  25. The included Makefile should be reviewed and updated based on the
  26. location of your Lua header and library directories. Then:
  27. # make
  28. # make install
  29. OR
  30. # cp cjson.so [your_module_directory]
  31. Linux distributions using RPM should be able to build a package with
  32. the following command:
  33. # rpmbuild -tb lua-cjson-1.0.tar.gz
  34. Lua CJSON API
  35. =============
  36. Synopsis
  37. --------
  38. require "cjson"
  39. -- Or:
  40. local cjson = require "cjson"
  41. -- Translate Lua value to/from JSON
  42. text = cjson.encode(value)
  43. value = cjson.decode(text)
  44. -- Get and/or Set CJSON configuration
  45. setting = cjson.refuse_invalid_numbers([setting])
  46. depth = cjson.encode_max_depth([depth])
  47. convert, ratio, safe = cjson.encode_sparse_array([convert[, ratio[, safe]]])
  48. Encoding
  49. --------
  50. json_text = cjson.encode(value)
  51. cjson.encode() will serialise the following types:
  52. * number, string, table, boolean, lightuserdata (NULL) or nil
  53. The remaining Lua types cannot be serialised:
  54. * thread, userdata, lightuserdata (non-NULL), function
  55. Numbers are encoded using the standard Lua number format.
  56. ASCII 0 - 31, double-quote, forward-slash, black-slash and ASCII 127
  57. are escaped when encoding strings. Other octets are passed
  58. transparently. It is expected the application will perform UTF-8 error
  59. checking if required.
  60. If a Lua table only contains positive integer keys (>0) it is encoded
  61. as an array, otherwise it will be encoded as an object.
  62. A Lua table will only recognised as an array if all keys are type
  63. "number", and are positive integers (>0). Otherwise CJSON will encode
  64. the table as a JSON object.
  65. CJSON will also recognise and handle sparse arrays. Missing entries
  66. will be encoded as "null". Eg:
  67. { [3] = "data" }
  68. becomes:
  69. [ null, null, "data" ]
  70. Note: standards compliant JSON must be encapsulated in either an
  71. object ({}) or an array ([]). Hence you must pass a table to
  72. cjson.encode() if you want to generate standards compliant JSON
  73. output.
  74. By default, errors will be raised for:
  75. - Excessively sparse arrays (see below)
  76. - More than 20 nested tables
  77. - Invalid numbers (NaN, Infinity)
  78. These defaults can be changed with:
  79. - cjson.encode_sparse_array()
  80. - cjson.encode_max_depth()
  81. - cjson.refuse_invalid_numbers()
  82. Example:
  83. data_obj = { true, { foo = "bar" } }
  84. data_json = cjson.encode(data_obj)
  85. Decoding
  86. --------
  87. value = cjson.decode(json_text)
  88. cjson.decode() will deserialise any UTF-8 JSON string into a Lua data
  89. structure. It can return any of the types that cjson.encode()
  90. supports.
  91. UTF-16 and UTF-32 JSON strings are not supported.
  92. CJSON only requires that NULL (\0) and double quote (\") are escaped
  93. within strings. All other octets will be passed transparently. UTF-8
  94. characters are not validated and should be checked elsewhere if
  95. desired.
  96. JSON "null" will be converted to a NULL lightuserdata value. This can
  97. be compared with cjson.null for convenience.
  98. By default, invalid numbers (NaN, Infinity, Hex) will be decoded
  99. correctly.
  100. Example:
  101. data_json = '[ true, { "foo": "bar" } ]'
  102. data_obj = cjson.decode(data_json)
  103. Invalid numbers
  104. ---------------
  105. setting = cjson.refuse_invalid_numbers([setting])
  106. -- "setting" must be on of:
  107. -- false, "encode", "decode", "both", true
  108. CJSON considers numbers which are outside the JSON specification to be
  109. "invalid". Eg:
  110. - Infinity
  111. - NaN
  112. - Hexadecimal numbers
  113. This setting can be configured separately for encoding and/or
  114. decoding:
  115. - Enabled: an error will be generated if an invalid number is found.
  116. - Disabled (encoding): NaN and Infinity can be encoded.
  117. - Disabled (decoding): All numbers supported by strtod(3) will be
  118. parsed.
  119. Sparse arrays
  120. -------------
  121. convert, ratio, safe = cjson.encode_sparse_array([convert[, ratio[, safe]]])
  122. -- "convert" must be a boolean. Default: false.
  123. -- "ratio" must be a positive integer (>0). Default: 2
  124. -- "safe" must be a positive integer (>0). Default: 10
  125. CJSON detects excessively sparse arrays by comparing the number of
  126. items in an array with the maximum index. An excessively sparse array
  127. is defined as:
  128. max_index > safe AND max_index > items * ratio
  129. Eg:
  130. { [1000] = "excessively sparse array" }
  131. Setting "ratio" to 0 disables checking for excessively sparse arrays.
  132. When "convert" is enabled, CJSON will encode excessively sparse arrays
  133. as JSON objects.
  134. Nested tables
  135. -------------
  136. depth = cjson.encode_max_depth([depth])
  137. -- "depth" must be a positive integer (>0).
  138. By default, CJSON will reject data structure with more than 20 nested
  139. tables.
  140. This check is used to prevent a nested data structure from crashing
  141. the application. Eg:
  142. a = {}; b = { a }; a[1] = b
  143. References
  144. ==========
  145. - http://tools.ietf.org/html/rfc4627
  146. - http://www.json.org/