aboutsummaryrefslogtreecommitdiff
path: root/tests/sort_json.lua
diff options
context:
space:
mode:
authorleaf corcoran <leafot@gmail.com>2019-09-22 13:21:45 -0700
committerleaf <leafot@gmail.com>2020-04-25 14:24:18 -0700
commit461c7ef23a49062d4b1bf0e1afb3be294d007861 (patch)
tree36b4e13561dd53d40bd87676c9aaf3fb5f1eea74 /tests/sort_json.lua
parentdbcfb92e12e9016060d1907a2556cc61e82f467d (diff)
downloadlua-cjson-461c7ef23a49062d4b1bf0e1afb3be294d007861.tar.gz
lua-cjson-461c7ef23a49062d4b1bf0e1afb3be294d007861.tar.bz2
lua-cjson-461c7ef23a49062d4b1bf0e1afb3be294d007861.zip
write sort_json, use it on perl tests to prevent hash table ordering failures
Diffstat (limited to '')
-rw-r--r--tests/sort_json.lua61
1 files changed, 61 insertions, 0 deletions
diff --git a/tests/sort_json.lua b/tests/sort_json.lua
new file mode 100644
index 0000000..c95ab3c
--- /dev/null
+++ b/tests/sort_json.lua
@@ -0,0 +1,61 @@
1-- NOTE: This will only work for simple tests. It doesn't parse strings so if
2-- you put any symbols like {?[], inside of a string literal then it will break
3-- The point of this function is to test basic structures, and not test JSON
4-- strings
5
6local function sort_callback(str)
7 local inside = str:sub(2, -2)
8
9 local parts = {}
10 local buffer = ""
11 local pos = 1
12
13 while true do
14 if pos > #inside then
15 break
16 end
17
18 local append
19
20 local parens = inside:match("^%b{}", pos)
21 if parens then
22 pos = pos + #parens
23 append = sort_callback(parens)
24 else
25 local array = inside:match("^%b[]", pos)
26 if array then
27 pos = pos + #array
28 append = array
29 else
30 local front = inside:sub(pos, pos)
31 pos = pos + 1
32
33 if front == "," then
34 table.insert(parts, buffer)
35 buffer = ""
36 else
37 append = front
38 end
39 end
40 end
41
42 if append then
43 buffer = buffer .. append
44 end
45 end
46
47 if buffer ~= "" then
48 table.insert(parts, buffer)
49 end
50
51 table.sort(parts)
52
53 return "{" .. table.concat(parts, ",") .. "}"
54end
55
56local function sort_json(str)
57 return (str:gsub("%b{}", sort_callback))
58end
59
60
61return sort_json