diff options
author | Mark Pulford <mark@kyne.com.au> | 2011-12-15 23:45:08 +1030 |
---|---|---|
committer | Mark Pulford <mark@kyne.com.au> | 2011-12-15 23:45:08 +1030 |
commit | 7bbb4bd8decb8239c78abe3c7f54dafb9fca8120 (patch) | |
tree | a0e556b59bfd97fd13fa24465b0bcd2da572dde8 | |
parent | 9bb1f928e03a022e7382b1d9c7b14384fe348ffb (diff) | |
download | lua-cjson-7bbb4bd8decb8239c78abe3c7f54dafb9fca8120.tar.gz lua-cjson-7bbb4bd8decb8239c78abe3c7f54dafb9fca8120.tar.bz2 lua-cjson-7bbb4bd8decb8239c78abe3c7f54dafb9fca8120.zip |
Update manual (various minor improvements)
- Fix typo (keep_encode_buffer -> encode_keep_buffer)
- Update for consistency
- Remove duplicate sparse table example
- Change manual to use horizontal item lists
-rw-r--r-- | manual.txt | 163 |
1 files changed, 79 insertions, 84 deletions
@@ -12,12 +12,12 @@ Lua CJSON provides fast JSON parsing and encoding support for Lua. | |||
12 | - Full support for JSON with UTF-8, including decoding surrogate | 12 | - Full support for JSON with UTF-8, including decoding surrogate |
13 | pairs. | 13 | pairs. |
14 | - Optional run-time support for common exceptions to the JSON | 14 | - Optional run-time support for common exceptions to the JSON |
15 | specification (NaN, Inf,..). | 15 | specification (NaN, Infinity,..). |
16 | 16 | ||
17 | .Caveats | 17 | .Caveats |
18 | - UTF-16 and UTF-32 are not supported. | 18 | - UTF-16 and UTF-32 are not supported. |
19 | - Multi-threading within a single Lua state is not supported. | 19 | - Multi-threading within a single Lua state is not supported |
20 | However, this is not a recommended configuration under Lua. | 20 | (+lua_lock+ / +lua_unlock+). |
21 | 21 | ||
22 | Lua CJSON is covered by the MIT license. Review the file +LICENSE+ for | 22 | Lua CJSON is covered by the MIT license. Review the file +LICENSE+ for |
23 | details. | 23 | details. |
@@ -37,14 +37,11 @@ http://www.luajit.org[LuaJIT] to build. | |||
37 | 37 | ||
38 | There are 4 build methods available: | 38 | There are 4 build methods available: |
39 | 39 | ||
40 | Make:: | 40 | [horizontal] |
41 | Unix (including Linux, BSD, Mac OSX & Solaris) | 41 | Make:: Unix (including Linux, BSD, Mac OSX & Solaris) |
42 | CMake:: | 42 | CMake:: Unix, Windows |
43 | Unix, Windows | 43 | RPM:: Linux |
44 | RPM:: | 44 | LuaRocks:: Unix, Windows |
45 | Linux | ||
46 | LuaRocks:: | ||
47 | Unix, Windows | ||
48 | 45 | ||
49 | 46 | ||
50 | Build options (#define) | 47 | Build options (#define) |
@@ -52,19 +49,21 @@ Build options (#define) | |||
52 | 49 | ||
53 | Lua CJSON uses +strtod+(3) and +snprintf+(3) to perform numeric | 50 | Lua CJSON uses +strtod+(3) and +snprintf+(3) to perform numeric |
54 | conversion as they are usually well supported, fast and bug free. To | 51 | conversion as they are usually well supported, fast and bug free. To |
55 | ensure JSON encoding/decoding works correctly for locales using comma | 52 | ensure JSON encoding/decoding works correctly under locales using |
56 | decimal separators, Lua CJSON may optionally be compiled with one of | 53 | comma decimal separators, Lua CJSON may optionally be compiled with |
57 | the following preprocessor macros: | 54 | one of the following preprocessor macros: |
58 | 55 | ||
59 | USE_POSIX_USELOCALE:: | 56 | [horizontal] |
60 | Thread safe. Supported by Linux and Mac OSX. Recommended where available. | 57 | USE_POSIX_USELOCALE:: Thread safe. Supported by Linux and Mac OSX. |
61 | USE_POSIX_SETLOCALE:: | 58 | Recommended where available. |
62 | Works on all ANSI C platforms. Only use with single threaded programs. | 59 | |
60 | USE_POSIX_SETLOCALE:: Use only with single threaded programs. | ||
61 | Works on all ANSI C platforms. | ||
63 | 62 | ||
64 | Also available: | 63 | Also available: |
65 | 64 | ||
66 | USE_INTERNAL_ISINF:: | 65 | [horizontal] |
67 | Workaround for Solaris platforms missing isinf(). | 66 | USE_INTERNAL_ISINF:: Workaround for Solaris platforms missing isinf(). |
68 | 67 | ||
69 | 68 | ||
70 | Make | 69 | Make |
@@ -179,9 +178,9 @@ checked elsewhere if required. | |||
179 | JSON +null+ will be converted to a NULL +lightuserdata+ value. This | 178 | JSON +null+ will be converted to a NULL +lightuserdata+ value. This |
180 | can be compared with +cjson.null+ for convenience. | 179 | can be compared with +cjson.null+ for convenience. |
181 | 180 | ||
182 | By default, _invalid_ numbers (NaN, Infinity, Hexidecimal) will be | 181 | By default, numbers incompatible with the JSON specification (NaN, |
183 | decoded. This default can be changed with | 182 | Infinity, Hexidecimal) can be decoded. This default can be changed |
184 | +cjson.refuse_invalid_numbers+. | 183 | with +cjson.refuse_invalid_numbers+. |
185 | 184 | ||
186 | .Example: Decoding | 185 | .Example: Decoding |
187 | [source,lua] | 186 | [source,lua] |
@@ -222,7 +221,8 @@ The remaining Lua types will generate an error: | |||
222 | - +thread+ | 221 | - +thread+ |
223 | - +userdata+ | 222 | - +userdata+ |
224 | 223 | ||
225 | Numbers are encoded using the standard Lua number format. | 224 | By default, numbers are encoded using the standard Lua number |
225 | +printf+(3) format ("%.14g"). | ||
226 | 226 | ||
227 | Lua CJSON will escape the following characters within each string: | 227 | Lua CJSON will escape the following characters within each string: |
228 | 228 | ||
@@ -236,16 +236,12 @@ All other characters are passed transparently. Any UTF-8 error | |||
236 | checking must be done by the application. | 236 | checking must be done by the application. |
237 | 237 | ||
238 | Lua CJSON uses a heuristic to determine whether to encode a Lua table | 238 | Lua CJSON uses a heuristic to determine whether to encode a Lua table |
239 | as a JSON array or an object. A Lua table which only has positive | 239 | as a JSON array or an object. A Lua table with only positive integers |
240 | integers (>0) keys of type +number+ will be encoded as a JSON array. | 240 | keys of type +number+ will be encoded as a JSON array. All other |
241 | All other tables will be encoded as a JSON object. | 241 | tables will be encoded as a JSON object. |
242 | |||
243 | Missing entries from sparse Lua arrays are encoded as +null+. | ||
244 | 242 | ||
245 | .Example: Encoding a sparse array | 243 | Refer to <<encode_sparse_array,+cjson.encode_sparse_array+>> for details |
246 | [source,lua] | 244 | on sparse array handling. |
247 | cjson.encode({ [3] = "data" }) | ||
248 | -- Returns: '[null,null,"data"]' | ||
249 | 245 | ||
250 | Lua CJSON does not use metamethods when serialising tables. | 246 | Lua CJSON does not use metamethods when serialising tables. |
251 | 247 | ||
@@ -256,38 +252,54 @@ JSON object keys are always strings. +cjson.encode+ can only handle | |||
256 | table keys which are type +number+ or +string+. All other types will | 252 | table keys which are type +number+ or +string+. All other types will |
257 | generate an error. | 253 | generate an error. |
258 | 254 | ||
259 | |||
260 | [NOTE] | 255 | [NOTE] |
261 | Standards compliant JSON must be encapsulated in either an | 256 | Standards compliant JSON must be encapsulated in either an object |
262 | object (+{}+) or an array (+[]+). Hence a table must be passed to | 257 | (+{}+) or an array (+[]+). Hence a table must be passed to |
263 | +cjson.encode+ to generate standards compliant JSON output. | 258 | +cjson.encode+ to generate standards compliant JSON output. |
264 | 259 | ||
265 | By default, the following will generate errors: | 260 | By default, the following Lua values will generate errors: |
266 | 261 | ||
267 | - Excessively <<sparse_arrays,sparse arrays>> | 262 | - Numbers incompatible with the JSON specification (NaN, Infinity, Hexidecimal) |
268 | - More than 20 nested tables | 263 | - Tables nested more than 20 levels deep |
269 | - Invalid numbers (NaN, Infinity) | 264 | - Excessively sparse Lua arrays |
270 | 265 | ||
271 | These defaults can be changed with: | 266 | These defaults can be changed with: |
272 | 267 | ||
273 | - +cjson.encode_sparse_array+ | 268 | - <<encode_max_depth,+cjson.encode_max_depth+>> |
274 | - +cjson.encode_max_depth+ | 269 | - <<encode_sparse_array,+cjson.encode_sparse_array+>> |
275 | - +cjson.refuse_invalid_numbers+ | 270 | - <<refuse_invalid_numbers,+cjson.refuse_invalid_numbers+>> |
276 | 271 | ||
277 | .Example: encoding | 272 | .Example: Encoding |
278 | [source,lua] | 273 | [source,lua] |
279 | value = { true, { foo = "bar" } } | 274 | value = { true, { foo = "bar" } } |
280 | json_text = cjson.encode(value) | 275 | json_text = cjson.encode(value) |
281 | -- Returns: '[true,{"foo":"bar"}]' | 276 | -- Returns: '[true,{"foo":"bar"}]' |
282 | 277 | ||
283 | 278 | ||
279 | cjson.encode_keep_buffer | ||
280 | ~~~~~~~~~~~~~~~~~~~~~~~~ | ||
281 | |||
282 | [source,lua] | ||
283 | ------------ | ||
284 | keep = cjson.encode_keep_buffer([keep]) | ||
285 | -- "keep" must be a boolean | ||
286 | ------------ | ||
287 | |||
288 | By default, Lua CJSON will reuse the JSON encoding buffer to improve | ||
289 | performance. The buffer will grow to the largest size required and is | ||
290 | not freed until the Lua CJSON module is garbage collected. Setting this | ||
291 | option to +false+ will cause the buffer to be freed after each call to | ||
292 | +cjson.encode+. | ||
293 | |||
294 | |||
295 | [[encode_max_depth]] | ||
284 | cjson.encode_max_depth | 296 | cjson.encode_max_depth |
285 | ~~~~~~~~~~~~~~~~~~~~~~ | 297 | ~~~~~~~~~~~~~~~~~~~~~~ |
286 | 298 | ||
287 | [source,lua] | 299 | [source,lua] |
288 | ------------ | 300 | ------------ |
289 | depth = cjson.encode_max_depth([depth]) | 301 | depth = cjson.encode_max_depth([depth]) |
290 | -- "depth" must be a positive integer (>0). | 302 | -- "depth" must be a positive integer |
291 | ------------ | 303 | ------------ |
292 | 304 | ||
293 | By default, Lua CJSON will reject data structure with more than 20 nested | 305 | By default, Lua CJSON will reject data structure with more than 20 nested |
@@ -319,7 +331,7 @@ Reducing number precision to _3_ can improve performance of number | |||
319 | heavy JSON conversions by up to 50%. | 331 | heavy JSON conversions by up to 50%. |
320 | 332 | ||
321 | 333 | ||
322 | [[sparse_arrays]] | 334 | [[encode_sparse_array]] |
323 | cjson.encode_sparse_array | 335 | cjson.encode_sparse_array |
324 | ~~~~~~~~~~~~~~~~~~~~~~~~~ | 336 | ~~~~~~~~~~~~~~~~~~~~~~~~~ |
325 | 337 | ||
@@ -327,27 +339,25 @@ cjson.encode_sparse_array | |||
327 | ------------ | 339 | ------------ |
328 | convert, ratio, safe = cjson.encode_sparse_array([convert[, ratio[, safe]]]) | 340 | convert, ratio, safe = cjson.encode_sparse_array([convert[, ratio[, safe]]]) |
329 | -- "convert" must be a boolean. Default: false. | 341 | -- "convert" must be a boolean. Default: false. |
330 | -- "ratio" must be a positive integer (>0). Default: 2 | 342 | -- "ratio" must be a positive integer. Default: 2 |
331 | -- "safe" must be a positive integer (>0). Default: 10 | 343 | -- "safe" must be a positive integer. Default: 10 |
332 | ------------ | 344 | ------------ |
333 | 345 | ||
334 | Lua CJSON classifies Lua tables into 3 types when encoding as a JSON | 346 | Lua CJSON classifies Lua tables into 3 types when encoding as a JSON |
335 | array. This is determined by the number of values missing for keys up | 347 | array. This is determined by the number of values missing for keys up |
336 | to the maximum index: | 348 | to the maximum index: |
337 | 349 | ||
338 | Normal:: | 350 | [horizontal] |
339 | All values are available. | 351 | Normal:: All values are available. |
340 | Sparse:: | 352 | Sparse:: At least 1 value is missing. |
341 | At least 1 value is missing. | 353 | Excessively sparse:: The number of values missing exceed the configured ratio. |
342 | Excessively sparse:: | ||
343 | The number of missing values exceed a configured number. | ||
344 | 354 | ||
345 | Lua CJSON encodes sparse Lua arrays by as JSON arrays using JSON +null+. | 355 | Lua CJSON encodes sparse Lua arrays by as JSON arrays using JSON +null+. |
346 | 356 | ||
347 | .Example: Encoding a sparse array | 357 | .Example: Encoding a sparse array |
348 | [source,lua] | 358 | [source,lua] |
349 | cjson.encode({ [3] = "sparse" }) | 359 | cjson.encode({ [3] = "data" }) |
350 | -- Returns: '[null,null,"sparse"]' | 360 | -- Returns: '[null,null,"data"]' |
351 | 361 | ||
352 | Lua CJSON checks for excessively sparse arrays when the maximum index | 362 | Lua CJSON checks for excessively sparse arrays when the maximum index |
353 | is greater an the +safe+ limit and +ratio+ is greater than +0+. An | 363 | is greater an the +safe+ limit and +ratio+ is greater than +0+. An |
@@ -366,22 +376,7 @@ cjson.encode({ [1000] = "excessively sparse" }) | |||
366 | -- Returns: '{"1000":"excessively sparse"}' | 376 | -- Returns: '{"1000":"excessively sparse"}' |
367 | 377 | ||
368 | 378 | ||
369 | cjson.keep_encode_buffer | 379 | [[refuse_invalid_numbers]] |
370 | ~~~~~~~~~~~~~~~~~~~~~~~~ | ||
371 | |||
372 | [source,lua] | ||
373 | ------------ | ||
374 | keep = cjson.keep_encode_buffer([keep]) | ||
375 | -- "keep" must be a boolean | ||
376 | ------------ | ||
377 | |||
378 | By default, Lua CJSON will reuse the JSON encoding buffer to improve | ||
379 | performance. The buffer will grow to the largest size required and is | ||
380 | not freed until the Lua CJSON module is garbage collected. Setting this | ||
381 | option to +false+ will cause the buffer to be freed after each call to | ||
382 | +cjson.encode+. | ||
383 | |||
384 | |||
385 | cjson.refuse_invalid_numbers | 380 | cjson.refuse_invalid_numbers |
386 | ~~~~~~~~~~~~~~~~~~~~~~~~~~~~ | 381 | ~~~~~~~~~~~~~~~~~~~~~~~~~~~~ |
387 | 382 | ||
@@ -392,25 +387,23 @@ setting = cjson.refuse_invalid_numbers([setting]) | |||
392 | -- false, "encode", "decode", "both", true | 387 | -- false, "encode", "decode", "both", true |
393 | ------------ | 388 | ------------ |
394 | 389 | ||
395 | Lua CJSON considers numbers which are outside the JSON specification to be | 390 | Lua CJSON can throw an error for numbers outside of the JSON |
396 | _invalid_: | 391 | specification (_invalid numbers_): |
397 | 392 | ||
398 | - Infinity | 393 | - Infinity |
399 | - NaN | 394 | - NaN |
400 | - Hexadecimal numbers | 395 | - Hexadecimal |
401 | 396 | ||
402 | By default Lua CJSON will decode _invalid_ numbers, but will refuse to | 397 | By default Lua CJSON will decode _invalid numbers_, but will refuse to |
403 | encode them. | 398 | encode them. |
404 | 399 | ||
405 | This setting can be configured separately for encoding and/or | 400 | This setting can be configured separately for encoding and/or |
406 | decoding: | 401 | decoding: |
407 | 402 | ||
408 | Enabled:: | 403 | [horizontal] |
409 | an error will be generated if an _invalid_ number is found. | 404 | Enabled:: An error will be generated if an _invalid number_ is found. |
410 | Disabled (encoding):: | 405 | Disabled for encoding:: NaN and Infinity can be encoded. |
411 | NaN and Infinity can be encoded. | 406 | Disabled for decoding:: All numbers supported by +strtod+(3) will be parsed. |
412 | Disabled (decoding):: | ||
413 | All numbers supported by +strtod+(3) will be parsed. | ||
414 | 407 | ||
415 | 408 | ||
416 | API (Variables) | 409 | API (Variables) |
@@ -419,8 +412,8 @@ API (Variables) | |||
419 | cjson.null | 412 | cjson.null |
420 | ~~~~~~~~~~ | 413 | ~~~~~~~~~~ |
421 | 414 | ||
422 | Lua CJSON decodes JSON +null+ as a Lua lightuserdata NULL pointer. | 415 | Lua CJSON decodes JSON +null+ as a Lua +lightuserdata+ NULL pointer. |
423 | +cjson.null+ can be used for comparison. | 416 | +cjson.null+ is provided for comparison. |
424 | 417 | ||
425 | 418 | ||
426 | cjson.version | 419 | cjson.version |
@@ -436,3 +429,5 @@ References | |||
436 | 429 | ||
437 | - http://tools.ietf.org/html/rfc4627[RFC 4627] | 430 | - http://tools.ietf.org/html/rfc4627[RFC 4627] |
438 | - http://www.json.org/[JSON website] | 431 | - http://www.json.org/[JSON website] |
432 | |||
433 | // vi:tw=70: | ||