5
5
-- https://github.com/rxi/json.lua
6
6
--
7
7
-- 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
8
9
--
9
10
-- Permission is hereby granted, free of charge, to any person obtaining a copy of
10
11
-- this software and associated documentation files (the "Software"), to deal in
27
28
28
29
local json = { _version = " 0.1.2-quarto" }
29
30
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
+
30
46
---- ---------------------------------------------------------------------------
31
47
-- Encode
32
48
---- ---------------------------------------------------------------------------
@@ -89,7 +105,7 @@ local function encode_table(val, stack)
89
105
90
106
else
91
107
-- Treat as an object
92
- for k , v in pairs (val ) do
108
+ for k , v in pairsByKeys (val ) do
93
109
if type (k ) ~= " string" then
94
110
error (" invalid table: mixed or invalid key types" )
95
111
end
@@ -388,4 +404,4 @@ function json.decode(str)
388
404
end
389
405
390
406
391
- return json
407
+ return json
0 commit comments