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