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