aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMark Pulford <mark@kyne.com.au>2011-12-15 23:45:08 +1030
committerMark Pulford <mark@kyne.com.au>2011-12-15 23:45:08 +1030
commit7bbb4bd8decb8239c78abe3c7f54dafb9fca8120 (patch)
treea0e556b59bfd97fd13fa24465b0bcd2da572dde8
parent9bb1f928e03a022e7382b1d9c7b14384fe348ffb (diff)
downloadlua-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.txt163
1 files changed, 79 insertions, 84 deletions
diff --git a/manual.txt b/manual.txt
index 39adef8..bb946fe 100644
--- a/manual.txt
+++ b/manual.txt
@@ -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
22Lua CJSON is covered by the MIT license. Review the file +LICENSE+ for 22Lua CJSON is covered by the MIT license. Review the file +LICENSE+ for
23details. 23details.
@@ -37,14 +37,11 @@ http://www.luajit.org[LuaJIT] to build.
37 37
38There are 4 build methods available: 38There are 4 build methods available:
39 39
40Make:: 40[horizontal]
41 Unix (including Linux, BSD, Mac OSX & Solaris) 41Make:: Unix (including Linux, BSD, Mac OSX & Solaris)
42CMake:: 42CMake:: Unix, Windows
43 Unix, Windows 43RPM:: Linux
44RPM:: 44LuaRocks:: Unix, Windows
45 Linux
46LuaRocks::
47 Unix, Windows
48 45
49 46
50Build options (#define) 47Build options (#define)
@@ -52,19 +49,21 @@ Build options (#define)
52 49
53Lua CJSON uses +strtod+(3) and +snprintf+(3) to perform numeric 50Lua CJSON uses +strtod+(3) and +snprintf+(3) to perform numeric
54conversion as they are usually well supported, fast and bug free. To 51conversion as they are usually well supported, fast and bug free. To
55ensure JSON encoding/decoding works correctly for locales using comma 52ensure JSON encoding/decoding works correctly under locales using
56decimal separators, Lua CJSON may optionally be compiled with one of 53comma decimal separators, Lua CJSON may optionally be compiled with
57the following preprocessor macros: 54one of the following preprocessor macros:
58 55
59USE_POSIX_USELOCALE:: 56[horizontal]
60 Thread safe. Supported by Linux and Mac OSX. Recommended where available. 57USE_POSIX_USELOCALE:: Thread safe. Supported by Linux and Mac OSX.
61USE_POSIX_SETLOCALE:: 58Recommended where available.
62 Works on all ANSI C platforms. Only use with single threaded programs. 59
60USE_POSIX_SETLOCALE:: Use only with single threaded programs.
61Works on all ANSI C platforms.
63 62
64Also available: 63Also available:
65 64
66USE_INTERNAL_ISINF:: 65[horizontal]
67 Workaround for Solaris platforms missing isinf(). 66USE_INTERNAL_ISINF:: Workaround for Solaris platforms missing isinf().
68 67
69 68
70Make 69Make
@@ -179,9 +178,9 @@ checked elsewhere if required.
179JSON +null+ will be converted to a NULL +lightuserdata+ value. This 178JSON +null+ will be converted to a NULL +lightuserdata+ value. This
180can be compared with +cjson.null+ for convenience. 179can be compared with +cjson.null+ for convenience.
181 180
182By default, _invalid_ numbers (NaN, Infinity, Hexidecimal) will be 181By default, numbers incompatible with the JSON specification (NaN,
183decoded. This default can be changed with 182Infinity, Hexidecimal) can be decoded. This default can be changed
184+cjson.refuse_invalid_numbers+. 183with +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
225Numbers are encoded using the standard Lua number format. 224By default, numbers are encoded using the standard Lua number
225+printf+(3) format ("%.14g").
226 226
227Lua CJSON will escape the following characters within each string: 227Lua 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
236checking must be done by the application. 236checking must be done by the application.
237 237
238Lua CJSON uses a heuristic to determine whether to encode a Lua table 238Lua CJSON uses a heuristic to determine whether to encode a Lua table
239as a JSON array or an object. A Lua table which only has positive 239as a JSON array or an object. A Lua table with only positive integers
240integers (>0) keys of type +number+ will be encoded as a JSON array. 240keys of type +number+ will be encoded as a JSON array. All other
241All other tables will be encoded as a JSON object. 241tables will be encoded as a JSON object.
242
243Missing entries from sparse Lua arrays are encoded as +null+.
244 242
245.Example: Encoding a sparse array 243Refer to <<encode_sparse_array,+cjson.encode_sparse_array+>> for details
246[source,lua] 244on sparse array handling.
247cjson.encode({ [3] = "data" })
248-- Returns: '[null,null,"data"]'
249 245
250Lua CJSON does not use metamethods when serialising tables. 246Lua 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
256table keys which are type +number+ or +string+. All other types will 252table keys which are type +number+ or +string+. All other types will
257generate an error. 253generate an error.
258 254
259
260[NOTE] 255[NOTE]
261Standards compliant JSON must be encapsulated in either an 256Standards compliant JSON must be encapsulated in either an object
262object (+{}+) 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
265By default, the following will generate errors: 260By 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
271These defaults can be changed with: 266These 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]
279value = { true, { foo = "bar" } } 274value = { true, { foo = "bar" } }
280json_text = cjson.encode(value) 275json_text = cjson.encode(value)
281-- Returns: '[true,{"foo":"bar"}]' 276-- Returns: '[true,{"foo":"bar"}]'
282 277
283 278
279cjson.encode_keep_buffer
280~~~~~~~~~~~~~~~~~~~~~~~~
281
282[source,lua]
283------------
284keep = cjson.encode_keep_buffer([keep])
285-- "keep" must be a boolean
286------------
287
288By default, Lua CJSON will reuse the JSON encoding buffer to improve
289performance. The buffer will grow to the largest size required and is
290not freed until the Lua CJSON module is garbage collected. Setting this
291option to +false+ will cause the buffer to be freed after each call to
292+cjson.encode+.
293
294
295[[encode_max_depth]]
284cjson.encode_max_depth 296cjson.encode_max_depth
285~~~~~~~~~~~~~~~~~~~~~~ 297~~~~~~~~~~~~~~~~~~~~~~
286 298
287[source,lua] 299[source,lua]
288------------ 300------------
289depth = cjson.encode_max_depth([depth]) 301depth = cjson.encode_max_depth([depth])
290-- "depth" must be a positive integer (>0). 302-- "depth" must be a positive integer
291------------ 303------------
292 304
293By default, Lua CJSON will reject data structure with more than 20 nested 305By 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
319heavy JSON conversions by up to 50%. 331heavy JSON conversions by up to 50%.
320 332
321 333
322[[sparse_arrays]] 334[[encode_sparse_array]]
323cjson.encode_sparse_array 335cjson.encode_sparse_array
324~~~~~~~~~~~~~~~~~~~~~~~~~ 336~~~~~~~~~~~~~~~~~~~~~~~~~
325 337
@@ -327,27 +339,25 @@ cjson.encode_sparse_array
327------------ 339------------
328convert, ratio, safe = cjson.encode_sparse_array([convert[, ratio[, safe]]]) 340convert, 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
334Lua CJSON classifies Lua tables into 3 types when encoding as a JSON 346Lua CJSON classifies Lua tables into 3 types when encoding as a JSON
335array. This is determined by the number of values missing for keys up 347array. This is determined by the number of values missing for keys up
336to the maximum index: 348to the maximum index:
337 349
338Normal:: 350[horizontal]
339 All values are available. 351Normal:: All values are available.
340Sparse:: 352Sparse:: At least 1 value is missing.
341 At least 1 value is missing. 353Excessively sparse:: The number of values missing exceed the configured ratio.
342Excessively sparse::
343 The number of missing values exceed a configured number.
344 354
345Lua CJSON encodes sparse Lua arrays by as JSON arrays using JSON +null+. 355Lua 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]
349cjson.encode({ [3] = "sparse" }) 359cjson.encode({ [3] = "data" })
350-- Returns: '[null,null,"sparse"]' 360-- Returns: '[null,null,"data"]'
351 361
352Lua CJSON checks for excessively sparse arrays when the maximum index 362Lua CJSON checks for excessively sparse arrays when the maximum index
353is greater an the +safe+ limit and +ratio+ is greater than +0+. An 363is 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
369cjson.keep_encode_buffer 379[[refuse_invalid_numbers]]
370~~~~~~~~~~~~~~~~~~~~~~~~
371
372[source,lua]
373------------
374keep = cjson.keep_encode_buffer([keep])
375-- "keep" must be a boolean
376------------
377
378By default, Lua CJSON will reuse the JSON encoding buffer to improve
379performance. The buffer will grow to the largest size required and is
380not freed until the Lua CJSON module is garbage collected. Setting this
381option to +false+ will cause the buffer to be freed after each call to
382+cjson.encode+.
383
384
385cjson.refuse_invalid_numbers 380cjson.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
395Lua CJSON considers numbers which are outside the JSON specification to be 390Lua CJSON can throw an error for numbers outside of the JSON
396_invalid_: 391specification (_invalid numbers_):
397 392
398- Infinity 393- Infinity
399- NaN 394- NaN
400- Hexadecimal numbers 395- Hexadecimal
401 396
402By default Lua CJSON will decode _invalid_ numbers, but will refuse to 397By default Lua CJSON will decode _invalid numbers_, but will refuse to
403encode them. 398encode them.
404 399
405This setting can be configured separately for encoding and/or 400This setting can be configured separately for encoding and/or
406decoding: 401decoding:
407 402
408Enabled:: 403[horizontal]
409 an error will be generated if an _invalid_ number is found. 404Enabled:: An error will be generated if an _invalid number_ is found.
410Disabled (encoding):: 405Disabled for encoding:: NaN and Infinity can be encoded.
411 NaN and Infinity can be encoded. 406Disabled for decoding:: All numbers supported by +strtod+(3) will be parsed.
412Disabled (decoding)::
413 All numbers supported by +strtod+(3) will be parsed.
414 407
415 408
416API (Variables) 409API (Variables)
@@ -419,8 +412,8 @@ API (Variables)
419cjson.null 412cjson.null
420~~~~~~~~~~ 413~~~~~~~~~~
421 414
422Lua CJSON decodes JSON +null+ as a Lua lightuserdata NULL pointer. 415Lua 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
426cjson.version 419cjson.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: