Skip to content

Commit 57b1048

Browse files
committed
Updated LuaJSON to version 1.2.1 - fixes incompatibility with LPEG 0.10
1 parent ebea1e9 commit 57b1048

File tree

13 files changed

+53
-31
lines changed

13 files changed

+53
-31
lines changed

lua/json/decode.lua

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ local util = require("json.decode.util")
1515
local setmetatable, getmetatable = setmetatable, getmetatable
1616
local assert = assert
1717
local ipairs, pairs = ipairs, pairs
18-
local string_char = string.char
18+
local string_char = require("string").char
1919

2020
local require = require
2121
module("json.decode")

lua/json/decode/array.lua

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ local lpeg = require("lpeg")
77
local util = require("json.decode.util")
88
local jsonutil = require("json.util")
99

10-
local table_maxn = table.maxn
10+
local table_maxn = require("table").maxn
1111

1212
local unpack = unpack
1313

lua/json/decode/number.lua

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,8 @@ local frac = lpeg.P('.') * digits
2020
local exp = lpeg.S("Ee") * (lpeg.S("-+") + 0) * digits
2121

2222
local nan = lpeg.S("Nn") * lpeg.S("Aa") * lpeg.S("Nn")
23-
local inf = (lpeg.P('-') + 0) * lpeg.S("Ii") * lpeg.P("nfinity")
23+
local inf = lpeg.S("Ii") * lpeg.P("nfinity")
24+
local ninf = lpeg.P('-') * lpeg.S("Ii") * lpeg.P("nfinity")
2425
local hex = (lpeg.P("0x") + lpeg.P("0X")) * lpeg.R("09","AF","af")^1
2526

2627
local defaultOptions = {
@@ -36,6 +37,11 @@ strict = {
3637
nan = false,
3738
inf = false
3839
}
40+
41+
local nan_value = 0/0
42+
local inf_value = 1/0
43+
local ninf_value = -1/0
44+
3945
--[[
4046
Options: configuration options for number rules
4147
nan: match NaN
@@ -44,7 +50,7 @@ strict = {
4450
exp: match exponent portion (e1)
4551
DEFAULT: nan, inf, frac, exp
4652
--]]
47-
local function buildMatch(options)
53+
local function buildCapture(options)
4854
options = options and merge({}, defaultOptions, options) or defaultOptions
4955
local ret = int
5056
if options.frac then
@@ -56,19 +62,17 @@ local function buildMatch(options)
5662
if options.hex then
5763
ret = hex + ret
5864
end
65+
-- Capture number now
66+
ret = ret / tonumber
5967
if options.nan then
60-
ret = ret + nan
68+
ret = ret + nan / function() return nan_value end
6169
end
6270
if options.inf then
63-
ret = ret + inf
71+
ret = ret + ninf / function() return ninf_value end + inf / function() return inf_value end
6472
end
6573
return ret
6674
end
6775

68-
local function buildCapture(options)
69-
return buildMatch(options) / tonumber
70-
end
71-
7276
function register_types()
7377
util.register_type("INTEGER")
7478
end

lua/json/decode/object.lua

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -14,13 +14,11 @@ local tostring = tostring
1414

1515
local rawset = rawset
1616

17-
local DecimalLpegVersion = util.DecimalLpegVersion
18-
1917
module("json.decode.object")
2018

2119
-- BEGIN LPEG < 0.9 SUPPORT
2220
local initObject, applyObjectKey
23-
if DecimalLpegVersion < 0.9 then
21+
if not (lpeg.Cg and lpeg.Cf and lpeg.Ct) then
2422
function initObject()
2523
return {}
2624
end
@@ -65,13 +63,22 @@ local function buildCapture(options, global_options)
6563
local objectItems
6664
local objectItem = (key * ignored * lpeg.P(":") * ignored * value_type)
6765
-- BEGIN LPEG < 0.9 SUPPORT
68-
if DecimalLpegVersion < 0.9 then
69-
objectItems = buildItemSequence(objectItem / applyObjectKey, ignored)
66+
if not (lpeg.Cg and lpeg.Cf and lpeg.Ct) then
67+
local set_key = applyObjectKey
68+
if options.setObjectKey then
69+
local setObjectKey = options.setObjectKey
70+
set_key = function(tab, key, val)
71+
setObjectKey(tab, key, val)
72+
return tab
73+
end
74+
end
75+
76+
objectItems = buildItemSequence(objectItem / set_key, ignored)
7077
objectItems = lpeg.Ca(lpeg.Cc(false) / initObject * objectItems)
7178
-- END LPEG < 0.9 SUPPORT
7279
else
7380
objectItems = buildItemSequence(lpeg.Cg(objectItem), ignored)
74-
objectItems = lpeg.Cf(lpeg.Ct(0) * objectItems, rawset)
81+
objectItems = lpeg.Cf(lpeg.Ct(0) * objectItems, options.setObjectKey or rawset)
7582
end
7683

7784

lua/json/decode/others.lua

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@ local lpeg = require("lpeg")
66
local jsonutil = require("json.util")
77
local util = require("json.decode.util")
88

9+
local rawset = rawset
10+
911
-- Container module for other JavaScript types (bool, null, undefined)
1012
module("json.decode.others")
1113

@@ -20,7 +22,8 @@ local undefinedCapture = lpeg.P("undefined")
2022
local defaultOptions = {
2123
allowUndefined = true,
2224
null = jsonutil.null,
23-
undefined = jsonutil.undefined
25+
undefined = jsonutil.undefined,
26+
setObjectKey = rawset
2427
}
2528

2629
default = nil -- Let the buildCapture optimization take place

lua/json/decode/strings.lua

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,9 @@ local util = require("json.decode.util")
77
local merge = require("json.util").merge
88

99
local tonumber = tonumber
10-
local string = string
11-
local string_char = string.char
12-
local floor = math.floor
13-
local table_concat = table.concat
10+
local string_char = require("string").char
11+
local floor = require("math").floor
12+
local table_concat = require("table").concat
1413

1514
local error = error
1615
module("json.decode.strings")

lua/json/decode/util.lua

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,8 @@ local lpeg = require("lpeg")
66
local select = select
77
local pairs, ipairs = pairs, ipairs
88
local tonumber = tonumber
9-
local string_char = string.char
9+
local string_char = require("string").char
10+
local rawset = rawset
1011

1112
local error = error
1213
local setmetatable = setmetatable
@@ -90,3 +91,8 @@ function get_invalid_character_info(input, index)
9091
local last_line = parsed:match("\n([^\n]+.)$") or parsed
9192
return line_number, #last_line, bad_character, last_line
9293
end
94+
95+
function setObjectKeyForceNumber(t, key, value)
96+
key = tonumber(key) or key
97+
return rawset(t, key, value)
98+
end

lua/json/encode/array.lua

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@ local type = type
88
local pairs = pairs
99
local assert = assert
1010

11+
local table = require("table")
12+
local math = require("math")
1113
local table_concat = table.concat
1214
local math_floor, math_modf = math.floor, math.modf
1315

lua/json/encode/calls.lua

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
--]]
55
local jsonutil = require("json.util")
66

7+
local table = require("table")
78
local table_concat = table.concat
89

910
local select = select

lua/json/encode/number.lua

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
local tostring = tostring
66
local assert = assert
77
local util = require("json.util")
8+
local huge = require("math").huge
89

910
module("json.encode.number")
1011

@@ -20,20 +21,19 @@ strict = {
2021
}
2122

2223
local function encodeNumber(number, options)
23-
local str = tostring(number)
24-
if str == "nan" then
24+
if number ~= number then
2525
assert(options.nan, "Invalid number: NaN not enabled")
2626
return "NaN"
2727
end
28-
if str == "inf" then
28+
if number == huge then
2929
assert(options.inf, "Invalid number: Infinity not enabled")
3030
return "Infinity"
3131
end
32-
if str == "-inf" then
32+
if number == -huge then
3333
assert(options.inf, "Invalid number: Infinity not enabled")
3434
return "-Infinity"
3535
end
36-
return str
36+
return tostring(number)
3737
end
3838

3939
function getEncoder(options)

0 commit comments

Comments
 (0)