Skip to content

Commit 620edb8

Browse files
committed
Fixes & New functionality
* Merged exponential and initial decimal number pattern into one. Probably will incur performance hit. But necessary to support exponential numbers. * Allow passing an array as the initial value to parse into json. Even though I don't believe this is right, rxi/json.lua supports it. And it won't really incur any performance hits, so why not. * Use string.format's `%q` for encoding strings, as to not break with escaped quotes.
1 parent abc357a commit 620edb8

File tree

2 files changed

+6
-7
lines changed

2 files changed

+6
-7
lines changed

qjson.lua

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -43,10 +43,9 @@ local function decode(json --[[@param json string]]) ---@return table
4343
local function value()
4444
return faststring()
4545
or object()
46-
or tonumber(consume("^%s*(%-?%d+%.%d+)") or consume("^%s*(%-?%d+)"))
46+
or tonumber(consume("^%s*(%-?%d+%.?%d*[eE][%+%-]?%d+)") or consume("^%s*(%-?%d+%.?%d*)"))
4747
or consume("^%s*(true)") or consume("^%s*(false)") or consume("^%s*(null)")
4848
or array()
49-
or tonumber(consume("^%s*(%-?%d+[eE][%+%-]?%d+)")) -- Exponential number.
5049
or slowstring()
5150
end
5251

@@ -99,10 +98,10 @@ local function decode(json --[[@param json string]]) ---@return table
9998
end
10099
end
101100

102-
return object()
101+
return object() or array()
103102
end
104103

105-
local concat, tostring, pairs = table.concat, tostring, pairs
104+
local concat, tostring, format, pairs = table.concat, tostring, string.format, pairs
106105
local function isarray(t)
107106
local i = 1
108107
for k in pairs(t) do
@@ -120,7 +119,7 @@ local function value(v)
120119
if t == "table" then
121120
return encode(v)
122121
elseif t == "string" then
123-
return "\"" .. v .. "\""
122+
return format("%q", v)
124123
else
125124
return tostring(v)
126125
end

qjson_simple.lua

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ local function decode(json --[[@param json string]]) ---@return table
5050
return table()
5151
end
5252

53-
local concat, tostring, pairs = table.concat, tostring, pairs
53+
local concat, tostring, format, pairs = table.concat, tostring, string.format, pairs
5454
local function isarray(t)
5555
local i = 1
5656
for k in pairs(t) do
@@ -68,7 +68,7 @@ local function value(v)
6868
if t == "table" then
6969
return encode(v)
7070
elseif t == "string" then
71-
return "\"" .. v .. "\""
71+
return format("%q", v)
7272
else
7373
return tostring(v)
7474
end

0 commit comments

Comments
 (0)