Skip to content

Commit 4d70f52

Browse files
committed
Update json.lua from pending pull request
rxi/json.lua#51
1 parent c6386c5 commit 4d70f52

File tree

1 file changed

+17
-2
lines changed

1 file changed

+17
-2
lines changed

src/resources/pandoc/datadir/_json.lua

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,21 @@
2424

2525
local json = { _version = "0.1.2" }
2626

27+
-- taken from https://www.lua.org/pil/19.3.html
28+
function pairsByKeys (t, f)
29+
local a = {}
30+
for n in pairs(t) do table.insert(a, n) end
31+
table.sort(a, f)
32+
local i = 0 -- iterator variable
33+
local iter = function () -- iterator function
34+
i = i + 1
35+
if a[i] == nil then return nil
36+
else return a[i], t[a[i]]
37+
end
38+
end
39+
return iter
40+
end
41+
2742
-------------------------------------------------------------------------------
2843
-- Encode
2944
-------------------------------------------------------------------------------
@@ -86,7 +101,7 @@ local function encode_table(val, stack)
86101

87102
else
88103
-- Treat as an object
89-
for k, v in pairs(val) do
104+
for k, v in pairsByKeys(val) do
90105
if type(k) ~= "string" then
91106
error("invalid table: mixed or invalid key types")
92107
end
@@ -385,4 +400,4 @@ function json.decode(str)
385400
end
386401

387402

388-
return json
403+
return json

0 commit comments

Comments
 (0)