Skip to content

Commit fdf6813

Browse files
author
LocalIdentity
committed
Update dkjson to export sorted json
JIT 2.1 changed pairs so that it now randomises the export order of the JSON object This sorts it in case we need it to export the tree like we do in PoB 2
1 parent af2f181 commit fdf6813

File tree

1 file changed

+24
-1
lines changed

1 file changed

+24
-1
lines changed

runtime/lua/dkjson.lua

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -130,6 +130,26 @@ local function escapeutf8 (uchar)
130130
end
131131
end
132132

133+
local function sortedkeys(tbl)
134+
local keys = {}
135+
for k in pairs(tbl) do
136+
keys[#keys + 1] = k
137+
end
138+
table.sort(keys, function(a, b)
139+
local ta, tb = type(a), type(b)
140+
if ta == tb then
141+
return a < b
142+
end
143+
if ta == "number" then
144+
return true
145+
elseif tb == "number" then
146+
return false
147+
end
148+
return ta < tb
149+
end)
150+
return keys
151+
end
152+
133153
local function fsub (str, pattern, repl)
134154
-- gsub always builds a new string in a buffer, even when no match
135155
-- exists. First using find should be more efficient when most strings
@@ -333,7 +353,10 @@ encode2 = function (value, indent, level, buffer, buflen, tables, globalorder, s
333353
end
334354
end
335355
else -- unordered
336-
for k,v in pairs (value) do
356+
local keys = sortedkeys(value)
357+
for i = 1, #keys do
358+
local k = keys[i]
359+
local v = value[k]
337360
buflen, msg = addpair (k, v, prev, indent, level, buffer, buflen, tables, globalorder, state)
338361
if not buflen then return nil, msg end
339362
prev = true -- add a seperator before the next element

0 commit comments

Comments
 (0)