aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMark Pulford <mark@kyne.com.au>2011-05-08 23:38:34 +0930
committerMark Pulford <mark@kyne.com.au>2011-05-08 23:38:34 +0930
commitffb9aa79eb828118b0ebd214d2e47a324df9bd74 (patch)
tree10660ea9556fd879de524a11d5abdcf62624d590
parent1798471fcdd3425e7d2bafbd69048ff1165eb31e (diff)
downloadlua-cjson-ffb9aa79eb828118b0ebd214d2e47a324df9bd74.tar.gz
lua-cjson-ffb9aa79eb828118b0ebd214d2e47a324df9bd74.tar.bz2
lua-cjson-ffb9aa79eb828118b0ebd214d2e47a324df9bd74.zip
Add documentation (README)
-rw-r--r--README211
-rw-r--r--lua-cjson.spec2
2 files changed, 212 insertions, 1 deletions
diff --git a/README b/README
new file mode 100644
index 0000000..71ad376
--- /dev/null
+++ b/README
@@ -0,0 +1,211 @@
1Lua CJSON v1.0
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- Optionally supports common JSON extensions (NaN, Inf,..).
14
15Caveats:
16- UTF-16 and UTF-32 are not supported.
17- Multiple OS threads within a single Lua state are not currently
18 supported.
19
20To obtain the latest version of Lua CJSON visit:
21
22 http://www.kyne.com.au/~mark/software/lua-cjson.php
23
24Feel free to email me if you have any patches, suggestions, or comments.
25
26- Mark Pulford <mark@kyne.com.au>
27
28
29Installing
30==========
31
32Build requirements:
33- Lua (http://www.lua.org/)
34Or:
35- LuaJIT (http://www.luajit.org/)
36
37The included Makefile should be reviewed and updated based on the
38location of your Lua header and library directories. Then:
39
40 # make
41 # make install
42 OR
43 # cp cjson.so [your_module_directory]
44
45Linux distributions using RPM should be able to build a package with
46the following command:
47
48 # rpmbuild -tb lua-cjson-1.0.tar.gz
49
50
51Lua CJSON API
52=============
53
54Synopsis
55--------
56
57 require "cjson"
58 -- Or:
59 local cjson = require "cjson"
60
61 -- Translate Lua value to/from JSON
62 text = cjson.encode(value)
63 value = cjson.decode(text)
64
65 -- Get and/or Set CJSON configuration
66 setting = cjson.refuse_invalid_numbers([setting])
67 depth = cjson.encode_max_depth([depth])
68 convert, ratio, safe = cjson.encode_sparse_array([convert[, ratio[, safe]]])
69
70
71Encoding
72--------
73
74 json_text = cjson.encode(value)
75
76cjson.encode() will serialise the following types:
77 * number, string, table, boolean, lightuserdata (NULL) or nil
78
79The remaining Lua types cannot be serialised:
80 * thread, userdata, lightuserdata (non-NULL), function
81
82Numbers are encoded using the standard Lua number format.
83
84ASCII 0 - 31, double-quote, forward-slash, black-slash and ASCII 127
85are escaped when encoding strings. Other octets are passed
86transparently. It is expected the application will perform UTF-8 error
87checking if required.
88
89If a Lua table only contains positive integer keys (>0) it is encoded
90as an array, otherwise it will be encoded as an object.
91
92A Lua table will only recognised as an array if all keys are type
93"number", and are positive integers (>0). Otherwise CJSON will encode
94the table as a JSON object.
95
96CJSON will also recognise and handle sparse arrays. Missing entries
97will be encoded as "null". Eg:
98 { [3] = "data" }
99becomes:
100 [ null, null, "data" ]
101
102Note: standards compliant JSON must be encapsulated in either an
103object ({}) or an array ([]). Hence you must pass a table to
104cjson.encode() if you want to generate standards compliant JSON
105output.
106
107By default, errors will be raised for:
108- Excessively sparse arrays (see below)
109- More than 20 nested tables
110- Invalid numbers (NaN, Infinity)
111
112These defaults can be changed with:
113- cjson.encode_sparse_array()
114- cjson.encode_max_depth()
115- cjson.refuse_invalid_numbers()
116
117Example:
118 data_obj = { true, { foo = "bar" } }
119 data_json = cjson.encode(data_obj)
120
121
122Decoding
123--------
124
125 value = cjson.decode(json_text)
126
127cjson.decode() will deserialise any UTF-8 JSON string into a Lua data
128structure. It can return any of the types that cjson.encode()
129supports.
130
131UTF-16 and UTF-32 JSON strings are not supported.
132
133CJSON only requires that NULL (\0) and double quote (\") are escaped
134within strings. All other octets will be passed transparently. UTF-8
135characters are not validated and should be checked elsewhere if
136desired.
137
138JSON "null" will be converted to a NULL lightuserdata value. This can
139be compared with cjson.null for convenience.
140
141By default, invalid numbers (NaN, Infinity, Hex) will be decoded
142correctly.
143
144Example:
145 data_json = '[ true, { "foo": "bar" } ]'
146 data_obj = cjson.decode(data_json)
147
148
149Invalid numbers
150---------------
151
152 setting = cjson.refuse_invalid_numbers([setting])
153 -- "setting" must be on of:
154 -- false, "encode", "decode", "both", true
155
156CJSON considers numbers which are outside the JSON specification to be
157"invalid". Eg:
158- Infinity
159- NaN
160- Hexadecimal numbers
161
162This setting can be configured separately for encoding and/or
163decoding:
164- Enabled: an error will be generated if an invalid number is found.
165- Disabled (encoding): NaN and Infinity can be encoded.
166- Disabled (decoding): All numbers supported by strtod(3) will be
167 parsed.
168
169
170Sparse arrays
171-------------
172
173 convert, ratio, safe = cjson.encode_sparse_array([convert[, ratio[, safe]]])
174 -- "convert" must be a boolean. Default: false.
175 -- "ratio" must be a positive integer (>0). Default: 2
176 -- "safe" must be a positive integer (>0). Default: 10
177
178CJSON detects excessively sparse arrays by comparing the number of
179items in an array with the maximum index. An excessively sparse array
180is defined as:
181
182 max_index > safe AND max_index > items * ratio
183
184Eg:
185 { [1000] = "excessively sparse array" }
186
187Setting "ratio" to 0 disables checking for excessively sparse arrays.
188
189When "convert" is enabled, CJSON will encode excessively sparse arrays
190as JSON objects.
191
192
193Nested tables
194-------------
195
196 depth = cjson.encode_max_depth([depth])
197 -- "depth" must be a positive integer (>0).
198
199By default, CJSON will reject data structure with more than 20 nested
200tables.
201
202This check is used to prevent a nested data structure from crashing
203the application. Eg:
204 a = {}; b = { a }; a[1] = b
205
206
207References
208==========
209
210- http://tools.ietf.org/html/rfc4627
211- http://www.json.org/
diff --git a/lua-cjson.spec b/lua-cjson.spec
index 1398107..8f3b8c5 100644
--- a/lua-cjson.spec
+++ b/lua-cjson.spec
@@ -39,7 +39,7 @@ rm -rf "$RPM_BUILD_ROOT"
39 39
40%files 40%files
41%defattr(-,root,root,-) 41%defattr(-,root,root,-)
42%doc LICENSE performance.txt rfc4627.txt tests 42%doc LICENSE performance.txt README rfc4627.txt tests
43%{lualibdir}/* 43%{lualibdir}/*
44 44
45 45