diff options
Diffstat (limited to '')
| -rw-r--r-- | README | 211 | ||||
| -rw-r--r-- | lua-cjson.spec | 2 |
2 files changed, 212 insertions, 1 deletions
| @@ -0,0 +1,211 @@ | |||
| 1 | Lua CJSON v1.0 | ||
| 2 | ============== | ||
| 3 | |||
| 4 | Lua CJSON is covered by the MIT license. See the file "LICENSE" for | ||
| 5 | details. | ||
| 6 | |||
| 7 | Lua CJSON provides fast JSON parsing and encoding support for Lua. | ||
| 8 | |||
| 9 | Features: | ||
| 10 | - 10x to 20x quicker (or more) than the fastest pure Lua JSON modules. | ||
| 11 | - Full support for JSON with UTF-8, including decoding surrogate | ||
| 12 | pairs. | ||
| 13 | - Optionally supports common JSON extensions (NaN, Inf,..). | ||
| 14 | |||
| 15 | Caveats: | ||
| 16 | - UTF-16 and UTF-32 are not supported. | ||
| 17 | - Multiple OS threads within a single Lua state are not currently | ||
| 18 | supported. | ||
| 19 | |||
| 20 | To obtain the latest version of Lua CJSON visit: | ||
| 21 | |||
| 22 | http://www.kyne.com.au/~mark/software/lua-cjson.php | ||
| 23 | |||
| 24 | Feel free to email me if you have any patches, suggestions, or comments. | ||
| 25 | |||
| 26 | - Mark Pulford <mark@kyne.com.au> | ||
| 27 | |||
| 28 | |||
| 29 | Installing | ||
| 30 | ========== | ||
| 31 | |||
| 32 | Build requirements: | ||
| 33 | - Lua (http://www.lua.org/) | ||
| 34 | Or: | ||
| 35 | - LuaJIT (http://www.luajit.org/) | ||
| 36 | |||
| 37 | The included Makefile should be reviewed and updated based on the | ||
| 38 | location of your Lua header and library directories. Then: | ||
| 39 | |||
| 40 | # make | ||
| 41 | # make install | ||
| 42 | OR | ||
| 43 | # cp cjson.so [your_module_directory] | ||
| 44 | |||
| 45 | Linux distributions using RPM should be able to build a package with | ||
| 46 | the following command: | ||
| 47 | |||
| 48 | # rpmbuild -tb lua-cjson-1.0.tar.gz | ||
| 49 | |||
| 50 | |||
| 51 | Lua CJSON API | ||
| 52 | ============= | ||
| 53 | |||
| 54 | Synopsis | ||
| 55 | -------- | ||
| 56 | |||
| 57 | require "cjson" | ||
| 58 | -- Or: | ||
| 59 | local cjson = require "cjson" | ||
| 60 | |||
| 61 | -- Translate Lua value to/from JSON | ||
| 62 | text = cjson.encode(value) | ||
| 63 | value = cjson.decode(text) | ||
| 64 | |||
| 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 | |||
| 70 | |||
| 71 | Encoding | ||
| 72 | -------- | ||
| 73 | |||
| 74 | json_text = cjson.encode(value) | ||
| 75 | |||
| 76 | cjson.encode() will serialise the following types: | ||
| 77 | * number, string, table, boolean, lightuserdata (NULL) or nil | ||
| 78 | |||
| 79 | The remaining Lua types cannot be serialised: | ||
| 80 | * thread, userdata, lightuserdata (non-NULL), function | ||
| 81 | |||
| 82 | Numbers are encoded using the standard Lua number format. | ||
| 83 | |||
| 84 | ASCII 0 - 31, double-quote, forward-slash, black-slash and ASCII 127 | ||
| 85 | are escaped when encoding strings. Other octets are passed | ||
| 86 | transparently. It is expected the application will perform UTF-8 error | ||
| 87 | checking if required. | ||
| 88 | |||
| 89 | If a Lua table only contains positive integer keys (>0) it is encoded | ||
| 90 | as an array, otherwise it will be encoded as an object. | ||
| 91 | |||
| 92 | A Lua table will only recognised as an array if all keys are type | ||
| 93 | "number", and are positive integers (>0). Otherwise CJSON will encode | ||
| 94 | the table as a JSON object. | ||
| 95 | |||
| 96 | CJSON will also recognise and handle sparse arrays. Missing entries | ||
| 97 | will be encoded as "null". Eg: | ||
| 98 | { [3] = "data" } | ||
| 99 | becomes: | ||
| 100 | [ null, null, "data" ] | ||
| 101 | |||
| 102 | Note: standards compliant JSON must be encapsulated in either an | ||
| 103 | object ({}) or an array ([]). Hence you must pass a table to | ||
| 104 | cjson.encode() if you want to generate standards compliant JSON | ||
| 105 | output. | ||
| 106 | |||
| 107 | By default, errors will be raised for: | ||
| 108 | - Excessively sparse arrays (see below) | ||
| 109 | - More than 20 nested tables | ||
| 110 | - Invalid numbers (NaN, Infinity) | ||
| 111 | |||
| 112 | These defaults can be changed with: | ||
| 113 | - cjson.encode_sparse_array() | ||
| 114 | - cjson.encode_max_depth() | ||
| 115 | - cjson.refuse_invalid_numbers() | ||
| 116 | |||
| 117 | Example: | ||
| 118 | data_obj = { true, { foo = "bar" } } | ||
| 119 | data_json = cjson.encode(data_obj) | ||
| 120 | |||
| 121 | |||
| 122 | Decoding | ||
| 123 | -------- | ||
| 124 | |||
| 125 | value = cjson.decode(json_text) | ||
| 126 | |||
| 127 | cjson.decode() will deserialise any UTF-8 JSON string into a Lua data | ||
| 128 | structure. It can return any of the types that cjson.encode() | ||
| 129 | supports. | ||
| 130 | |||
| 131 | UTF-16 and UTF-32 JSON strings are not supported. | ||
| 132 | |||
| 133 | CJSON only requires that NULL (\0) and double quote (\") are escaped | ||
| 134 | within strings. All other octets will be passed transparently. UTF-8 | ||
| 135 | characters are not validated and should be checked elsewhere if | ||
| 136 | desired. | ||
| 137 | |||
| 138 | JSON "null" will be converted to a NULL lightuserdata value. This can | ||
| 139 | be compared with cjson.null for convenience. | ||
| 140 | |||
| 141 | By default, invalid numbers (NaN, Infinity, Hex) will be decoded | ||
| 142 | correctly. | ||
| 143 | |||
| 144 | Example: | ||
| 145 | data_json = '[ true, { "foo": "bar" } ]' | ||
| 146 | data_obj = cjson.decode(data_json) | ||
| 147 | |||
| 148 | |||
| 149 | Invalid numbers | ||
| 150 | --------------- | ||
| 151 | |||
| 152 | setting = cjson.refuse_invalid_numbers([setting]) | ||
| 153 | -- "setting" must be on of: | ||
| 154 | -- false, "encode", "decode", "both", true | ||
| 155 | |||
| 156 | CJSON considers numbers which are outside the JSON specification to be | ||
| 157 | "invalid". Eg: | ||
| 158 | - Infinity | ||
| 159 | - NaN | ||
| 160 | - Hexadecimal numbers | ||
| 161 | |||
| 162 | This setting can be configured separately for encoding and/or | ||
| 163 | decoding: | ||
| 164 | - Enabled: an error will be generated if an invalid number is found. | ||
| 165 | - Disabled (encoding): NaN and Infinity can be encoded. | ||
| 166 | - Disabled (decoding): All numbers supported by strtod(3) will be | ||
| 167 | parsed. | ||
| 168 | |||
| 169 | |||
| 170 | Sparse arrays | ||
| 171 | ------------- | ||
| 172 | |||
| 173 | convert, ratio, safe = cjson.encode_sparse_array([convert[, ratio[, safe]]]) | ||
| 174 | -- "convert" must be a boolean. Default: false. | ||
| 175 | -- "ratio" must be a positive integer (>0). Default: 2 | ||
| 176 | -- "safe" must be a positive integer (>0). Default: 10 | ||
| 177 | |||
| 178 | CJSON detects excessively sparse arrays by comparing the number of | ||
| 179 | items in an array with the maximum index. An excessively sparse array | ||
| 180 | is defined as: | ||
| 181 | |||
| 182 | max_index > safe AND max_index > items * ratio | ||
| 183 | |||
| 184 | Eg: | ||
| 185 | { [1000] = "excessively sparse array" } | ||
| 186 | |||
| 187 | Setting "ratio" to 0 disables checking for excessively sparse arrays. | ||
| 188 | |||
| 189 | When "convert" is enabled, CJSON will encode excessively sparse arrays | ||
| 190 | as JSON objects. | ||
| 191 | |||
| 192 | |||
| 193 | Nested tables | ||
| 194 | ------------- | ||
| 195 | |||
| 196 | depth = cjson.encode_max_depth([depth]) | ||
| 197 | -- "depth" must be a positive integer (>0). | ||
| 198 | |||
| 199 | By default, CJSON will reject data structure with more than 20 nested | ||
| 200 | tables. | ||
| 201 | |||
| 202 | This check is used to prevent a nested data structure from crashing | ||
| 203 | the application. Eg: | ||
| 204 | a = {}; b = { a }; a[1] = b | ||
| 205 | |||
| 206 | |||
| 207 | References | ||
| 208 | ========== | ||
| 209 | |||
| 210 | - http://tools.ietf.org/html/rfc4627 | ||
| 211 | - http://www.json.org/ | ||
diff --git a/lua-cjson.spec b/lua-cjson.spec index 1398107..8f3b8c5 100644 --- a/lua-cjson.spec +++ b/lua-cjson.spec | |||
| @@ -39,7 +39,7 @@ rm -rf "$RPM_BUILD_ROOT" | |||
| 39 | 39 | ||
| 40 | %files | 40 | %files |
| 41 | %defattr(-,root,root,-) | 41 | %defattr(-,root,root,-) |
| 42 | %doc LICENSE performance.txt rfc4627.txt tests | 42 | %doc LICENSE performance.txt README rfc4627.txt tests |
| 43 | %{lualibdir}/* | 43 | %{lualibdir}/* |
| 44 | 44 | ||
| 45 | 45 | ||
