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
2728
2829local 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)
388404end
389405
390406
391- return json
407+ return json
0 commit comments