diff options
-rw-r--r-- | manual.txt | 423 |
1 files changed, 234 insertions, 189 deletions
@@ -1,43 +1,40 @@ | |||
1 | = Lua CJSON 2.0devel Manual = | 1 | = Lua CJSON 2.0devel Manual = |
2 | Mark Pulford <mark@kyne.com.au> | 2 | Mark Pulford <mark@kyne.com.au> |
3 | :revdate: November 30, 2011 | 3 | :revdate: Janurary ?, 2012 |
4 | 4 | ||
5 | Overview | 5 | Overview |
6 | -------- | 6 | -------- |
7 | 7 | ||
8 | The Lua CJSON library provides JSON support for Lua. | 8 | The Lua CJSON module provides JSON support for Lua. |
9 | 9 | ||
10 | .Features | 10 | *Features*:: |
11 | - Fast, standards compliant encoding/parsing routines | 11 | - Fast, standards compliant encoding/parsing routines |
12 | - Full support for JSON with UTF-8, including decoding surrogate | 12 | - Full support for JSON with UTF-8, including decoding surrogate pairs |
13 | pairs | ||
14 | - Optional run-time support for common exceptions to the JSON | 13 | - Optional run-time support for common exceptions to the JSON |
15 | specification (NaN, Infinity,..) | 14 | specification (infinity, NaN,..) |
16 | - No external dependencies | 15 | - No dependencies on other libraries |
17 | 16 | ||
18 | .Caveats | 17 | *Caveats*:: |
19 | - UTF-16 and UTF-32 are not supported. | 18 | - UTF-16 and UTF-32 are not supported |
20 | 19 | ||
21 | Lua CJSON is covered by the MIT license. Review the file +LICENSE+ for | 20 | Lua CJSON is covered by the MIT license. Review the file +LICENSE+ for |
22 | details. | 21 | details. |
23 | 22 | ||
24 | The latest version of this software is available from the | 23 | The latest version of this software is available from the |
25 | http://www.kyne.com.au/~mark/software/lua-cjson.php[Lua CJSON website]. | 24 | http://www.kyne.com.au/~mark/software/lua-cjson.php[Lua CJSON website]. |
26 | 25 | ||
27 | Feel free to email me if you have any patches, suggestions, or | 26 | Feel free to email me if you have any patches, suggestions, or comments. |
28 | comments. | ||
29 | 27 | ||
30 | 28 | ||
31 | Installation Methods | 29 | Installation |
32 | -------------------- | 30 | ------------ |
33 | 31 | ||
34 | Lua CJSON requires either http://www.lua.org[Lua] 5.1, Lua 5.2, or | 32 | Lua CJSON requires either http://www.lua.org[Lua] 5.1, Lua 5.2, or |
35 | http://www.luajit.org[LuaJIT] to build. | 33 | http://www.luajit.org[LuaJIT] to build. |
36 | 34 | ||
37 | There are 4 build methods available: | 35 | The build method can be selected from 4 options: |
38 | 36 | ||
39 | [horizontal] | 37 | Make:: Unix (including Linux, BSD, Mac OSX & Solaris), Windows |
40 | Make:: Unix (including Linux, BSD, Mac OSX & Solaris) | ||
41 | CMake:: Unix, Windows | 38 | CMake:: Unix, Windows |
42 | RPM:: Linux | 39 | RPM:: Linux |
43 | LuaRocks:: Unix, Windows | 40 | LuaRocks:: Unix, Windows |
@@ -46,46 +43,67 @@ LuaRocks:: Unix, Windows | |||
46 | Make | 43 | Make |
47 | ~~~~ | 44 | ~~~~ |
48 | 45 | ||
49 | Review and update the included Makefile to suit your platform. Next, | 46 | The included +Makefile+ has generic settings. |
50 | build and install the module: | 47 | |
48 | First, review and update the included makefile to suit your platform (if | ||
49 | required). | ||
50 | |||
51 | Next, build and install the module: | ||
51 | 52 | ||
52 | [source,sh] | 53 | [source,sh] |
53 | make install | 54 | make install |
54 | 55 | ||
55 | Or install manually: | 56 | Or install manually into your Lua module directory: |
56 | 57 | ||
57 | [source,sh] | 58 | [source,sh] |
58 | make | 59 | make |
59 | cp cjson.so $your_lua_module_directory | 60 | cp cjson.so $LUA_MODULE_DIRECTORY |
60 | 61 | ||
61 | 62 | ||
62 | CMake | 63 | CMake |
63 | ~~~~~ | 64 | ~~~~~ |
64 | 65 | ||
65 | http://www.cmake.org[CMake] can generate build configuration for many different | 66 | http://www.cmake.org[CMake] can generate build configuration for many |
66 | platforms (including Unix and Windows). | 67 | different platforms (including Unix and Windows). |
68 | |||
69 | First, generate the makefile for your platform using CMake. If CMake is | ||
70 | unable to find Lua, manually set the +LUA_DIR+ environment variable to | ||
71 | the base prefix of your Lua 5.1 installation. | ||
72 | |||
73 | While +cmake+ is used in the example below, +ccmake+ or +cmake-gui+ may | ||
74 | be used to present an interface for changing the default build options. | ||
67 | 75 | ||
68 | [source,sh] | 76 | [source,sh] |
69 | mkdir build | 77 | mkdir build |
70 | cd build | 78 | cd build |
79 | # Optional: export LUA_DIR=$LUA51_PREFIX | ||
71 | cmake .. | 80 | cmake .. |
81 | |||
82 | Next, build and install the module: | ||
83 | |||
84 | [source,sh] | ||
85 | make install | ||
86 | # Or: | ||
72 | make | 87 | make |
73 | cp cjson.so $your_lua_module_directory | 88 | cp cjson.so $LUA_MODULE_DIRECTORY |
74 | 89 | ||
75 | Review the http://www.cmake.org/cmake/help/documentation.html[CMake documentation] | 90 | Review the |
91 | http://www.cmake.org/cmake/help/documentation.html[CMake documentation] | ||
76 | for further details. | 92 | for further details. |
77 | 93 | ||
78 | 94 | ||
79 | RPM | 95 | RPM |
80 | ~~~ | 96 | ~~~ |
81 | 97 | ||
82 | Linux distributions using RPM should be able to create a package via | 98 | Linux distributions using http://rpm.org[RPM] can create a package via |
83 | the included RPM spec file. Install the +rpm-build+ package (or | 99 | the included RPM spec file. Ensure the +rpm-build+ package (or similar) |
84 | similar) then: | 100 | has been installed. |
101 | |||
102 | Build and install the module via RPM: | ||
85 | 103 | ||
86 | [source,sh] | 104 | [source,sh] |
87 | rpmbuild -tb lua-cjson-2.0devel.tar.gz | 105 | rpmbuild -tb lua-cjson-2.0devel.tar.gz |
88 | rpm -Uvh $newly_built_lua_cjson_rpm | 106 | rpm -Uvh $LUA_CJSON_RPM |
89 | 107 | ||
90 | 108 | ||
91 | LuaRocks | 109 | LuaRocks |
@@ -94,7 +112,9 @@ LuaRocks | |||
94 | http://luarocks.org[LuaRocks] can be used to install and manage Lua | 112 | http://luarocks.org[LuaRocks] can be used to install and manage Lua |
95 | modules on a wide range of platforms (including Windows). | 113 | modules on a wide range of platforms (including Windows). |
96 | 114 | ||
97 | Extract the Lua CJSON source package into a directory and run: | 115 | First, extract the Lua CJSON source package. |
116 | |||
117 | Next, install the module: | ||
98 | 118 | ||
99 | [source,sh] | 119 | [source,sh] |
100 | cd lua-cjson-2.0devel | 120 | cd lua-cjson-2.0devel |
@@ -109,35 +129,39 @@ Review the http://luarocks.org/en/Documentation[LuaRocks documentation] | |||
109 | for further details. | 129 | for further details. |
110 | 130 | ||
111 | 131 | ||
132 | [[build_options]] | ||
112 | Build Options (#define) | 133 | Build Options (#define) |
113 | ~~~~~~~~~~~~~~~~~~~~~~~ | 134 | ~~~~~~~~~~~~~~~~~~~~~~~ |
114 | 135 | ||
115 | [horizontal] | 136 | Lua CJSON offers several +#define+ build options to address portability |
116 | ENABLE_CJSON_GLOBAL:: Register +cjson+ module table as a global | 137 | issues, and enable non-default features. Some build methods may |
117 | variable (not recommended). | 138 | automatically set platform specific options if required. Other features |
118 | USE_INTERNAL_ISINF:: Workaround for Solaris platforms missing ++isinf++(3). | 139 | should be enabled manually. |
119 | DISABLE_INVALID_NUMBERS:: Recommended on platforms where ++strtod++(3) / | 140 | |
120 | ++sprintf++(3) are not POSIX compliant (Eg, Windows MinGW). Prevents | 141 | ENABLE_CJSON_GLOBAL:: Register +cjson+ module table as a global variable |
121 | +cjson.encode_invalid_numbers+ and +cjson.decode_invalid_numbers+ | 142 | (not recommended). |
122 | from being enabled. However, +cjson.encode_invalid_numbers+ may be | 143 | USE_INTERNAL_ISINF:: Workaround for Solaris platforms missing +isinf+. |
123 | set to +"null"+. This option is unnecessary and is ignored when | 144 | DISABLE_INVALID_NUMBERS:: Recommended on platforms where +strtod+ / |
124 | using built-in floating point conversion. | 145 | +sprintf+ are not POSIX compliant (eg, Windows MinGW). Prevents |
146 | +cjson.encode_invalid_numbers+ and +cjson.decode_invalid_numbers+ from | ||
147 | being enabled. However, +cjson.encode_invalid_numbers+ may still be | ||
148 | set to +"null"+. When using the Lua CJSON built-in floating point | ||
149 | conversion this option is unnecessary and is ignored. | ||
125 | 150 | ||
126 | 151 | ||
127 | Built-in dtoa() support | 152 | Built-in floating point conversion |
128 | ^^^^^^^^^^^^^^^^^^^^^^^ | 153 | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ |
129 | 154 | ||
130 | Lua CJSON may be built with David Gay's | 155 | Lua CJSON may be built with David Gay's |
131 | http://www.netlib.org/fp/[floating point conversion routines]. This | 156 | http://www.netlib.org/fp/[floating point conversion routines]. This can |
132 | can increase overall performance by 50% or more on some platforms when | 157 | increase overall performance by up to 50% on some platforms when |
133 | converting data number heavy data. However, this option reduces | 158 | converting a large amount of numeric data. However, this option reduces |
134 | portability and is disabled by default. | 159 | portability and is disabled by default. |
135 | 160 | ||
136 | [horizontal] | ||
137 | USE_INTERNAL_FPCONV:: Enable internal number conversion routines. | 161 | USE_INTERNAL_FPCONV:: Enable internal number conversion routines. |
138 | IEEE_BIG_ENDIAN:: Must be set on big endian architectures. | 162 | IEEE_BIG_ENDIAN:: Must be set on big endian architectures. |
139 | MULTIPLE_THREADS:: Must be set when Lua CJSON may be used in a | 163 | MULTIPLE_THREADS:: Must be set if Lua CJSON may be used in a |
140 | multi-threaded application. Requries _pthreads_. | 164 | multi-threaded application. Requires the _pthreads_ library. |
141 | 165 | ||
142 | 166 | ||
143 | API (Functions) | 167 | API (Functions) |
@@ -148,21 +172,21 @@ Synopsis | |||
148 | 172 | ||
149 | [source,lua] | 173 | [source,lua] |
150 | ------------ | 174 | ------------ |
151 | -- Module initalisation methods | 175 | -- Module instantiation |
152 | local cjson = require "cjson" | 176 | local cjson = require "cjson" |
153 | local cjson2 = cjson.new() | 177 | local cjson2 = cjson.new() |
154 | 178 | ||
155 | -- Translate Lua value to/from JSON | 179 | -- Translate Lua value to/from JSON |
156 | text = cjson.encode(value) | 180 | text = cjson.encode(value) |
157 | value = cjson.decode(text) | 181 | value = cjson.decode(text) |
158 | 182 | ||
159 | -- Get and/or set Lua CJSON configuration | 183 | -- Get and/or set Lua CJSON configuration |
160 | setting = cjson.decode_invalid_numbers([setting]) | 184 | setting = cjson.decode_invalid_numbers([setting]) |
161 | setting = cjson.encode_invalid_numbers([setting]) | 185 | setting = cjson.encode_invalid_numbers([setting]) |
186 | keep = cjson.encode_keep_buffer([keep]) | ||
162 | depth = cjson.encode_max_depth([depth]) | 187 | depth = cjson.encode_max_depth([depth]) |
163 | depth = cjson.decode_max_depth([depth]) | 188 | depth = cjson.decode_max_depth([depth]) |
164 | convert, ratio, safe = cjson.encode_sparse_array([convert[, ratio[, safe]]]) | 189 | convert, ratio, safe = cjson.encode_sparse_array([convert[, ratio[, safe]]]) |
165 | keep = cjson.encode_keep_buffer([keep]) | ||
166 | ------------ | 190 | ------------ |
167 | 191 | ||
168 | 192 | ||
@@ -175,35 +199,35 @@ local cjson = require "cjson" | |||
175 | local cjson2 = cjson.new() | 199 | local cjson2 = cjson.new() |
176 | ------------ | 200 | ------------ |
177 | 201 | ||
178 | Lua CJSON can be loaded via the Lua +require+ function. A global | 202 | Import Lua CJSON via the Lua +require+ function. Lua CJSON does not |
179 | +cjson+ module table is registered under Lua 5.1 to maintain backward | 203 | register a global module table with the default |
180 | compatibility. Lua CJSON does not register a global table under Lua | 204 | <<build_options,build options>>. |
181 | 5.2 since this practice is discouraged. | ||
182 | 205 | ||
183 | +cjson.new+ can be used to instantiate an independent copy of the Lua | 206 | +cjson.new+ can be used to instantiate an independent copy of the Lua |
184 | CJSON module. The new module has a separate persistent encoding | 207 | CJSON module. The new module has a separate persistent encoding buffer, |
185 | buffer, and default settings. | 208 | and default settings. |
186 | 209 | ||
187 | Lua CJSON can support Lua implementations using multiple pre-emptive | 210 | Lua CJSON can support Lua implementations using multiple preemptive |
188 | threads within a single Lua state provided the persistent encoding | 211 | threads within a single Lua state provided the persistent encoding |
189 | buffer is not shared. This can be achieved by one of the following | 212 | buffer is not shared. This can be achieved by one of the following |
190 | methods: | 213 | methods: |
191 | 214 | ||
192 | - Disabling the persistent encoding buffer with | 215 | - Disabling the persistent encoding buffer with |
193 | +cjson.encode_keep_buffer+ | 216 | <<encode_keep_buffer,+cjson.encode_keep_buffer+>> |
194 | - Ensuring each thread calls +cjson.encode+ at a time | 217 | - Ensuring each thread calls <<encode,+cjson.encode+>> separately (ie, |
195 | - Using a separate +cjson+ module table per pre-emptive thread | 218 | treat +cjson.encode+ as non-reentrant). |
219 | - Using a separate +cjson+ module table per preemptive thread | ||
196 | (+cjson.new+) | 220 | (+cjson.new+) |
197 | 221 | ||
198 | [NOTE] | 222 | [NOTE] |
199 | Lua CJSON uses ++strtod++(3) and ++snprintf++(3) to perform numeric | 223 | Lua CJSON uses +strtod+ and +snprintf+ to perform numeric conversion as |
200 | conversion as they are usually well supported, fast and bug free. | 224 | they are usually well supported, fast and bug free. However, these |
201 | However, these functions require a workaround for JSON | 225 | functions require a workaround for JSON encoding/parsing under locales |
202 | encoding/parsing under locales using a comma decimal separator. Lua | 226 | using a comma decimal separator. Lua CJSON detects the current locale |
203 | CJSON detects the current locale during instantiation to determine | 227 | during instantiation to determine and automatically implement the |
204 | whether a workaround is required. Lua CJSON should be reinitialised | 228 | workaround if required. Lua CJSON should be reinitialised via |
205 | via +cjson.new+ if the locale of the current process changes. | 229 | +cjson.new+ if the locale of the current process changes. Using a |
206 | Different locales per thread are not supported. | 230 | different locale per thread is not supported. |
207 | 231 | ||
208 | 232 | ||
209 | decode | 233 | decode |
@@ -215,22 +239,22 @@ value = cjson.decode(json_text) | |||
215 | ------------ | 239 | ------------ |
216 | 240 | ||
217 | +cjson.decode+ will deserialise any UTF-8 JSON string into a Lua value | 241 | +cjson.decode+ will deserialise any UTF-8 JSON string into a Lua value |
218 | or table. It may return any of the types that +cjson.encode+ supports. | 242 | or table. |
219 | 243 | ||
220 | UTF-16 and UTF-32 JSON strings are not supported. | 244 | UTF-16 and UTF-32 JSON strings are not supported. |
221 | 245 | ||
222 | +cjson.decode+ requires that any NULL (ASCII 0) and double quote | 246 | +cjson.decode+ requires that any NULL (ASCII 0) and double quote (ASCII |
223 | (ASCII 34) characters are escaped within strings. All escape codes | 247 | 34) characters are escaped within strings. All escape codes will be |
224 | will be decoded and other characters will be passed transparently. | 248 | decoded and other bytes will be passed transparently. UTF-8 characters |
225 | UTF-8 characters are not validated during decoding and should be | 249 | are not validated during decoding and should be checked elsewhere if |
226 | checked elsewhere if required. | 250 | required. |
227 | 251 | ||
228 | JSON +null+ will be converted to a NULL +lightuserdata+ value. This | 252 | JSON +null+ will be converted to a NULL +lightuserdata+ value. This can |
229 | can be compared with +cjson.null+ for convenience. | 253 | be compared with +cjson.null+ for convenience. |
230 | 254 | ||
231 | By default, numbers incompatible with the JSON specification (NaN, | 255 | By default, numbers incompatible with the JSON specification (infinity, |
232 | Infinity, Hexadecimal) can be decoded. This default can be changed | 256 | NaN, hexadecimal) can be decoded. This default can be changed with |
233 | with +cjson.decode_invalid_numbers+. | 257 | <<decode_invalid_numbers,+cjson.decode_invalid_numbers+>>. |
234 | 258 | ||
235 | .Example: Decoding | 259 | .Example: Decoding |
236 | [source,lua] | 260 | [source,lua] |
@@ -239,9 +263,9 @@ value = cjson.decode(json_text) | |||
239 | -- Returns: { true, { foo = "bar" } } | 263 | -- Returns: { true, { foo = "bar" } } |
240 | 264 | ||
241 | [CAUTION] | 265 | [CAUTION] |
242 | Care must be taken when after decoding JSON objects with numeric keys. Each | 266 | Care must be taken after decoding JSON objects with numeric keys. Each |
243 | numeric key will be stored as a Lua +string+. Any code assuming type | 267 | numeric key will be stored as a Lua +string+. Any subsequent code |
244 | +number+ may break. | 268 | assuming type +number+ may break. |
245 | 269 | ||
246 | 270 | ||
247 | [[decode_invalid_numbers]] | 271 | [[decode_invalid_numbers]] |
@@ -251,21 +275,24 @@ decode_invalid_numbers | |||
251 | [source,lua] | 275 | [source,lua] |
252 | ------------ | 276 | ------------ |
253 | setting = cjson.decode_invalid_numbers([setting]) | 277 | setting = cjson.decode_invalid_numbers([setting]) |
254 | -- "setting" must be on of: | 278 | -- "setting" must be a boolean. Default: true. |
255 | -- "off", "on", false, true | ||
256 | ------------ | 279 | ------------ |
257 | 280 | ||
258 | Lua CJSON can throw an error when trying to parse numbers outside of | 281 | Lua CJSON may throw an error when trying to decode numbers not supported |
259 | the JSON specification (_invalid numbers_): | 282 | by the JSON specification. _Invalid numbers_ are defined as: |
260 | 283 | ||
261 | - Infinity | 284 | - infinity |
262 | - Not-a-number (NaN) | 285 | - not-a-number (NaN) |
263 | - Hexadecimal | 286 | - hexadecimal |
264 | 287 | ||
265 | By default Lua CJSON will decode _invalid numbers_. | 288 | Available settings: |
266 | 289 | ||
267 | This setting is only changed when an argument is provided. The current | 290 | +true+:: Accept and decode _invalid numbers_. This is the default |
268 | setting is always returned. | 291 | setting. |
292 | +false+:: Throw an error when _invalid numbers_ are encountered. | ||
293 | |||
294 | The current setting is always returned, and is only updated when an | ||
295 | argument is provided. | ||
269 | 296 | ||
270 | 297 | ||
271 | [[decode_max_depth]] | 298 | [[decode_max_depth]] |
@@ -275,24 +302,25 @@ decode_max_depth | |||
275 | [source,lua] | 302 | [source,lua] |
276 | ------------ | 303 | ------------ |
277 | depth = cjson.decode_max_depth([depth]) | 304 | depth = cjson.decode_max_depth([depth]) |
278 | -- "depth" must be a positive integer | 305 | -- "depth" must be a positive integer. Default: 1000. |
279 | ------------ | 306 | ------------ |
280 | 307 | ||
281 | By default, Lua CJSON will reject JSON with arrays and/or objects | 308 | Lua CJSON will throw an error when parsing deeply nested JSON once the |
282 | nested more than 1000 deep. | 309 | maximum array/object depth has been exceeded. This check prevents |
310 | unnecessarily complicated JSON from slowing down the application, or | ||
311 | crashing the application due to lack of process stack space. | ||
283 | 312 | ||
284 | This setting is only changed when an argument is provided. The current | 313 | An error may be thrown before the depth limit is hit if Lua is unable to |
285 | setting is always returned. | 314 | allocate more objects on the Lua stack. |
286 | 315 | ||
287 | When the maximum array/object depth is exceeded Lua CJSON will throw | 316 | By default, Lua CJSON will reject JSON with arrays and/or objects nested |
288 | an error. An error may be thrown before the depth limit is hit if Lua | 317 | more than 1000 levels deep. |
289 | is unable to allocate more objects on the Lua stack. | ||
290 | 318 | ||
291 | This check prevents unnecessarily complicated JSON from slowing down | 319 | The current setting is always returned, and is only updated when an |
292 | the application, or crashing the application due to lack of process | 320 | argument is provided. |
293 | stack space. | ||
294 | 321 | ||
295 | 322 | ||
323 | [[encode]] | ||
296 | encode | 324 | encode |
297 | ~~~~~~ | 325 | ~~~~~~ |
298 | 326 | ||
@@ -320,11 +348,10 @@ The remaining Lua types will generate an error: | |||
320 | - +thread+ | 348 | - +thread+ |
321 | - +userdata+ | 349 | - +userdata+ |
322 | 350 | ||
323 | By default, numbers are encoded using the standard Lua number | 351 | By default, numbers are encoded with 14 significant digits. Refer to |
324 | +printf+(3) format (+%.14g+). | 352 | <<encode_number_precision,+cjson.encode_number_precision+>> for details. |
325 | 353 | ||
326 | Lua CJSON will escape the following characters within each UTF-8 | 354 | Lua CJSON will escape the following characters within each UTF-8 string: |
327 | string: | ||
328 | 355 | ||
329 | - Control characters (ASCII 0 - 31) | 356 | - Control characters (ASCII 0 - 31) |
330 | - Double quote (ASCII 34) | 357 | - Double quote (ASCII 34) |
@@ -332,55 +359,56 @@ string: | |||
332 | - Blackslash (ASCII 92) | 359 | - Blackslash (ASCII 92) |
333 | - Delete (ASCII 127) | 360 | - Delete (ASCII 127) |
334 | 361 | ||
335 | All other characters are passed transparently. | 362 | All other bytes are passed transparently. |
336 | 363 | ||
337 | [CAUTION] | 364 | [CAUTION] |
338 | ========= | 365 | ========= |
339 | Lua CJSON will successfully encode/decode binary strings, but this is | 366 | Lua CJSON will successfully encode/decode binary strings, but this is |
340 | technically not supported by JSON and may not be compatible with other | 367 | technically not supported by JSON and may not be compatible with other |
341 | JSON libraries. Applications should ensure all Lua strings passed to | 368 | JSON libraries. To ensure the output is valid JSON, applications should |
342 | +cjson.encode+ are valid UTF-8 to ensure the output is valid JSON. | 369 | ensure all Lua strings passed to +cjson.encode+ are UTF-8. |
343 | 370 | ||
344 | Base64 is a common way to transport binary data through JSON. Lua | 371 | Base64 is commonly used to encode binary data as the most efficient |
345 | Base64 routines can be found in the | 372 | encoding under UTF-8 can only reduce the encoded size by a further \~8%. |
373 | Lua Base64 routines can be found in the | ||
346 | http://w3.impa.br/~diego/software/luasocket/[LuaSocket] and | 374 | http://w3.impa.br/~diego/software/luasocket/[LuaSocket] and |
347 | http://www.tecgraf.puc-rio.br/~lhf/ftp/lua/#lbase64[lbase64] packages. | 375 | http://www.tecgraf.puc-rio.br/~lhf/ftp/lua/#lbase64[lbase64] packages. |
348 | ========= | 376 | ========= |
349 | 377 | ||
350 | Lua CJSON uses a heuristic to determine whether to encode a Lua table | 378 | Lua CJSON uses a heuristic to determine whether to encode a Lua table as |
351 | as a JSON array or an object. A Lua table with only positive integers | 379 | a JSON array or an object. A Lua table with only positive integer keys |
352 | keys of type +number+ will be encoded as a JSON array. All other | 380 | of type +number+ will be encoded as a JSON array. All other tables will |
353 | tables will be encoded as a JSON object. | 381 | be encoded as a JSON object. |
354 | |||
355 | Refer to <<encode_sparse_array,+cjson.encode_sparse_array+>> for details | ||
356 | on sparse array handling. | ||
357 | 382 | ||
358 | Lua CJSON does not use metamethods when serialising tables. | 383 | Lua CJSON does not use metamethods when serialising tables. |
359 | 384 | ||
360 | - +rawget+ is used to iterate over Lua arrays | 385 | - +rawget+ is used to iterate over Lua arrays |
361 | - +next+ is used to iterate over Lua objects | 386 | - +next+ is used to iterate over Lua objects |
362 | 387 | ||
363 | JSON object keys are always strings. +cjson.encode+ can only handle | 388 | Lua arrays with missing entries (_sparse arrays_) may optionally be |
389 | encoded in several different ways. Refer to | ||
390 | <<encode_sparse_array,+cjson.encode_sparse_array+>> for details. | ||
391 | |||
392 | JSON object keys are always strings. Hence +cjson.encode+ only supports | ||
364 | table keys which are type +number+ or +string+. All other types will | 393 | table keys which are type +number+ or +string+. All other types will |
365 | generate an error. | 394 | generate an error. |
366 | 395 | ||
367 | [NOTE] | 396 | [NOTE] |
368 | Standards compliant JSON must be encapsulated in either an object | 397 | Standards compliant JSON must be encapsulated in either an object (+{}+) |
369 | (+{}+) or an array (+[]+). Hence a table must be passed to | 398 | or an array (+[]+). If strictly standards compliant JSON is desired, a |
370 | +cjson.encode+ to generate standards compliant JSON output. | 399 | table must be passed to +cjson.encode+. |
371 | This may not be required by some applications. | ||
372 | 400 | ||
373 | By default, the following Lua values will generate errors: | 401 | By default, encoding the following Lua values will generate errors: |
374 | 402 | ||
375 | - Numbers incompatible with the JSON specification (NaN, Infinity, Hexadecimal) | 403 | - Numbers incompatible with the JSON specification (infinity, NaN) |
376 | - Tables nested more than 1000 levels deep | 404 | - Tables nested more than 1000 levels deep |
377 | - Excessively sparse Lua arrays | 405 | - Excessively sparse Lua arrays |
378 | 406 | ||
379 | These defaults can be changed with: | 407 | These defaults can be changed with: |
380 | 408 | ||
409 | - <<encode_invalid_numbers,+cjson.encode_invalid_numbers+>> | ||
381 | - <<encode_max_depth,+cjson.encode_max_depth+>> | 410 | - <<encode_max_depth,+cjson.encode_max_depth+>> |
382 | - <<encode_sparse_array,+cjson.encode_sparse_array+>> | 411 | - <<encode_sparse_array,+cjson.encode_sparse_array+>> |
383 | - <<encode_invalid_numbers,+cjson.encode_invalid_numbers+>> | ||
384 | 412 | ||
385 | .Example: Encoding | 413 | .Example: Encoding |
386 | [source,lua] | 414 | [source,lua] |
@@ -395,41 +423,49 @@ encode_invalid_numbers | |||
395 | [source,lua] | 423 | [source,lua] |
396 | ------------ | 424 | ------------ |
397 | setting = cjson.encode_invalid_numbers([setting]) | 425 | setting = cjson.encode_invalid_numbers([setting]) |
398 | -- "setting" must be on of: | 426 | -- "setting" must a boolean or "null". Default: false. |
399 | -- "off", "on", "null", false, true | ||
400 | ------------ | 427 | ------------ |
401 | 428 | ||
402 | By default, Lua CJSON will throw an error when trying to encode | 429 | Lua CJSON may throw an error when encoding floating point numbers not |
403 | numbers outside of the JSON specification (_invalid numbers_): | 430 | supported by the JSON specification (_invalid numbers_): |
404 | 431 | ||
405 | - Infinity | 432 | - infinity |
406 | - Not-a-number (NaN) | 433 | - not-a-number (NaN) |
407 | 434 | ||
408 | When set to +"null"+, Lua CJSON will encode _invalid numbers_ as a | 435 | Available settings: |
409 | JSON +null+ value. This allows Infinity and NaN to be represented as | ||
410 | valid JSON. | ||
411 | 436 | ||
412 | This setting is only changed when an argument is provided. The current | 437 | +true+:: Allow _invalid numbers_ to be encoded. This will generate |
413 | setting is always returned. | 438 | non-standard JSON, but this output is supported by some libraries. |
439 | +"null"+:: Encode _invalid numbers_ as a JSON +null+ value. This allows | ||
440 | infinity and NaN to be encoded into valid JSON. | ||
441 | +false+:: Throw an error when attempting to encode _invalid numbers_. | ||
442 | This is the default setting. | ||
414 | 443 | ||
444 | The current setting is always returned, and is only updated when an | ||
445 | argument is provided. | ||
415 | 446 | ||
447 | |||
448 | [[encode_keep_buffer]] | ||
416 | encode_keep_buffer | 449 | encode_keep_buffer |
417 | ~~~~~~~~~~~~~~~~~~ | 450 | ~~~~~~~~~~~~~~~~~~ |
418 | 451 | ||
419 | [source,lua] | 452 | [source,lua] |
420 | ------------ | 453 | ------------ |
421 | keep = cjson.encode_keep_buffer([keep]) | 454 | keep = cjson.encode_keep_buffer([keep]) |
422 | -- "keep" must be a boolean | 455 | -- "keep" must be a boolean. Default: true. |
423 | ------------ | 456 | ------------ |
424 | 457 | ||
425 | By default, Lua CJSON will reuse the JSON encoding buffer to improve | 458 | Lua CJSON can reuse the JSON encoding buffer to improve performance. |
426 | performance. The buffer will grow to the largest size required and is | 459 | |
427 | not freed until the Lua CJSON module is garbage collected. Setting this | 460 | Available settings: |
428 | option to +false+ will cause the buffer to be freed after each call to | ||
429 | +cjson.encode+. | ||
430 | 461 | ||
431 | This setting is only changed when an argument is provided. The current | 462 | +true+:: The buffer will grow to the largest size required and is not |
432 | setting is always returned. | 463 | freed until the Lua CJSON module is garbage collected. This is the |
464 | default setting. | ||
465 | +false+:: Free the encode buffer after each call to +cjson.encode+. | ||
466 | |||
467 | The current setting is always returned, and is only updated when an | ||
468 | argument is provided. | ||
433 | 469 | ||
434 | 470 | ||
435 | [[encode_max_depth]] | 471 | [[encode_max_depth]] |
@@ -439,42 +475,45 @@ encode_max_depth | |||
439 | [source,lua] | 475 | [source,lua] |
440 | ------------ | 476 | ------------ |
441 | depth = cjson.encode_max_depth([depth]) | 477 | depth = cjson.encode_max_depth([depth]) |
442 | -- "depth" must be a positive integer | 478 | -- "depth" must be a positive integer. Default: 1000. |
443 | ------------ | 479 | ------------ |
444 | 480 | ||
445 | By default, Lua CJSON will reject data structures with more than 1000 | 481 | Once the maximum table depth has been exceeded Lua CJSON will throw an |
446 | nested tables. | 482 | error. This prevents a deeply nested or recursive data structure from |
483 | crashing the application. | ||
447 | 484 | ||
448 | This setting is only changed when an argument is provided. The current | 485 | By default, Lua CJSON will throw an error when trying to encode data |
449 | setting is always returned. | 486 | structures with more than 1000 nested tables. |
450 | 487 | ||
451 | This check prevents a deeply nested or recursive data structure from | 488 | The current setting is always returned, and is only updated when an |
452 | crashing the application. | 489 | argument is provided. |
453 | 490 | ||
454 | .Example: Recursive Lua tables | 491 | .Example: Recursive Lua table |
455 | [source,lua] | 492 | [source,lua] |
456 | a = {}; b = { a }; a[1] = b | 493 | a = {}; a[1] = a |
457 | |||
458 | Once the maximum table depth has been reached Lua CJSON will throw an error. | ||
459 | 494 | ||
460 | 495 | ||
496 | [[encode_number_precision]] | ||
461 | encode_number_precision | 497 | encode_number_precision |
462 | ~~~~~~~~~~~~~~~~~~~~~~~ | 498 | ~~~~~~~~~~~~~~~~~~~~~~~ |
463 | 499 | ||
464 | [source,lua] | 500 | [source,lua] |
465 | ------------ | 501 | ------------ |
466 | precision = cjson.encode_number_precision([precision]) | 502 | precision = cjson.encode_number_precision([precision]) |
467 | -- "precision" must be between 1 and 14 (inclusive) | 503 | -- "precision" must be an integer between 1 and 14. Default: 14. |
468 | ------------ | 504 | ------------ |
469 | 505 | ||
470 | By default, Lua CJSON will output 14 significant digits when | 506 | The amount of significant digits returned by Lua CJSON when encoding |
471 | converting a number to text. | 507 | numbers can be changed to balance accuracy versus performance. For data |
508 | structures containing many numbers, setting | ||
509 | +cjson.encode_number_precision+ to a smaller integer, for example +3+, | ||
510 | can improve encoding performance by up to 50%. | ||
472 | 511 | ||
473 | This setting is only changed when an argument is provided. The current | 512 | By default, Lua CJSON will output 14 significant digits when converting |
474 | setting is always returned. | 513 | a number to text. |
475 | 514 | ||
476 | Reducing number precision to _3_ can improve performance of number | 515 | The current setting is always returned, and is only updated when an |
477 | heavy JSON conversions by up to 50%. | 516 | argument is provided. |
478 | 517 | ||
479 | 518 | ||
480 | [[encode_sparse_array]] | 519 | [[encode_sparse_array]] |
@@ -485,39 +524,45 @@ encode_sparse_array | |||
485 | ------------ | 524 | ------------ |
486 | convert, ratio, safe = cjson.encode_sparse_array([convert[, ratio[, safe]]]) | 525 | convert, ratio, safe = cjson.encode_sparse_array([convert[, ratio[, safe]]]) |
487 | -- "convert" must be a boolean. Default: false. | 526 | -- "convert" must be a boolean. Default: false. |
488 | -- "ratio" must be a positive integer. Default: 2 | 527 | -- "ratio" must be a positive integer. Default: 2. |
489 | -- "safe" must be a positive integer. Default: 10 | 528 | -- "safe" must be a positive integer. Default: 10. |
490 | ------------ | 529 | ------------ |
491 | 530 | ||
492 | Lua CJSON classifies Lua tables into 3 types when encoding as a JSON | 531 | Lua CJSON classifies a Lua table into one of three kinds when encoding a |
493 | array. This is determined by the number of values missing for keys up | 532 | JSON array. This is determined by the number of values missing from the |
494 | to the maximum index: | 533 | Lua array as follows: |
495 | 534 | ||
496 | Each particular setting is only changed when that argument is | ||
497 | provided. The current settings are always returned. | ||
498 | |||
499 | [horizontal] | ||
500 | Normal:: All values are available. | 535 | Normal:: All values are available. |
501 | Sparse:: At least 1 value is missing. | 536 | Sparse:: At least 1 value is missing. |
502 | Excessively sparse:: The number of values missing exceed the configured ratio. | 537 | Excessively sparse:: The number of values missing exceeds the configured |
538 | ratio. | ||
539 | |||
540 | Lua CJSON encodes sparse Lua arrays as JSON arrays using JSON +null+ for | ||
541 | the missing entries. | ||
542 | |||
543 | An array is excessively sparse when all the following conditions are | ||
544 | met: | ||
545 | |||
546 | - +ratio+ > +0+ | ||
547 | - _maximum_index_ > +safe+ | ||
548 | - _maximum_index_ > _item_count_ * +ratio+ | ||
549 | |||
550 | Lua CJSON will never consider an array to be _excessively sparse_ when | ||
551 | +ratio+ = +0+. The +safe+ limit ensures that small Lua arrays are always | ||
552 | encoded as sparse arrays. | ||
503 | 553 | ||
504 | Lua CJSON encodes sparse Lua arrays by as JSON arrays using JSON +null+. | 554 | By default, attempting to encode an _excessively sparse_ array will |
555 | generate an error. If +convert+ is set to +true+, _excessively sparse_ | ||
556 | arrays will be converted to a JSON object. | ||
557 | |||
558 | The current settings are always returned. A particular setting is only | ||
559 | changed when the argument is provided (non-++nil++). | ||
505 | 560 | ||
506 | .Example: Encoding a sparse array | 561 | .Example: Encoding a sparse array |
507 | [source,lua] | 562 | [source,lua] |
508 | cjson.encode({ [3] = "data" }) | 563 | cjson.encode({ [3] = "data" }) |
509 | -- Returns: '[null,null,"data"]' | 564 | -- Returns: '[null,null,"data"]' |
510 | 565 | ||
511 | Lua CJSON checks for excessively sparse arrays when the maximum index | ||
512 | is greater an the +safe+ limit and +ratio+ is greater than +0+. An | ||
513 | array is _excessively sparse_ when: | ||
514 | |||
515 | _maximum_index_ > _item_count_ * +ratio+ | ||
516 | |||
517 | By default, attempting to encode excessively sparse arrays will | ||
518 | generate an error. If +convert+ is set to +true+, excessively sparse | ||
519 | arrays will be converted to a JSON object: | ||
520 | |||
521 | .Example: Enabling conversion to a JSON object | 566 | .Example: Enabling conversion to a JSON object |
522 | [source,lua] | 567 | [source,lua] |
523 | cjson.encode_sparse_array(true) | 568 | cjson.encode_sparse_array(true) |
@@ -537,7 +582,7 @@ The name of the Lua CJSON module (+"cjson"+). | |||
537 | _VERSION | 582 | _VERSION |
538 | ~~~~~~~~ | 583 | ~~~~~~~~ |
539 | 584 | ||
540 | The version number of the Lua CJSON module (Eg, +"2.0devel"+). | 585 | The version number of the Lua CJSON module (+"2.0devel"+). |
541 | 586 | ||
542 | 587 | ||
543 | null | 588 | null |
@@ -555,4 +600,4 @@ References | |||
555 | - http://www.json.org/[JSON website] | 600 | - http://www.json.org/[JSON website] |
556 | 601 | ||
557 | 602 | ||
558 | // vi:ft=asciidoc tw=70: | 603 | // vi:ft=asciidoc tw=72: |