Skip to content

Commit 2e56476

Browse files
committed
Update json.lua from pending pull request
rxi/json.lua#51
1 parent 3462373 commit 2e56476

File tree

1 file changed

+18
-2
lines changed

1 file changed

+18
-2
lines changed

src/resources/pandoc/datadir/_json.lua

+18-2
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
-- https://github.com/rxi/json.lua
66
--
77
-- includes unreleased upstream changes: https://github.com/rxi/json.lua/blob/dbf4b2dd2eb7c23be2773c89eb059dadd6436f94/json.lua
8+
-- includes unmerged upstream pull request: https://github.com/rxi/json.lua/pull/51
89
--
910
-- Permission is hereby granted, free of charge, to any person obtaining a copy of
1011
-- this software and associated documentation files (the "Software"), to deal in
@@ -27,6 +28,21 @@
2728

2829
local json = { _version = "0.1.2-quarto" }
2930

31+
-- taken from https://www.lua.org/pil/19.3.html
32+
function pairsByKeys (t, f)
33+
local a = {}
34+
for n in pairs(t) do table.insert(a, n) end
35+
table.sort(a, f)
36+
local i = 0 -- iterator variable
37+
local iter = function () -- iterator function
38+
i = i + 1
39+
if a[i] == nil then return nil
40+
else return a[i], t[a[i]]
41+
end
42+
end
43+
return iter
44+
end
45+
3046
-------------------------------------------------------------------------------
3147
-- Encode
3248
-------------------------------------------------------------------------------
@@ -89,7 +105,7 @@ local function encode_table(val, stack)
89105

90106
else
91107
-- Treat as an object
92-
for k, v in pairs(val) do
108+
for k, v in pairsByKeys(val) do
93109
if type(k) ~= "string" then
94110
error("invalid table: mixed or invalid key types")
95111
end
@@ -388,4 +404,4 @@ function json.decode(str)
388404
end
389405

390406

391-
return json
407+
return json

0 commit comments

Comments
 (0)