Skip to content

Instantly share code, notes, and snippets.

@muhmuhten
Created November 23, 2018 03:15
Show Gist options
  • Save muhmuhten/11d52269daa208b47eb4f675092fa236 to your computer and use it in GitHub Desktop.
Save muhmuhten/11d52269daa208b47eb4f675092fa236 to your computer and use it in GitHub Desktop.
diff --git a/decoder.lua b/decoder.lua
index 4478894..2e40aed 100644
--- a/decoder.lua
+++ b/decoder.lua
@@ -403,10 +403,12 @@ local function newdecoder()
if rec_depth > 1000 then
decode_error('too deeply nested json (> 1000)')
end
- local obj = {}
+ local obj = {[0]=false} -- distinguish empty arrays from objects
f, pos = find(json, '^[ \n\r\t]*', pos)
pos = pos+1
+
+ local i = 0
if byte(json, pos) ~= 0x7D then -- check closing bracket '}' which means the object empty
local newpos = pos-1
@@ -441,7 +443,8 @@ local function newdecoder()
f = dispatcher[byte(json, newpos+1)]
end
pos = newpos+2
- obj[key] = f() -- parse value
+ i = i+2
+ obj[i-1], obj[i] = key, f() -- parse value
f, newpos = find(json, '^[ \n\r\t]*,[ \n\r\t]*', pos)
until not newpos
@@ -452,6 +455,12 @@ local function newdecoder()
pos = newpos
end
+ -- Second pass for kv mapping is empirically faster than doing it
+ -- alongside the pair assignments in the inner loop
+ for j=1,i,2 do
+ obj[obj[j]] = obj[j+1]
+ end
+
pos = pos+1
rec_depth = rec_depth - 1
return obj
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment