-
Notifications
You must be signed in to change notification settings - Fork 10
/
README
271 lines (203 loc) · 9.58 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
**********************************************************************
* Author : Brian Maher <maherb at brimworks dot com>
* Library : lua_yajl - Lua 5.1 interface to yajl.
*
* The MIT License
*
* Copyright (c) 2009 Brian Maher
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
**********************************************************************
To use this library, you need yajl version 2.x, get it here:
http://lloyd.github.com/yajl/
To build this library, you can use CMake (which is how yajl is built):
http://www.cmake.org/cmake/resources/software.html
...or you can use lua rocks.
...or you can use GNU Make. If the headers and/or library are
installed in non-standard locations, simply set the LIBDIR and/or
INCDIR make variables. For example:
make macosx \
LIBDIR='-L/opt/lua/lib -L/opt/yajl/lib' \
INCDIR='-I/opt/lua/include -I/opt/yajl/include'
Loading the library:
If you built the library as a loadable package
[local] yajl = require 'yajl'
If you compiled the package statically into your application, call
the function "luaopen_yajl(L)". It will create a table with the yajl
functions and leave it on the stack.
NOTE ON LARGE NUMBERS:
JSON integers and floating points are handled with the 'double' C
type, which is normally the native number format for Lua. We do
NOT currently do error handling if a number is outside of range,
instead the number will be truncated as specified by the strtod()
C function. At some point in the future perhaps we will add
support for custom error handlers if a double can not accurately
represent the JSON number. (want to write this support!?)
-- yajl functions --
yajl.null
A light user data object that represents the JSON null value. We
use this instead of nil to represent null since Lua tables can not
have a value of nil (that would delete the key-value pair), and
nil's can not exist at the end of an array.
If you don't care about streaming, here are two convenience methods:
string = yajl.to_string(lua_obj)
Translate a lua object into a JSON representation by creating a
generator and calling the 'value' method with the passed in
lua_obj and returns the result. See generator:value() for more
information about this operation. Note that information loss
may happen in certain situations. For example, empty tables are
always translated into empty arrays.
lua_obj = yajl.to_value(string)
Translate a string to a lua object. This is done in C by running
code that is essentially the same as this (but faster):
function to_value(string)
local result
local stack = {
function(val) result = val end
}
local obj_key
local events = {
value: function(_, val)
stack[#stack](val)
end,
open_array: function()
local arr = {}
stack[#stack](arr)
table.insert(stack, function(val)
table.insert(arr, val)
end)
end,
open_object: function()
local obj = {}
stack[#stack](obj)
table.insert(stack, function(val)
obj[obj_key] = val
end)
end,
object_key: function(_, val)
obj_key = val
end,
close: function()
stack[#stack] = nil
end,
}
yajl.parser({ events: events })(string)
return result
end
parser = yajl.parser {
allow_comments: true,
check_utf8: false,
events: {
value: function(events, value, type) ... end,
open_object: function(events) ... end,
object_key: function(events, string) ... end,
open_array: function(events) ... end,
close: function(events, type) ... end,
},
}
If allow_comments is true, then both // and /* ... */ javascript
comments are allowed. If check_utf8 is true, then the parser will
make sure all input is valid UTF8.
The results of this function is a parser function:
parser(string)
Call it with a string to be parsed.
The events table has various callback functions that will be
called when the events happen. All callbacks are passed the
events table as the first argument. Note that the yajl.generator
conforms to this events interface. Here is the details of each
callback function:
events:value(value, type)
Called when a JSON string, boolean, integer, double, or null
is found in the input stream. The type parameter is one of
these strings depending on what was found in the input stream:
"string" - A JSON string was found.
"boolean" - A JSON true or false was found.
"number" - A JSON number was found.
"null" - A JSON null was found.
Note that nulls are NOT represented as a Lua nil value,
instead they are the yajl.null value.
events:open_object()
An open object brace was found.
events:object_key(string)
If defined, instead of calling events:value(string, "string")
when an object key is found, this method is called instead.
events:open_array()
An open array brace was found.
events:close(type)
A close array or object brace was found. Type is the string
"object" or "array" depending on what type is being
terminated.
generator = yajl.generator {
printer: function(string) print(string) end,
indent: "\t",
}
Create a new generator with the specified printer callback,
optionally specify a whitespace string to be used for indenting if
you want "pretty" output. The printer must be a function that
takes a single string parameter:
printer(string)
The returned value is ignored, string is the text to be
written to the stream. Single characters are "printed" to the
stream, so if writing to a file, be sure it is buffered.
The returned generator object has the following methods:
generator = generator:value(lua_object)
Traverse the lua_object and call the appropriate generator
methods. lua_object can be any of these types:
number/boolean/string - Generates an appropriate JSON
representation.
nil/yajl.null - Generates a JSON null representation.
metatable(lua_object).__gen_json(lua_object, generator) -
If the lua_object has a metatable with a __gen_json
function in the metatable, then the work of generating
a JSON representation is delegated to that function.
table - If all keys in the table are positive integers,
generates a JSON array representation with element 1
being the first element of the array. Otherwise, it
is reprsented as a JSON object, and all keys are
coorced into strings using tostring() lua function.
The values are recursively traversed in depth first
fashion. All keys that begin with two underscores are
ignored. An empty table is always treated as an empty
array.
All other types are ignored (thread, userdata) unless there is
an appropriate __gen_json metatable method.
generator = generator:integer(number)
Generate an integer JSON representation.
generator = generator:double(number)
Generate a floating point JSON representation.
generator = generator:number(number)
Generate a number JSON representation. If the number is
infinity, we will generate the special 1e+666 number. If the
number is NAN (not a number), then we generate -0. At least
this way there is minimal information loss (since 1e+666 can
not be represented in a double, that will be re-encoded as
infinity).
generator = generator:string(string)
Generate a string JSON representation with proper escaping.
generator = generator:null()
Generate a null JSON representation.
generator = generator:boolean(boolean)
Generate a boolean JSON representation.
generator = generator:open_object()
Begin generating a JSON object. May only be followed with a
call to generator:close() or generator:string().
generator = generator:open_array()
Begin generating an array.
generator = generator:close()
End generation of an array or object.