summaryrefslogtreecommitdiffhomepage
path: root/libs/json/luasrc/json.lua
blob: 3d6a576a8ce4d26c90851d4981e63674ff7b959e (plain)
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
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
--[[
LuCI - Lua Configuration Interface

Copyright 2008 Steven Barth <steven@midlink.org>
Copyright 2008 Jo-Philipp Wich <xm@leipzig.freifunk.net>

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

$Id$

Decoder:
	Info:
		null will be decoded to luci.json.null if first parameter of Decoder() is true
	
	Example:
		decoder = luci.json.Decoder()
		luci.ltn12.pump.all(luci.ltn12.source.string("decodableJSON"), decoder:sink())
		luci.util.dumptable(decoder:get())
		
	Known issues:
		does not support unicode conversion \uXXYY with XX != 00 will be ignored
		
			
Encoder:
	Info:
		Accepts numbers, strings, nil, booleans as they are
		Accepts luci.json.null as replacement for nil
		Accepts full associative and full numerically indexed tables
		Mixed tables will loose their associative values during conversion
		Iterator functions will be encoded as an array of their return values
		Non-iterator functions will probably corrupt the encoder
	
	Example:
		encoder = luci.json.Encoder(encodableData)
		luci.ltn12.pump.all(encoder:source(), luci.ltn12.sink.file(io.open("someFile", w)))
]]--

local util      = require "luci.util"
local table     = require "table"
local string    = require "string"
local coroutine = require "coroutine"

local assert    = assert
local tonumber  = tonumber
local tostring  = tostring
local error     = error
local type	    = type
local pairs	    = pairs
local ipairs    = ipairs
local next      = next

local getmetatable = getmetatable

module "luci.json"

--- Null replacement function
-- @return null
function null()
	return null
end


Encoder = util.class()

--- Creates a new Encoder.
-- @param data			Data to be encoded.
-- @param buffersize	Buffersize of returned data.
-- @param fastescape	Use non-standard escaping (don't escape control chars) 
function Encoder.__init__(self, data, buffersize, fastescape)
	self.data = data
	self.buffersize = buffersize or 512
	self.buffer = ""
	self.fastescape = fastescape
	
	getmetatable(self).__call = Encoder.source
end

--- Create an LTN12 source from the encoder object
-- @return LTN12 source
function Encoder.source(self)
	local source = coroutine.create(self.dispatch)
	return function()
		local res, data = coroutine.resume(source, self, self.data, true)
		if res then
			return data
		else
			return nil, data
		end
	end	
end

function Encoder.dispatch(self, data, start)
	local parser = self.parsers[type(data)]
	
	parser(self, data)
	
	if start then
		if #self.buffer > 0 then
			coroutine.yield(self.buffer)
		end
		
		coroutine.yield()
	end
end

function Encoder.put(self, chunk)
	if self.buffersize < 2 then
		corountine.yield(chunk)
	else
		if #self.buffer + #chunk > self.buffersize then
			local written = 0
			local fbuffer = self.buffersize - #self.buffer

			coroutine.yield(self.buffer .. chunk:sub(written + 1, fbuffer))
			written = fbuffer
			
			while #chunk - written > self.buffersize do
				fbuffer = written + self.buffersize
				coroutine.yield(chunk:sub(written + 1, fbuffer))
				written = fbuffer
			end 
			
			self.buffer = chunk:sub(written + 1)
		else
			self.buffer = self.buffer .. chunk
		end
	end
end

function Encoder.parse_nil(self)
	self:put("null")
end

function Encoder.parse_bool(self, obj)
	self:put(obj and "true" or "false")
end

function Encoder.parse_number(self, obj)
	self:put(tostring(obj))
end

function Encoder.parse_string(self, obj)
	if self.fastescape then
		self:put('"' .. obj:gsub('\\', '\\\\'):gsub('"', '\\"') .. '"')
	else
		self:put('"' ..
			obj:gsub('[%c\\"]',
				function(char)
					return '\\u00%02x' % char:byte()
				end
			)
		.. '"')
	end
end

function Encoder.parse_iter(self, obj)
	if obj == null then
		return self:put("null")
	end

	if type(obj) == "table" and (#obj == 0 and next(obj)) then
		self:put("{")
		local first = true
		
		for key, entry in pairs(obj) do
			first = first or self:put(",")
			first = first and false
			self:parse_string(tostring(key))
			self:put(":")
			self:dispatch(entry)
		end
		
		self:put("}") 		
	else
		self:put("[")
		local first = true
		
		if type(obj) == "table" then
			for i, entry in pairs(obj) do
				first = first or self:put(",")
				first = first and nil
				self:dispatch(entry)
			end
		else
			for entry in obj do
				first = first or self:put(",")
				first = first and nil
				self:dispatch(entry)
			end		
		end
		
		self:put("]") 	
	end
end

Encoder.parsers = {
	['nil']      = Encoder.parse_nil,
	['table']    = Encoder.parse_iter,
	['number']   = Encoder.parse_number,
	['string']   = Encoder.parse_string,
	['boolean']  = Encoder.parse_bool,
	['function'] = Encoder.parse_iter
} 



Decoder = util.class()

--- Create a new Decoder object.
-- @param customnull User luci.json.null instead of nil
function Decoder.__init__(self, customnull)
	self.cnull = customnull
	getmetatable(self).__call = Decoder.sink
end

--- Create an LTN12 sink from the decoder object.
-- @return LTN12 sink
function Decoder.sink(self)
	local sink = coroutine.create(self.dispatch)
	return function(...)
		return coroutine.resume(sink, self, ...)
	end
end


--- Get the decoded data packets
-- @return Decoded data
function Decoder.get(self)
	return self.data
end

function Decoder.dispatch(self, chunk, src_err, strict)
	local robject, object
	local oset = false
	 
	while chunk do
		while chunk and #chunk < 1 do
			chunk = self:fetch()
		end
		
		assert(not strict or chunk, "Unexpected EOS")
		if not chunk then break end
		
		local char   = chunk:sub(1, 1)
		local parser = self.parsers[char]
		 or (char:match("%s")     and self.parse_space)
		 or (char:match("[0-9-]") and self.parse_number)
		 or error("Unexpected char '%s'" % char)
		
		chunk, robject = parser(self, chunk)
		
		if parser ~= self.parse_space then
			assert(not oset, "Scope violation: Too many objects")
			object = robject
			oset = true
		
			if strict then
				return chunk, object
			end
		end
	end
	
	assert(not src_err, src_err)
	assert(oset, "Unexpected EOS")
	
	self.data = object
end


function Decoder.fetch(self)
	local tself, chunk, src_err = coroutine.yield()
	assert(chunk or not src_err, src_err)
	return chunk
end


function Decoder.fetch_atleast(self, chunk, bytes)
	while #chunk < bytes do
		local nchunk = self:fetch()
		assert(nchunk, "Unexpected EOS")
		chunk = chunk .. nchunk
	end
	
	return chunk
end


function Decoder.fetch_until(self, chunk, pattern)
	local start = chunk:find(pattern)

	while not start do
		local nchunk = self:fetch()
		assert(nchunk, "Unexpected EOS")
		chunk = chunk .. nchunk
		start = chunk:find(pattern)
	end

	return chunk, start
end


function Decoder.parse_space(self, chunk)
	local start = chunk:find("[^%s]")
	
	while not start do
		chunk = self:fetch()
		if not chunk then
			return nil
		end
		start = chunk:find("[^%s]")
	end
	
	return chunk:sub(start)
end


function Decoder.parse_literal(self, chunk, literal, value)
	chunk = self:fetch_atleast(chunk, #literal)	
	assert(chunk:sub(1, #literal) == literal, "Invalid character sequence")
	return chunk:sub(#literal + 1), value
end


function Decoder.parse_null(self, chunk)
	return self:parse_literal(chunk, "null", self.cnull and null)
end


function Decoder.parse_true(self, chunk)
	return self:parse_literal(chunk, "true", true)
end


function Decoder.parse_false(self, chunk)
	return self:parse_literal(chunk, "false", false)
end


function Decoder.parse_number(self, chunk)
	local chunk, start = self:fetch_until(chunk, "[^0-9eE.+-]")
	local number = tonumber(chunk:sub(1, start - 1))
	assert(number, "Invalid number specification")
	return chunk:sub(start), number
end


function Decoder.parse_string(self, chunk)
	local str = ""
	local object = nil
	assert(chunk:sub(1, 1) == '"', 'Expected "')
	chunk = chunk:sub(2)

	while true do
		local spos = chunk:find('[\\"]')
		if spos then
			str = str .. chunk:sub(1, spos - 1)
			
			local char = chunk:sub(spos, spos)
			if char == '"' then				-- String end
				chunk = chunk:sub(spos + 1)
				break
			elseif char == "\\" then 		-- Escape sequence
				chunk, object = self:parse_escape(chunk:sub(spos))
				str = str .. object
			end
		else
			str = str .. chunk
			chunk = self:fetch()
			assert(chunk, "Unexpected EOS while parsing a string")		
		end
	end

	return chunk, str
end


function Decoder.parse_escape(self, chunk)
	local str = ""
	chunk = self:fetch_atleast(chunk:sub(2), 1)
	local char = chunk:sub(1, 1)
	chunk = chunk:sub(2)
	
	if char == '"' then
		return chunk, '"'
	elseif char == "\\" then
		return chunk, "\\"
	elseif char == "u" then
		chunk = self:fetch_atleast(chunk, 4)
		local s1, s2 = chunk:sub(1, 2), chunk:sub(3, 4)
		s1, s2 = tonumber(s1, 16), tonumber(s2, 16)
		assert(s1 and s2, "Invalid Unicode character")
		
		-- ToDo: Unicode support
		return chunk:sub(5), s1 == 0 and string.char(s2) or ""
	elseif char == "/" then
		return chunk, "/"
	elseif char == "b" then
		return chunk, "\b"
	elseif char == "f" then
		return chunk, "\f"
	elseif char == "n" then
		return chunk, "\n"
	elseif char == "r" then
		return chunk, "\r"
	elseif char == "t" then
		return chunk, "\t"
	else
		error("Unexpected escaping sequence '\\%s'" % char)
	end
end


function Decoder.parse_array(self, chunk)
	chunk = chunk:sub(2)
	local array = {}
	local nextp = 1
	
	local chunk, object = self:parse_delimiter(chunk, "%]")
	
	if object then
		return chunk, array
	end
	
	repeat
		chunk, object = self:dispatch(chunk, nil, true)
		table.insert(array, nextp, object)
		nextp = nextp + 1
		
		chunk, object = self:parse_delimiter(chunk, ",%]")
		assert(object, "Delimiter expected")
	until object == "]"

	return chunk, array
end


function Decoder.parse_object(self, chunk)
	chunk = chunk:sub(2)
	local array = {}
	local name

	local chunk, object = self:parse_delimiter(chunk, "}")

	if object then
		return chunk, array
	end

	repeat
		chunk = self:parse_space(chunk)
		assert(chunk, "Unexpected EOS")
		
		chunk, name   = self:parse_string(chunk)
		
		chunk, object = self:parse_delimiter(chunk, ":")
		assert(object, "Separator expected")
		
		chunk, object = self:dispatch(chunk, nil, true)
		array[name] = object

		chunk, object = self:parse_delimiter(chunk, ",}")
		assert(object, "Delimiter expected")
	until object == "}"

	return chunk, array
end


function Decoder.parse_delimiter(self, chunk, delimiter)
	while true do
		chunk = self:fetch_atleast(chunk, 1)
		local char = chunk:sub(1, 1)
		if char:match("%s") then
			chunk = self:parse_space(chunk)
			assert(chunk, "Unexpected EOS")
		elseif char:match("[%s]" % delimiter) then
			return chunk:sub(2), char
		else
			return chunk, nil
		end
	end
end


Decoder.parsers = { 
	['"'] = Decoder.parse_string,
	['t'] = Decoder.parse_true,
	['f'] = Decoder.parse_false,
	['n'] = Decoder.parse_null,
	['['] = Decoder.parse_array,
	['{'] = Decoder.parse_object
}