Skip to content

Commit 98eb1d0

Browse files
committed
Make sparse array test immune to table hash order
The table hash function was changed in Lua 5.3 which swapped the JSON object output order for the sparse array test. Ignore ordering altogether by checking the JSON output type instead.
1 parent db12267 commit 98eb1d0

File tree

1 file changed

+13
-2
lines changed

1 file changed

+13
-2
lines changed

tests/test.lua

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,17 @@ local json = require "cjson"
1010
local json_safe = require "cjson.safe"
1111
local util = require "cjson.util"
1212

13+
local function json_encode_output_type(value)
14+
local text = json.encode(value)
15+
if string.match(text, "{.*}") then
16+
return "object"
17+
elseif string.match(text, "%[.*%]") then
18+
return "array"
19+
else
20+
return "scalar"
21+
end
22+
end
23+
1324
local function gen_raw_octets()
1425
local chars = {}
1526
for i = 0, 255 do chars[i + 1] = string.char(i) end
@@ -292,8 +303,8 @@ local cjson_tests = {
292303
json.encode, { { [1] = "one", [4] = "sparse test" } },
293304
true, { '["one",null,null,"sparse test"]' } },
294305
{ "Encode sparse array as object",
295-
json.encode, { { [1] = "one", [5] = "sparse test" } },
296-
true, { '{"1":"one","5":"sparse test"}' } },
306+
json_encode_output_type, { { [1] = "one", [5] = "sparse test" } },
307+
true, { 'object' } },
297308
{ "Encode table with numeric string key as object",
298309
json.encode, { { ["2"] = "numeric string key test" } },
299310
true, { '{"2":"numeric string key test"}' } },

0 commit comments

Comments
 (0)