Skip to content

Commit 2c2f71d

Browse files
committed
checked in the ljson.lua library for the test suite for sorted-keys json outputs.
1 parent ccfa9c6 commit 2c2f71d

File tree

4 files changed

+171
-82
lines changed

4 files changed

+171
-82
lines changed

t/big.t

Lines changed: 17 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ my $pwd = cwd();
1919

2020
our $HttpConfig = qq{
2121
resolver \$TEST_NGINX_RESOLVER;
22-
lua_package_path "$pwd/lib/?.lua;;";
22+
lua_package_path "$pwd/lib/?.lua;$pwd/t/lib/?.lua;;";
2323
lua_package_cpath "/usr/local/openresty-debug/lualib/?.so;/usr/local/openresty/lualib/?.so;;";
2424
};
2525

@@ -43,7 +43,7 @@ __DATA__
4343
--- config
4444
location /t {
4545
content_by_lua '
46-
local cjson = require "cjson"
46+
local ljson = require "ljson"
4747
4848
local mysql = require "resty.mysql"
4949
local db = mysql:new()
@@ -97,15 +97,15 @@ __DATA__
9797
return
9898
end
9999
100-
ngx.say("result: ", cjson.encode(res))
100+
ngx.say("result: ", ljson.encode(res))
101101
102102
res, err, errno, sqlstate = db:query("select * from cats order by id desc")
103103
if not res then
104104
ngx.say("bad result: ", err, ": ", errno, ": ", sqlstate, ".")
105105
return
106106
end
107107
108-
ngx.say("result: ", cjson.encode(res))
108+
ngx.say("result: ", ljson.encode(res))
109109
110110
local ok, err = db:close()
111111
if not ok then
@@ -121,10 +121,10 @@ GET /t
121121
table cats dropped.
122122
table cats created.
123123
1 rows inserted into table cats (last id: 1)
124-
result: [{"name":"' . ('B' x 1024)
125-
. '","id":"1"}]' . "\n" .
126-
'result: [{"name":"' . ('B' x 1024)
127-
. '","id":"1"}]' . "\n"
124+
result: [{"id":"1","name":"' . ('B' x 1024)
125+
. '"}]' . "\n" .
126+
'result: [{"id":"1","name":"' . ('B' x 1024)
127+
. '"}]' . "\n"
128128
--- no_error_log
129129
[error]
130130

@@ -135,7 +135,7 @@ result: [{"name":"' . ('B' x 1024)
135135
--- config
136136
location /t {
137137
content_by_lua '
138-
local cjson = require "cjson"
138+
local ljson = require "ljson"
139139
140140
local mysql = require "resty.mysql"
141141
local db = mysql:new()
@@ -191,7 +191,7 @@ result: [{"name":"' . ('B' x 1024)
191191
return
192192
end
193193
194-
ngx.say("result: ", cjson.encode(res))
194+
ngx.say("result: ", ljson.encode(res))
195195
196196
res, err, errno, sqlstate =
197197
db:query("select * from cats order by id desc")
@@ -200,7 +200,7 @@ result: [{"name":"' . ('B' x 1024)
200200
return
201201
end
202202
203-
ngx.say("result: ", cjson.encode(res))
203+
ngx.say("result: ", ljson.encode(res))
204204
205205
local ok, err = db:close()
206206
if not ok then
@@ -228,7 +228,7 @@ bad result: packet size too big: 1029: nil: nil.
228228
--- config
229229
location /t {
230230
content_by_lua '
231-
local cjson = require "cjson"
231+
local ljson = require "ljson"
232232
233233
local mysql = require "resty.mysql"
234234
local db = mysql:new()
@@ -282,15 +282,15 @@ bad result: packet size too big: 1029: nil: nil.
282282
return
283283
end
284284
285-
ngx.say("result: ", cjson.encode(res))
285+
ngx.say("result: ", ljson.encode(res))
286286
287287
res, err, errno, sqlstate = db:query("select name from cats order by id desc")
288288
if not res then
289289
ngx.say("bad result: ", err, ": ", errno, ": ", sqlstate, ".")
290290
return
291291
end
292292
293-
ngx.say("result: ", cjson.encode(res))
293+
ngx.say("result: ", ljson.encode(res))
294294
295295
local ok, err = db:close()
296296
if not ok then
@@ -320,7 +320,7 @@ result: [{"name":"' . ('B' x 1024)
320320
--- config
321321
location /t {
322322
content_by_lua '
323-
local cjson = require "cjson"
323+
local ljson = require "ljson"
324324
325325
local mysql = require "resty.mysql"
326326
local db = mysql:new()
@@ -374,15 +374,15 @@ result: [{"name":"' . ('B' x 1024)
374374
return
375375
end
376376
377-
ngx.say("result: ", cjson.encode(res))
377+
ngx.say("result: ", ljson.encode(res))
378378
379379
res, err, errno, sqlstate = db:query("select name from cats order by id desc")
380380
if not res then
381381
ngx.say("bad result: ", err, ": ", errno, ": ", sqlstate, ".")
382382
return
383383
end
384384
385-
ngx.say("result: ", cjson.encode(res))
385+
ngx.say("result: ", ljson.encode(res))
386386
387387
local ok, err = db:close()
388388
if not ok then

t/compact_arrays.t

Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ my $pwd = cwd();
1111

1212
our $HttpConfig = qq{
1313
resolver \$TEST_NGINX_RESOLVER;
14-
lua_package_path "$pwd/lib/?.lua;;";
14+
lua_package_path "$pwd/lib/?.lua;$pwd/t/lib/?.lua;;";
1515
lua_package_cpath "/usr/local/openresty-debug/lualib/?.so;/usr/local/openresty/lualib/?.so;;";
1616
};
1717

@@ -66,8 +66,8 @@ __DATA__
6666
ngx.say("bad result: ", err, ": ", errno, ": ", sqlstate, ".")
6767
end
6868
69-
local cjson = require "cjson"
70-
ngx.say("result: ", cjson.encode(res))
69+
local ljson = require "ljson"
70+
ngx.say("result: ", ljson.encode(res))
7171
7272
local ok, err = db:close()
7373
if not ok then
@@ -92,7 +92,7 @@ result: (?:{"insert_id":0,"server_status":2,"warning_count":[01],"affected_rows"
9292
--- config
9393
location /t {
9494
content_by_lua '
95-
local cjson = require "cjson"
95+
local ljson = require "ljson"
9696
9797
local mysql = require "resty.mysql"
9898
local db = mysql:new()
@@ -144,15 +144,15 @@ result: (?:{"insert_id":0,"server_status":2,"warning_count":[01],"affected_rows"
144144
return
145145
end
146146
147-
ngx.say("result: ", cjson.encode(res))
147+
ngx.say("result: ", ljson.encode(res))
148148
149149
res, err, errno, sqlstate = db:query("select name, id from cats order by id desc")
150150
if not res then
151151
ngx.say("bad result: ", err, ": ", errno, ": ", sqlstate, ".")
152152
return
153153
end
154154
155-
ngx.say("result: ", cjson.encode(res))
155+
ngx.say("result: ", ljson.encode(res))
156156
157157
local ok, err = db:close()
158158
if not ok then
@@ -180,7 +180,7 @@ result: [[null,"3"],["","2"],["Bob","1"]]
180180
--- config
181181
location /t {
182182
content_by_lua '
183-
local cjson = require "cjson"
183+
local ljson = require "ljson"
184184
185185
local mysql = require "resty.mysql"
186186
local db = mysql:new()
@@ -224,15 +224,15 @@ result: [[null,"3"],["","2"],["Bob","1"]]
224224
return
225225
end
226226
227-
ngx.say("result: ", cjson.encode(res))
227+
ngx.say("result: ", ljson.encode(res))
228228
229229
res, err, errno, sqlstate = db:query("select * from cats order by id desc")
230230
if not res then
231231
ngx.say("bad result: ", err, ": ", errno, ": ", sqlstate, ".")
232232
return
233233
end
234234
235-
ngx.say("result: ", cjson.encode(res))
235+
ngx.say("result: ", ljson.encode(res))
236236
237237
local ok, err = db:close()
238238
if not ok then
@@ -247,8 +247,8 @@ GET /t
247247
connected to mysql.
248248
table cats dropped.
249249
table cats created.
250-
result: {}
251-
result: {}
250+
result: []
251+
result: []
252252
--- no_error_log
253253
[error]
254254

@@ -259,7 +259,7 @@ result: {}
259259
--- config
260260
location /t {
261261
content_by_lua '
262-
local cjson = require "cjson"
262+
local ljson = require "ljson"
263263
264264
local mysql = require "resty.mysql"
265265
local db = mysql:new()
@@ -312,7 +312,7 @@ result: {}
312312
return
313313
end
314314
315-
ngx.say("result: ", cjson.encode(res))
315+
ngx.say("result: ", ljson.encode(res))
316316
317317
db:set_compact_arrays(false)
318318
@@ -322,7 +322,7 @@ result: {}
322322
return
323323
end
324324
325-
ngx.say("result: ", cjson.encode(res))
325+
ngx.say("result: ", ljson.encode(res))
326326
327327
local ok, err = db:close()
328328
if not ok then
@@ -339,7 +339,7 @@ table cats dropped.
339339
table cats created.
340340
3 rows inserted into table cats (last id: 1)
341341
result: [["Bob","1"],["","2"],[null,"3"]]
342-
result: [{"name":null,"id":"3"},{"name":"","id":"2"},{"name":"Bob","id":"1"}]
342+
result: [{"id":"3","name":null},{"id":"2","name":""},{"id":"1","name":"Bob"}]
343343
--- no_error_log
344344
[error]
345345

t/lib/ljson.lua

Lines changed: 89 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
1+
local ngx_null = ngx.null
2+
local tostring = tostring
3+
local byte = string.byte
4+
local gsub = string.gsub
5+
local sort = table.sort
6+
local pairs = pairs
7+
local ipairs = ipairs
8+
local concat = table.concat
9+
10+
local ok, new_tab = pcall(require, "table.new")
11+
if not ok then
12+
new_tab = function (narr, nrec) return {} end
13+
end
14+
15+
local _M = {}
16+
17+
local metachars = {
18+
['\t'] = '\\t',
19+
["\\"] = "\\\\",
20+
['"'] = '\\"',
21+
['\r'] = '\\r',
22+
['\n'] = '\\n',
23+
}
24+
25+
local function encode_str(s)
26+
-- XXX we will rewrite this when string.buffer is implemented
27+
-- in LuaJIT 2.1 because string.gsub cannot be JIT compiled.
28+
return gsub(s, '["\\\r\n\t]', metachars)
29+
end
30+
31+
local function is_arr(t)
32+
local exp = 1
33+
for k, _ in pairs(t) do
34+
if k ~= exp then
35+
return nil
36+
end
37+
exp = exp + 1
38+
end
39+
return exp - 1
40+
end
41+
42+
local encode
43+
44+
encode = function (v)
45+
if v == nil or v == ngx_null then
46+
return "null"
47+
end
48+
49+
local typ = type(v)
50+
if typ == 'string' then
51+
return '"' .. encode_str(v) .. '"'
52+
end
53+
54+
if typ == 'number' or typ == 'boolean' then
55+
return tostring(v)
56+
end
57+
58+
if typ == 'table' then
59+
local n = is_arr(v)
60+
if n then
61+
local bits = new_tab(n, 0)
62+
for i, elem in ipairs(v) do
63+
bits[i] = encode(elem)
64+
end
65+
return "[" .. concat(bits, ",") .. "]"
66+
end
67+
68+
local keys = {}
69+
local i = 0
70+
for key, _ in pairs(v) do
71+
i = i + 1
72+
keys[i] = key
73+
end
74+
sort(keys)
75+
76+
local bits = new_tab(0, i)
77+
i = 0
78+
for _, key in ipairs(keys) do
79+
i = i + 1
80+
bits[i] = encode(key) .. ":" .. encode(v[key])
81+
end
82+
return "{" .. concat(bits, ",") .. "}"
83+
end
84+
85+
return '"<' .. typ .. '>"'
86+
end
87+
_M.encode = encode
88+
89+
return _M

0 commit comments

Comments
 (0)