diff options
| author | Mark Pulford <mark@kyne.com.au> | 2011-12-14 00:57:57 +1030 |
|---|---|---|
| committer | Mark Pulford <mark@kyne.com.au> | 2011-12-14 00:57:57 +1030 |
| commit | 353784724505b23539aa693bd2aed3932493ed62 (patch) | |
| tree | ca2f2c634fc3e4800dd93ba6f41f3e15830195c5 | |
| parent | 2487f7fa11a9c3c6d24948b4abc9a0e5468d1b1d (diff) | |
| download | lua-cjson-353784724505b23539aa693bd2aed3932493ed62.tar.gz lua-cjson-353784724505b23539aa693bd2aed3932493ed62.tar.bz2 lua-cjson-353784724505b23539aa693bd2aed3932493ed62.zip | |
Remove quirks section from manual
Merge quirks section into:
- Build overview (locales)
- API docs
Add API section for variables.
| -rw-r--r-- | manual.txt | 129 |
1 files changed, 55 insertions, 74 deletions
| @@ -38,9 +38,33 @@ http://www.luajit.org[LuaJIT] to build. | |||
| 38 | 38 | ||
| 39 | There are 3 build methods available: | 39 | There are 3 build methods available: |
| 40 | 40 | ||
| 41 | - Make: POSIX, OSX | 41 | Make:: |
| 42 | - RPM: Various Linux distributions | 42 | POSIX (including Linux, BSD, Mac OSX & Solaris) |
| 43 | - http://www.luarocks.org[LuaRocks]: POSIX, OSX, Windows | 43 | RPM:: |
| 44 | Linux | ||
| 45 | LuaRocks:: | ||
| 46 | POSIX (including Linux, BSD, Mac OSX & Solaris), Windows | ||
| 47 | |||
| 48 | |||
| 49 | Build configuration overview | ||
| 50 | ~~~~~~~~~~~~~~~~~~~~~~~~~~~~ | ||
| 51 | |||
| 52 | Lua CJSON uses +strtod+(3) and +snprintf+(3) to perform numeric | ||
| 53 | conversion as they are usually well supported, fast and bug free. | ||
| 54 | |||
| 55 | To ensure JSON encoding/decoding works correctly for locales using | ||
| 56 | comma decimal separators, Lua CJSON must be compiled with one of the | ||
| 57 | following ++#define++s: | ||
| 58 | |||
| 59 | USE_POSIX_USELOCALE:: | ||
| 60 | Thread safe. Supported by Linux and Mac OSX. Recommended where available. | ||
| 61 | USE_POSIX_SETLOCALE:: | ||
| 62 | Works on all ANSI C platforms. May be used when thread-safety isn't required. | ||
| 63 | |||
| 64 | Also available: | ||
| 65 | |||
| 66 | USE_INTERNAL_ISINF:: | ||
| 67 | Workaround for Solaris platforms missing isinf(). | ||
| 44 | 68 | ||
| 45 | 69 | ||
| 46 | Make | 70 | Make |
| @@ -91,6 +115,7 @@ cd lua-cjson-1.0.4 | |||
| 91 | luarocks make | 115 | luarocks make |
| 92 | ----------- | 116 | ----------- |
| 93 | 117 | ||
| 118 | [NOTE] | ||
| 94 | LuaRocks does not support platform specific configuration for Solaris. | 119 | LuaRocks does not support platform specific configuration for Solaris. |
| 95 | On Solaris, you may need to manually uncomment +USE_INTERNAL_ISINF+ in | 120 | On Solaris, you may need to manually uncomment +USE_INTERNAL_ISINF+ in |
| 96 | the rockspec before building this module. | 121 | the rockspec before building this module. |
| @@ -99,8 +124,8 @@ See the http://luarocks.org/en/Documentation[LuaRocks documentation] for | |||
| 99 | further details. | 124 | further details. |
| 100 | 125 | ||
| 101 | 126 | ||
| 102 | Lua API | 127 | API (Functions) |
| 103 | ------- | 128 | --------------- |
| 104 | 129 | ||
| 105 | Synopsis | 130 | Synopsis |
| 106 | ~~~~~~~~ | 131 | ~~~~~~~~ |
| @@ -156,6 +181,11 @@ data_json = '[ true, { "foo": "bar" } ]' | |||
| 156 | data_obj = cjson.decode(data_json) | 181 | data_obj = cjson.decode(data_json) |
| 157 | ------------ | 182 | ------------ |
| 158 | 183 | ||
| 184 | [CAUTION] | ||
| 185 | Care must be taken when after decoding objects with numeric keys. Each | ||
| 186 | numeric keys will be stored as a Lua +string+. Any code assuming type | ||
| 187 | +number+ may break. | ||
| 188 | |||
| 159 | 189 | ||
| 160 | cjson.encode | 190 | cjson.encode |
| 161 | ~~~~~~~~~~~~ | 191 | ~~~~~~~~~~~~ |
| @@ -177,7 +207,7 @@ JSON representation. | |||
| 177 | - +string+ | 207 | - +string+ |
| 178 | - +table+ | 208 | - +table+ |
| 179 | 209 | ||
| 180 | The remaining Lua types cannot be serialised: | 210 | The remaining Lua types will generate an error: |
| 181 | 211 | ||
| 182 | - +function+ | 212 | - +function+ |
| 183 | - +lightuserdata+ (non-NULL values) | 213 | - +lightuserdata+ (non-NULL values) |
| @@ -217,6 +247,16 @@ example: | |||
| 217 | [null,null,"data"] | 247 | [null,null,"data"] |
| 218 | ------------------- | 248 | ------------------- |
| 219 | 249 | ||
| 250 | Lua CJSON does not use metamethods when serialising tables. | ||
| 251 | |||
| 252 | - +rawget+ is used to iterate over Lua arrays. | ||
| 253 | - +next+ is used to iterate over Lua objects. | ||
| 254 | |||
| 255 | JSON object keys are always strings. +cjson.encode+ can only handle | ||
| 256 | table keys which are type +number+ or +string+. All other types will | ||
| 257 | generate an error. | ||
| 258 | |||
| 259 | |||
| 220 | [NOTE] | 260 | [NOTE] |
| 221 | Standards compliant JSON must be encapsulated in either an | 261 | Standards compliant JSON must be encapsulated in either an |
| 222 | object (+{}+) or an array (+[]+). Hence a table must be passed to | 262 | object (+{}+) or an array (+[]+). Hence a table must be passed to |
| @@ -376,80 +416,21 @@ Disabled (decoding):: | |||
| 376 | All numbers supported by +strtod+(3) will be parsed. | 416 | All numbers supported by +strtod+(3) will be parsed. |
| 377 | 417 | ||
| 378 | 418 | ||
| 379 | cjson.version | 419 | API (Variables) |
| 380 | ~~~~~~~~~~~~~ | 420 | --------------- |
| 381 | |||
| 382 | [source,lua] | ||
| 383 | ------------ | ||
| 384 | ver = cjson.version | ||
| 385 | -- variable containing the package version (1.0.4) | ||
| 386 | ------------ | ||
| 387 | |||
| 388 | FIXME | ||
| 389 | |||
| 390 | |||
| 391 | JSON and handling under Lua CJSON | ||
| 392 | --------------------------------- | ||
| 393 | 421 | ||
| 394 | Nulls | 422 | cjson.null |
| 395 | ~~~~~ | ||
| 396 | |||
| 397 | Lua CJSON decodes JSON +null+ as a Lua lightuserdata NULL pointer. | ||
| 398 | |||
| 399 | As a convenience, +cjson.null+ is provided for comparison. | ||
| 400 | |||
| 401 | |||
| 402 | Table keys | ||
| 403 | ~~~~~~~~~~ | 423 | ~~~~~~~~~~ |
| 404 | 424 | ||
| 405 | If all Lua table keys are type +number+ (not +string+), Lua CJSON will | 425 | Lua CJSON decodes JSON +null+ as a Lua lightuserdata NULL pointer. |
| 406 | encode the table as a JSON array. See | 426 | +cjson.null+ can be used for comparison. |
| 407 | <<sparse_arrays,cjson.encode_sparse_array>> above for more details. | ||
| 408 | |||
| 409 | All other tables will be encoded as a JSON object. | ||
| 410 | |||
| 411 | JSON requires that object keys must be type string. +cjson.encode+ | ||
| 412 | will convert numeric keys to a string, and other non-string types will | ||
| 413 | generate an error. | ||
| 414 | |||
| 415 | [CAUTION] | ||
| 416 | ========= | ||
| 417 | Care must be taken when encoding/decoding objects which contain keys of type | ||
| 418 | +number+. | ||
| 419 | [source,lua] | ||
| 420 | ------------ | ||
| 421 | val = cjson.decode(cjson.encode{[1] = "1", a = "a"}) | ||
| 422 | -- Returns: { ["1"] = "1", ... | ||
| 423 | -- NOT: { [1] = "1", ... | ||
| 424 | ------------ | ||
| 425 | ========= | ||
| 426 | |||
| 427 | Metamethods | ||
| 428 | ~~~~~~~~~~~ | ||
| 429 | |||
| 430 | Lua CJSON does not use metamethods when serialising tables. | ||
| 431 | |||
| 432 | - +rawget+ is used to iterate over Lua arrays. | ||
| 433 | - +next+ is used to iterate over Lua objects. | ||
| 434 | |||
| 435 | |||
| 436 | Functions, Userdata, Threads | ||
| 437 | ~~~~~~~~~~~~~~~~~~~~~~~~~~~~ | ||
| 438 | |||
| 439 | Lua CJSON will generate an error if asked to serialise Lua functions, | ||
| 440 | userdata, lightuserdata or threads. | ||
| 441 | |||
| 442 | 427 | ||
| 443 | Locales | ||
| 444 | ~~~~~~~ | ||
| 445 | 428 | ||
| 446 | Lua CJSON uses +strtod+(3) and +snprintf+(3) to perform numeric conversion | 429 | cjson.version |
| 447 | as they are usually well supported, fast and bug free. | 430 | ~~~~~~~~~~~~~ |
| 448 | 431 | ||
| 449 | To ensure JSON encoding/decoding works correctly for locales using | 432 | The version number of the Lua CJSON module in use can be found in |
| 450 | comma decimal separators, Lua CJSON must be compiled with either | 433 | +cjson.version+. |
| 451 | +USE_POSIX_USELOCALE+ or +USE_POSIX_SETLOCALE+. See the +Makefile+ or the | ||
| 452 | rockspec for details. | ||
| 453 | 434 | ||
| 454 | 435 | ||
| 455 | [sect1] | 436 | [sect1] |
