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