diff options
author | Mark Pulford <mark@kyne.com.au> | 2011-09-15 20:42:13 +0930 |
---|---|---|
committer | Mark Pulford <mark@kyne.com.au> | 2011-09-15 20:42:13 +0930 |
commit | 62ec85b44f3050a975f75f0c878871d4f1fbac7c (patch) | |
tree | a9dd22ea4ed63e26da3e9866509ab4b479ac426d | |
parent | d49e97d59f9c7ca1882279a15d8345595b19ef11 (diff) | |
download | lua-cjson-62ec85b44f3050a975f75f0c878871d4f1fbac7c.tar.gz lua-cjson-62ec85b44f3050a975f75f0c878871d4f1fbac7c.tar.bz2 lua-cjson-62ec85b44f3050a975f75f0c878871d4f1fbac7c.zip |
Add limitations section to documentation
Improve text for clarity.
-rw-r--r-- | README | 124 |
1 files changed, 89 insertions, 35 deletions
@@ -10,7 +10,8 @@ Features: | |||
10 | - 10x to 20x quicker (or more) than the fastest pure Lua JSON modules. | 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 | 11 | - Full support for JSON with UTF-8, including decoding surrogate |
12 | pairs. | 12 | pairs. |
13 | - Optionally supports common JSON extensions (NaN, Inf,..). | 13 | - Optional run-time support for common exceptions to the JSON |
14 | specification (NaN, Inf,..). | ||
14 | 15 | ||
15 | Caveats: | 16 | Caveats: |
16 | - UTF-16 and UTF-32 are not supported. | 17 | - UTF-16 and UTF-32 are not supported. |
@@ -22,7 +23,8 @@ To obtain the latest version of Lua CJSON visit: | |||
22 | 23 | ||
23 | http://www.kyne.com.au/~mark/software/lua-cjson.php | 24 | http://www.kyne.com.au/~mark/software/lua-cjson.php |
24 | 25 | ||
25 | Feel free to email me if you have any patches, suggestions, or comments. | 26 | Feel free to email me if you have any patches, suggestions, or |
27 | comments. | ||
26 | 28 | ||
27 | - Mark Pulford <mark@kyne.com.au> | 29 | - Mark Pulford <mark@kyne.com.au> |
28 | 30 | ||
@@ -37,14 +39,15 @@ Or: | |||
37 | 39 | ||
38 | There are 3 build methods available: | 40 | There are 3 build methods available: |
39 | - Gmake: POSIX, OSX | 41 | - Gmake: POSIX, OSX |
40 | - RPM: Some Linux distributions | 42 | - RPM: Various Linux distributions |
41 | - LuaRocks (http://www.luarocks.org/): POSIX, OSX, Windows | 43 | - LuaRocks (http://www.luarocks.org/): POSIX, OSX, Windows |
42 | 44 | ||
43 | 45 | ||
44 | Gmake | 46 | Gmake |
45 | ----- | 47 | ----- |
46 | 48 | ||
47 | Review and update the included Makefile to suit your platform. Then: | 49 | Review and update the included Makefile to suit your platform. Next, |
50 | build and install the module: | ||
48 | 51 | ||
49 | # gmake | 52 | # gmake |
50 | # gmake install | 53 | # gmake install |
@@ -58,8 +61,9 @@ Note: Some Solaris platforms are missing isinf(). You can work around | |||
58 | RPM | 61 | RPM |
59 | --- | 62 | --- |
60 | 63 | ||
61 | Linux distributions using RPM should be able to build a package with | 64 | RPM-based Linux distributions should be able to create a package using |
62 | the following command: | 65 | the included RPM spec file. Install the "rpm-build" package, or |
66 | similar, then: | ||
63 | 67 | ||
64 | # rpmbuild -tb lua-cjson-1.0.3.tar.gz | 68 | # rpmbuild -tb lua-cjson-1.0.3.tar.gz |
65 | 69 | ||
@@ -67,10 +71,15 @@ the following command: | |||
67 | LuaRocks | 71 | LuaRocks |
68 | -------- | 72 | -------- |
69 | 73 | ||
70 | Extract the source package into a directory and run: | 74 | LuaRocks (http://luarocks.org/) can be used to install and manage Lua |
75 | modules on a wide range of platforms (including Windows). | ||
76 | |||
77 | Extract the Lua CJSON source package into a directory and run: | ||
71 | 78 | ||
72 | # cd lua-cjson-1.0.3; luarocks make | 79 | # cd lua-cjson-1.0.3; luarocks make |
73 | 80 | ||
81 | See the LuaRocks documentation for further details. | ||
82 | |||
74 | 83 | ||
75 | Lua CJSON API | 84 | Lua CJSON API |
76 | ============= | 85 | ============= |
@@ -86,7 +95,7 @@ Synopsis | |||
86 | text = cjson.encode(value) | 95 | text = cjson.encode(value) |
87 | value = cjson.decode(text) | 96 | value = cjson.decode(text) |
88 | 97 | ||
89 | -- Get and/or Set CJSON configuration | 98 | -- Get and/or set CJSON configuration |
90 | setting = cjson.refuse_invalid_numbers([setting]) | 99 | setting = cjson.refuse_invalid_numbers([setting]) |
91 | depth = cjson.encode_max_depth([depth]) | 100 | depth = cjson.encode_max_depth([depth]) |
92 | convert, ratio, safe = cjson.encode_sparse_array([convert[, ratio[, safe]]]) | 101 | convert, ratio, safe = cjson.encode_sparse_array([convert[, ratio[, safe]]]) |
@@ -111,23 +120,19 @@ are escaped when encoding strings. Other octets are passed | |||
111 | transparently. It is expected the application will perform UTF-8 error | 120 | transparently. It is expected the application will perform UTF-8 error |
112 | checking if required. | 121 | checking if required. |
113 | 122 | ||
114 | If a Lua table only contains positive integer keys (>0) it is encoded | 123 | A Lua table will only be recognised as an array if all keys are type |
115 | as an array, otherwise it will be encoded as an object. | 124 | "number" and are positive integers (>0). Otherwise CJSON will encode |
116 | |||
117 | A Lua table will only recognised as an array if all keys are type | ||
118 | "number", and are positive integers (>0). Otherwise CJSON will encode | ||
119 | the table as a JSON object. | 125 | the table as a JSON object. |
120 | 126 | ||
121 | CJSON will also recognise and handle sparse arrays. Missing entries | 127 | CJSON will also recognise and handle sparse arrays. Missing entries |
122 | will be encoded as "null". Eg: | 128 | will be encoded as "null". Eg: |
123 | { [3] = "data" } | 129 | { [3] = "data" } |
124 | becomes: | 130 | becomes: |
125 | [ null, null, "data" ] | 131 | [null,null,"data"] |
126 | 132 | ||
127 | Note: standards compliant JSON must be encapsulated in either an | 133 | Note: standards compliant JSON must be encapsulated in either an |
128 | object ({}) or an array ([]). Hence you must pass a table to | 134 | object ({}) or an array ([]). You must pass a table to cjson.encode() |
129 | cjson.encode() if you want to generate standards compliant JSON | 135 | if you want to generate standards compliant JSON output. |
130 | output. | ||
131 | 136 | ||
132 | By default, errors will be raised for: | 137 | By default, errors will be raised for: |
133 | - Excessively sparse arrays (see below) | 138 | - Excessively sparse arrays (see below) |
@@ -155,16 +160,16 @@ supports. | |||
155 | 160 | ||
156 | UTF-16 and UTF-32 JSON strings are not supported. | 161 | UTF-16 and UTF-32 JSON strings are not supported. |
157 | 162 | ||
158 | CJSON only requires that NULL (\0) and double quote (\") are escaped | 163 | CJSON requires that NULL (\0) and double quote (\") are escaped within |
159 | within strings. All other octets will be passed transparently. UTF-8 | 164 | strings. All escape codes will be decoded and other characters will be |
160 | characters are not validated and should be checked elsewhere if | 165 | passed transparently. UTF-8 characters are not validated during |
161 | desired. | 166 | decoding and should be checked elsewhere if required. |
162 | 167 | ||
163 | JSON "null" will be converted to a NULL lightuserdata value. This can | 168 | JSON "null" will be converted to a NULL lightuserdata value. This can |
164 | be compared with cjson.null for convenience. | 169 | be compared with cjson.null for convenience. |
165 | 170 | ||
166 | By default, invalid numbers (NaN, Infinity, Hex) will be decoded | 171 | By default, invalid numbers (NaN, Infinity, Hexidecimal) will be |
167 | correctly. | 172 | decoded. |
168 | 173 | ||
169 | Example: | 174 | Example: |
170 | data_json = '[ true, { "foo": "bar" } ]' | 175 | data_json = '[ true, { "foo": "bar" } ]' |
@@ -184,10 +189,13 @@ CJSON considers numbers which are outside the JSON specification to be | |||
184 | - NaN | 189 | - NaN |
185 | - Hexadecimal numbers | 190 | - Hexadecimal numbers |
186 | 191 | ||
192 | By default CJSON will decode "invalid" numbers, but will refuse to | ||
193 | encode them. | ||
194 | |||
187 | This setting can be configured separately for encoding and/or | 195 | This setting can be configured separately for encoding and/or |
188 | decoding: | 196 | decoding: |
189 | - Enabled: an error will be generated if an invalid number is found. | 197 | - Enabled: an error will be generated if an invalid number is found. |
190 | - Disabled (encoding): NaN and Infinity can be encoded. | 198 | - Disabled (encoding): NaN and Infinity can be encoded. |
191 | - Disabled (decoding): All numbers supported by strtod(3) will be | 199 | - Disabled (decoding): All numbers supported by strtod(3) will be |
192 | parsed. | 200 | parsed. |
193 | 201 | ||
@@ -200,19 +208,25 @@ Sparse arrays | |||
200 | -- "ratio" must be a positive integer (>0). Default: 2 | 208 | -- "ratio" must be a positive integer (>0). Default: 2 |
201 | -- "safe" must be a positive integer (>0). Default: 10 | 209 | -- "safe" must be a positive integer (>0). Default: 10 |
202 | 210 | ||
211 | A Lua array is sparse if it is missing a value for at least 1 index. | ||
212 | Lua CJSON encodes missing values as "null". Eg: | ||
213 | Lua array: { [3] = "sparse" } | ||
214 | JSON array: [null,null,"sparse"] | ||
215 | |||
203 | CJSON detects excessively sparse arrays by comparing the number of | 216 | CJSON detects excessively sparse arrays by comparing the number of |
204 | items in an array with the maximum index. An excessively sparse array | 217 | items in a Lua array with the maximum index. In particular: |
205 | is defined as: | ||
206 | 218 | ||
207 | max_index > safe AND max_index > items * ratio | 219 | maximum index > safe AND maximum index > array_items * ratio |
208 | 220 | ||
209 | Eg: | 221 | By default, attempting to encode excessively sparse arrays will |
210 | { [1000] = "excessively sparse array" } | 222 | generate an error. |
211 | 223 | ||
212 | Setting "ratio" to 0 disables checking for excessively sparse arrays. | 224 | If "convert" is set to "true", excessively sparse arrays will be |
225 | encoded as a JSON object: | ||
226 | Lua array: { [1000] = "excessively sparse" } | ||
227 | JSON array: {"1000":"excessively sparse"} | ||
213 | 228 | ||
214 | When "convert" is enabled, CJSON will encode excessively sparse arrays | 229 | Setting "ratio" to 0 disables checking for excessively sparse arrays. |
215 | as JSON objects. | ||
216 | 230 | ||
217 | 231 | ||
218 | Nested tables | 232 | Nested tables |
@@ -235,15 +249,15 @@ Number precision | |||
235 | precision = cjson.encode_number_precision([precision]) | 249 | precision = cjson.encode_number_precision([precision]) |
236 | -- "precision" must be between 1 and 14 (inclusive) | 250 | -- "precision" must be between 1 and 14 (inclusive) |
237 | 251 | ||
238 | By default CJSON will use up to 14 digits for precision when | 252 | By default CJSON will output 14 significant digits when converting a |
239 | converting a number to text. | 253 | number to text. |
240 | 254 | ||
241 | Reducing number precision to 3 can improve performance of number | 255 | Reducing number precision to 3 can improve performance of number |
242 | heavy conversions by up to 50%. | 256 | heavy conversions by up to 50%. |
243 | 257 | ||
244 | 258 | ||
245 | Persistent encoding buffer | 259 | Persistent encoding buffer |
246 | ------------------------- | 260 | -------------------------- |
247 | 261 | ||
248 | keep = cjson.keep_encode_buffer([keep]) | 262 | keep = cjson.keep_encode_buffer([keep]) |
249 | -- "keep" must be a boolean | 263 | -- "keep" must be a boolean |
@@ -255,6 +269,46 @@ not freed until CJSON is garbage collected. Setting this option to | |||
255 | cjson.encode(). | 269 | cjson.encode(). |
256 | 270 | ||
257 | 271 | ||
272 | Lua / JSON limitations and CJSON | ||
273 | ================================ | ||
274 | |||
275 | Null handling | ||
276 | ------------- | ||
277 | |||
278 | Lua CJSON decodes JSON "null" as a Lua lightuserdata NULL pointer. | ||
279 | |||
280 | CJSON provides "cjson.null" as a convenience for comparison. | ||
281 | |||
282 | |||
283 | Table keys | ||
284 | ---------- | ||
285 | |||
286 | JSON object keys must be strings - other types are not supported. Lua | ||
287 | CJSON will convert numeric keys to a string, and other non-string | ||
288 | types will generate an error. | ||
289 | |||
290 | JSON object keys are always be decoded as Lua strings. | ||
291 | |||
292 | If all Lua table keys are numbers (not strings), Lua CJSON will | ||
293 | encode the table as a JSON array. See "Sparse arrays" above for | ||
294 | more details. | ||
295 | |||
296 | |||
297 | Metamethods | ||
298 | ----------- | ||
299 | |||
300 | Lua CJSON does not use metamethods when serialising tables. | ||
301 | - next() is used to iterate over tables. | ||
302 | - rawget() is used when iterating over arrays. | ||
303 | |||
304 | |||
305 | Functions, Userdata, Threads | ||
306 | ---------------------------- | ||
307 | |||
308 | Lua CJSON will generate an error if asked to serialise Lua functions, | ||
309 | userdata, lightuserdata or threads. | ||
310 | |||
311 | |||
258 | References | 312 | References |
259 | ========== | 313 | ========== |
260 | 314 | ||