aboutsummaryrefslogtreecommitdiff
path: root/README
diff options
context:
space:
mode:
Diffstat (limited to 'README')
-rw-r--r--README328
1 files changed, 0 insertions, 328 deletions
diff --git a/README b/README
deleted file mode 100644
index 36c9a64..0000000
--- a/README
+++ /dev/null
@@ -1,328 +0,0 @@
1Lua CJSON v1.0.4
2================
3
4Lua CJSON is covered by the MIT license. See the file "LICENSE" for
5details.
6
7Lua CJSON provides fast JSON parsing and encoding support for Lua.
8
9Features:
10- 10x to 20x quicker (or more) than the fastest pure Lua JSON modules.
11- Full support for JSON with UTF-8, including decoding surrogate
12 pairs.
13- Optional run-time support for common exceptions to the JSON
14 specification (NaN, Inf,..).
15
16Caveats:
17- UTF-16 and UTF-32 are not supported.
18- Multi-threading within a single Lua state is not supported.
19 However, this is not a recommended configuration under Lua.
20
21To obtain the latest version of Lua CJSON visit:
22
23 http://www.kyne.com.au/~mark/software/lua-cjson.php
24
25Feel free to email me if you have any patches, suggestions, or
26comments.
27
28- Mark Pulford <mark@kyne.com.au>
29
30
31Installing
32==========
33
34Build requirements:
35- Lua (http://www.lua.org/)
36Or:
37- LuaJIT (http://www.luajit.org/)
38
39There are 3 build methods available:
40- Make: POSIX, OSX
41- RPM: Various Linux distributions
42- LuaRocks (http://www.luarocks.org/): POSIX, OSX, Windows
43
44
45Make
46----
47
48Review and update the included Makefile to suit your platform. Next,
49build and install the module:
50
51 # make
52 # make install
53 OR
54 # cp cjson.so [your_module_directory]
55
56
57RPM
58---
59
60RPM-based Linux distributions should be able to create a package using
61the included RPM spec file. Install the "rpm-build" package, or
62similar, then:
63
64 # rpmbuild -tb lua-cjson-1.0.4.tar.gz
65
66
67LuaRocks
68--------
69
70LuaRocks (http://luarocks.org/) can be used to install and manage Lua
71modules on a wide range of platforms (including Windows).
72
73Extract the Lua CJSON source package into a directory and run:
74
75 # cd lua-cjson-1.0.4; luarocks make
76
77LuaRocks does not support platform specific configuration for Solaris.
78On Solaris, you may need to manually uncomment "USE_INTERNAL_ISINF" in
79the rockspec before building this module.
80
81See the LuaRocks documentation for further details.
82
83
84Lua CJSON API
85=============
86
87Synopsis
88--------
89
90 require "cjson"
91 -- Or:
92 local cjson = require "cjson"
93
94 -- Translate Lua value to/from JSON
95 text = cjson.encode(value)
96 value = cjson.decode(text)
97
98 -- Get and/or set CJSON configuration
99 setting = cjson.refuse_invalid_numbers([setting])
100 depth = cjson.encode_max_depth([depth])
101 convert, ratio, safe = cjson.encode_sparse_array([convert[, ratio[, safe]]])
102 keep = cjson.encode_keep_buffer([keep])
103
104
105Encoding
106--------
107
108 json_text = cjson.encode(value)
109
110cjson.encode() will serialise the following types:
111 * number, string, table, boolean, lightuserdata (NULL) or nil
112
113The remaining Lua types cannot be serialised:
114 * thread, userdata, lightuserdata (non-NULL), function
115
116Numbers are encoded using the standard Lua number format.
117
118ASCII 0 - 31, double-quote, forward-slash, black-slash and ASCII 127
119are escaped when encoding strings. Other octets are passed
120transparently. It is expected the application will perform UTF-8 error
121checking if required.
122
123A Lua table will only be recognised as an array if all keys are type
124"number" and are positive integers (>0). Otherwise CJSON will encode
125the table as a JSON object.
126
127CJSON will also recognise and handle sparse arrays. Missing entries
128will be encoded as "null". Eg:
129 { [3] = "data" }
130becomes:
131 [null,null,"data"]
132
133Note: standards compliant JSON must be encapsulated in either an
134object ({}) or an array ([]). You must pass a table to cjson.encode()
135if you want to generate standards compliant JSON output.
136
137By default, errors will be raised for:
138- Excessively sparse arrays (see below)
139- More than 20 nested tables
140- Invalid numbers (NaN, Infinity)
141
142These defaults can be changed with:
143- cjson.encode_sparse_array()
144- cjson.encode_max_depth()
145- cjson.refuse_invalid_numbers()
146
147Example:
148 data_obj = { true, { foo = "bar" } }
149 data_json = cjson.encode(data_obj)
150
151
152Decoding
153--------
154
155 value = cjson.decode(json_text)
156
157cjson.decode() will deserialise any UTF-8 JSON string into a Lua data
158structure. It can return any of the types that cjson.encode()
159supports.
160
161UTF-16 and UTF-32 JSON strings are not supported.
162
163CJSON requires that NULL (\0) and double quote (\") are escaped within
164strings. All escape codes will be decoded and other characters will be
165passed transparently. UTF-8 characters are not validated during
166decoding and should be checked elsewhere if required.
167
168JSON "null" will be converted to a NULL lightuserdata value. This can
169be compared with cjson.null for convenience.
170
171By default, invalid numbers (NaN, Infinity, Hexidecimal) will be
172decoded.
173
174Example:
175 data_json = '[ true, { "foo": "bar" } ]'
176 data_obj = cjson.decode(data_json)
177
178
179Invalid numbers
180---------------
181
182 setting = cjson.refuse_invalid_numbers([setting])
183 -- "setting" must be on of:
184 -- false, "encode", "decode", "both", true
185
186CJSON considers numbers which are outside the JSON specification to be
187"invalid". Eg:
188- Infinity
189- NaN
190- Hexadecimal numbers
191
192By default CJSON will decode "invalid" numbers, but will refuse to
193encode them.
194
195This setting can be configured separately for encoding and/or
196decoding:
197- Enabled: an error will be generated if an invalid number is found.
198- Disabled (encoding): NaN and Infinity can be encoded.
199- Disabled (decoding): All numbers supported by strtod(3) will be
200 parsed.
201
202
203Sparse arrays
204-------------
205
206 convert, ratio, safe = cjson.encode_sparse_array([convert[, ratio[, safe]]])
207 -- "convert" must be a boolean. Default: false.
208 -- "ratio" must be a positive integer (>0). Default: 2
209 -- "safe" must be a positive integer (>0). Default: 10
210
211A Lua array is sparse if it is missing a value for at least 1 index.
212Lua CJSON encodes missing values as "null". Eg:
213 Lua array: { [3] = "sparse" }
214 JSON array: [null,null,"sparse"]
215
216CJSON detects excessively sparse arrays by comparing the number of
217items in a Lua array with the maximum index. In particular:
218
219 maximum index > safe AND maximum index > array_items * ratio
220
221By default, attempting to encode excessively sparse arrays will
222generate an error.
223
224If "convert" is set to "true", excessively sparse arrays will be
225encoded as a JSON object:
226 Lua array: { [1000] = "excessively sparse" }
227 JSON array: {"1000":"excessively sparse"}
228
229Setting "ratio" to 0 disables checking for excessively sparse arrays.
230
231
232Nested tables
233-------------
234
235 depth = cjson.encode_max_depth([depth])
236 -- "depth" must be a positive integer (>0).
237
238By default, CJSON will reject data structure with more than 20 nested
239tables.
240
241This check is used to prevent a nested data structure from crashing
242the application. Eg:
243 a = {}; b = { a }; a[1] = b
244
245
246Number precision
247----------------
248
249 precision = cjson.encode_number_precision([precision])
250 -- "precision" must be between 1 and 14 (inclusive)
251
252By default CJSON will output 14 significant digits when converting a
253number to text.
254
255Reducing number precision to 3 can improve performance of number
256heavy conversions by up to 50%.
257
258
259Persistent encoding buffer
260--------------------------
261
262 keep = cjson.keep_encode_buffer([keep])
263 -- "keep" must be a boolean
264
265By default, CJSON will reuse the JSON encoding buffer to improve
266performance. The buffer will grow to the largest size required and is
267not freed until CJSON is garbage collected. Setting this option to
268"false" will cause the buffer to be freed after each call to
269cjson.encode().
270
271
272JSON and handling under Lua CJSON
273=================================
274
275Nulls
276-----
277
278Lua CJSON decodes JSON "null" as a Lua lightuserdata NULL pointer.
279
280As a convenience, "cjson.null" is provided for comparison.
281
282
283Table keys
284----------
285
286JSON object keys must be strings - other types are not supported. Lua
287CJSON will convert numeric keys to a string, and other non-string
288types will generate an error.
289
290JSON object keys are always be decoded as Lua strings.
291
292If all Lua table keys are numbers (not strings), Lua CJSON will
293encode the table as a JSON array. See "Sparse arrays" above for
294more details.
295
296
297Metamethods
298-----------
299
300Lua CJSON does not use metamethods when serialising tables.
301- next() is used to iterate over tables.
302- rawget() is used to iterate over arrays.
303
304
305Functions, Userdata, Threads
306----------------------------
307
308Lua CJSON will generate an error if asked to serialise Lua functions,
309userdata, lightuserdata or threads.
310
311
312Locales
313-------
314
315Lua CJSON uses strtod() and snprintf() to perform numeric conversion
316as they are usually well supported, fast and bug free.
317
318To ensure JSON encoding/decoding works correctly for locales using
319comma decimal separators, Lua CJSON must be compiled with either
320USE_POSIX_USELOCALE or USE_POSIX_SETLOCALE. See the Makefile or the
321rockspec for details.
322
323
324References
325==========
326
327- http://tools.ietf.org/html/rfc4627
328- http://www.json.org/