aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMark Pulford <mark@kyne.com.au>2011-09-15 20:42:13 +0930
committerMark Pulford <mark@kyne.com.au>2011-09-15 20:42:13 +0930
commit62ec85b44f3050a975f75f0c878871d4f1fbac7c (patch)
treea9dd22ea4ed63e26da3e9866509ab4b479ac426d
parentd49e97d59f9c7ca1882279a15d8345595b19ef11 (diff)
downloadlua-cjson-62ec85b44f3050a975f75f0c878871d4f1fbac7c.tar.gz
lua-cjson-62ec85b44f3050a975f75f0c878871d4f1fbac7c.tar.bz2
lua-cjson-62ec85b44f3050a975f75f0c878871d4f1fbac7c.zip
Add limitations section to documentation
Improve text for clarity.
-rw-r--r--README124
1 files changed, 89 insertions, 35 deletions
diff --git a/README b/README
index d080ce3..473bf83 100644
--- a/README
+++ b/README
@@ -10,7 +10,8 @@ Features:
10- 10x to 20x quicker (or more) than the fastest pure Lua JSON modules. 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 11- Full support for JSON with UTF-8, including decoding surrogate
12 pairs. 12 pairs.
13- Optionally supports common JSON extensions (NaN, Inf,..). 13- Optional run-time support for common exceptions to the JSON
14 specification (NaN, Inf,..).
14 15
15Caveats: 16Caveats:
16- UTF-16 and UTF-32 are not supported. 17- UTF-16 and UTF-32 are not supported.
@@ -22,7 +23,8 @@ To obtain the latest version of Lua CJSON visit:
22 23
23 http://www.kyne.com.au/~mark/software/lua-cjson.php 24 http://www.kyne.com.au/~mark/software/lua-cjson.php
24 25
25Feel free to email me if you have any patches, suggestions, or comments. 26Feel free to email me if you have any patches, suggestions, or
27comments.
26 28
27- Mark Pulford <mark@kyne.com.au> 29- Mark Pulford <mark@kyne.com.au>
28 30
@@ -37,14 +39,15 @@ Or:
37 39
38There are 3 build methods available: 40There are 3 build methods available:
39- Gmake: POSIX, OSX 41- Gmake: POSIX, OSX
40- RPM: Some Linux distributions 42- RPM: Various Linux distributions
41- LuaRocks (http://www.luarocks.org/): POSIX, OSX, Windows 43- LuaRocks (http://www.luarocks.org/): POSIX, OSX, Windows
42 44
43 45
44Gmake 46Gmake
45----- 47-----
46 48
47Review and update the included Makefile to suit your platform. Then: 49Review and update the included Makefile to suit your platform. Next,
50build and install the module:
48 51
49 # gmake 52 # gmake
50 # gmake install 53 # gmake install
@@ -58,8 +61,9 @@ Note: Some Solaris platforms are missing isinf(). You can work around
58RPM 61RPM
59--- 62---
60 63
61Linux distributions using RPM should be able to build a package with 64RPM-based Linux distributions should be able to create a package using
62the following command: 65the included RPM spec file. Install the "rpm-build" package, or
66similar, then:
63 67
64 # rpmbuild -tb lua-cjson-1.0.3.tar.gz 68 # rpmbuild -tb lua-cjson-1.0.3.tar.gz
65 69
@@ -67,10 +71,15 @@ the following command:
67LuaRocks 71LuaRocks
68-------- 72--------
69 73
70Extract the source package into a directory and run: 74LuaRocks (http://luarocks.org/) can be used to install and manage Lua
75modules on a wide range of platforms (including Windows).
76
77Extract the Lua CJSON source package into a directory and run:
71 78
72 # cd lua-cjson-1.0.3; luarocks make 79 # cd lua-cjson-1.0.3; luarocks make
73 80
81See the LuaRocks documentation for further details.
82
74 83
75Lua CJSON API 84Lua CJSON API
76============= 85=============
@@ -86,7 +95,7 @@ Synopsis
86 text = cjson.encode(value) 95 text = cjson.encode(value)
87 value = cjson.decode(text) 96 value = cjson.decode(text)
88 97
89 -- Get and/or Set CJSON configuration 98 -- Get and/or set CJSON configuration
90 setting = cjson.refuse_invalid_numbers([setting]) 99 setting = cjson.refuse_invalid_numbers([setting])
91 depth = cjson.encode_max_depth([depth]) 100 depth = cjson.encode_max_depth([depth])
92 convert, ratio, safe = cjson.encode_sparse_array([convert[, ratio[, safe]]]) 101 convert, ratio, safe = cjson.encode_sparse_array([convert[, ratio[, safe]]])
@@ -111,23 +120,19 @@ are escaped when encoding strings. Other octets are passed
111transparently. It is expected the application will perform UTF-8 error 120transparently. It is expected the application will perform UTF-8 error
112checking if required. 121checking if required.
113 122
114If a Lua table only contains positive integer keys (>0) it is encoded 123A Lua table will only be recognised as an array if all keys are type
115as an array, otherwise it will be encoded as an object. 124"number" and are positive integers (>0). Otherwise CJSON will encode
116
117A Lua table will only recognised as an array if all keys are type
118"number", and are positive integers (>0). Otherwise CJSON will encode
119the table as a JSON object. 125the table as a JSON object.
120 126
121CJSON will also recognise and handle sparse arrays. Missing entries 127CJSON will also recognise and handle sparse arrays. Missing entries
122will be encoded as "null". Eg: 128will be encoded as "null". Eg:
123 { [3] = "data" } 129 { [3] = "data" }
124becomes: 130becomes:
125 [ null, null, "data" ] 131 [null,null,"data"]
126 132
127Note: standards compliant JSON must be encapsulated in either an 133Note: standards compliant JSON must be encapsulated in either an
128object ({}) or an array ([]). Hence you must pass a table to 134object ({}) or an array ([]). You must pass a table to cjson.encode()
129cjson.encode() if you want to generate standards compliant JSON 135if you want to generate standards compliant JSON output.
130output.
131 136
132By default, errors will be raised for: 137By default, errors will be raised for:
133- Excessively sparse arrays (see below) 138- Excessively sparse arrays (see below)
@@ -155,16 +160,16 @@ supports.
155 160
156UTF-16 and UTF-32 JSON strings are not supported. 161UTF-16 and UTF-32 JSON strings are not supported.
157 162
158CJSON only requires that NULL (\0) and double quote (\") are escaped 163CJSON requires that NULL (\0) and double quote (\") are escaped within
159within strings. All other octets will be passed transparently. UTF-8 164strings. All escape codes will be decoded and other characters will be
160characters are not validated and should be checked elsewhere if 165passed transparently. UTF-8 characters are not validated during
161desired. 166decoding and should be checked elsewhere if required.
162 167
163JSON "null" will be converted to a NULL lightuserdata value. This can 168JSON "null" will be converted to a NULL lightuserdata value. This can
164be compared with cjson.null for convenience. 169be compared with cjson.null for convenience.
165 170
166By default, invalid numbers (NaN, Infinity, Hex) will be decoded 171By default, invalid numbers (NaN, Infinity, Hexidecimal) will be
167correctly. 172decoded.
168 173
169Example: 174Example:
170 data_json = '[ true, { "foo": "bar" } ]' 175 data_json = '[ true, { "foo": "bar" } ]'
@@ -184,10 +189,13 @@ CJSON considers numbers which are outside the JSON specification to be
184- NaN 189- NaN
185- Hexadecimal numbers 190- Hexadecimal numbers
186 191
192By default CJSON will decode "invalid" numbers, but will refuse to
193encode them.
194
187This setting can be configured separately for encoding and/or 195This setting can be configured separately for encoding and/or
188decoding: 196decoding:
189- Enabled: an error will be generated if an invalid number is found. 197- Enabled: an error will be generated if an invalid number is found.
190- Disabled (encoding): NaN and Infinity can be encoded. 198- Disabled (encoding): NaN and Infinity can be encoded.
191- Disabled (decoding): All numbers supported by strtod(3) will be 199- Disabled (decoding): All numbers supported by strtod(3) will be
192 parsed. 200 parsed.
193 201
@@ -200,19 +208,25 @@ Sparse arrays
200 -- "ratio" must be a positive integer (>0). Default: 2 208 -- "ratio" must be a positive integer (>0). Default: 2
201 -- "safe" must be a positive integer (>0). Default: 10 209 -- "safe" must be a positive integer (>0). Default: 10
202 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
203CJSON detects excessively sparse arrays by comparing the number of 216CJSON detects excessively sparse arrays by comparing the number of
204items in an array with the maximum index. An excessively sparse array 217items in a Lua array with the maximum index. In particular:
205is defined as:
206 218
207 max_index > safe AND max_index > items * ratio 219 maximum index > safe AND maximum index > array_items * ratio
208 220
209Eg: 221By default, attempting to encode excessively sparse arrays will
210 { [1000] = "excessively sparse array" } 222generate an error.
211 223
212Setting "ratio" to 0 disables checking for excessively sparse arrays. 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"}
213 228
214When "convert" is enabled, CJSON will encode excessively sparse arrays 229Setting "ratio" to 0 disables checking for excessively sparse arrays.
215as JSON objects.
216 230
217 231
218Nested tables 232Nested tables
@@ -235,15 +249,15 @@ Number precision
235 precision = cjson.encode_number_precision([precision]) 249 precision = cjson.encode_number_precision([precision])
236 -- "precision" must be between 1 and 14 (inclusive) 250 -- "precision" must be between 1 and 14 (inclusive)
237 251
238By default CJSON will use up to 14 digits for precision when 252By default CJSON will output 14 significant digits when converting a
239converting a number to text. 253number to text.
240 254
241Reducing number precision to 3 can improve performance of number 255Reducing number precision to 3 can improve performance of number
242heavy conversions by up to 50%. 256heavy conversions by up to 50%.
243 257
244 258
245Persistent encoding buffer 259Persistent encoding buffer
246------------------------- 260--------------------------
247 261
248 keep = cjson.keep_encode_buffer([keep]) 262 keep = cjson.keep_encode_buffer([keep])
249 -- "keep" must be a boolean 263 -- "keep" must be a boolean
@@ -255,6 +269,46 @@ not freed until CJSON is garbage collected. Setting this option to
255cjson.encode(). 269cjson.encode().
256 270
257 271
272Lua / JSON limitations and CJSON
273================================
274
275Null handling
276-------------
277
278Lua CJSON decodes JSON "null" as a Lua lightuserdata NULL pointer.
279
280CJSON provides "cjson.null" as a convenience 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 when iterating 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
258References 312References
259========== 313==========
260 314