Skip to content

Commit 3462373

Browse files
committed
1 parent 08f63e6 commit 3462373

File tree

1 file changed

+35
-43
lines changed

1 file changed

+35
-43
lines changed

src/resources/pandoc/datadir/_json.lua

Lines changed: 35 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,7 @@
44
-- Copyright (c) 2020 rxi
55
-- https://github.com/rxi/json.lua
66
--
7-
-- 2023-02-08: Modified by RStudio, PBC to make encoding more robust under the
8-
-- following example: encode(decode("[null, 'test']"))
7+
-- includes unreleased upstream changes: https://github.com/rxi/json.lua/blob/dbf4b2dd2eb7c23be2773c89eb059dadd6436f94/json.lua
98
--
109
-- Permission is hereby granted, free of charge, to any person obtaining a copy of
1110
-- this software and associated documentation files (the "Software"), to deal in
@@ -26,7 +25,7 @@
2625
-- SOFTWARE.
2726
--
2827

29-
local json = { _version = "0.1.2" }
28+
local json = { _version = "0.1.2-quarto" }
3029

3130
-------------------------------------------------------------------------------
3231
-- Encode
@@ -59,9 +58,6 @@ local function encode_nil(val)
5958
return "null"
6059
end
6160

62-
local function encode_string(val)
63-
return '"' .. val:gsub('[%z\1-\31\\"]', escape_char) .. '"'
64-
end
6561

6662
local function encode_table(val, stack)
6763
local res = {}
@@ -72,45 +68,44 @@ local function encode_table(val, stack)
7268

7369
stack[val] = true
7470

75-
local n = 0
76-
local types = {}
77-
78-
for k in pairs(val) do
79-
types[type(k)] = true
80-
end
81-
82-
if #types > 1 then
83-
error("invalid table: mixed or invalid key types")
84-
elseif types["number"] then
85-
-- Treat as array
86-
local max_key = 0
71+
if rawget(val, 1) ~= nil or next(val) == nil then
72+
-- Treat as array -- check keys are valid and it is not sparse
73+
local n = 0
8774
for k in pairs(val) do
88-
if k > max_key then
89-
max_key = k
75+
if type(k) ~= "number" then
76+
error("invalid table: mixed or invalid key types")
9077
end
78+
n = n + 1
9179
end
92-
for i = 1, max_key do
93-
if val[i] == nil then
94-
table.insert(res, "null")
95-
else
96-
local v = encode(val[i], stack)
97-
table.insert(res, v)
98-
end
80+
if n ~= #val then
81+
error("invalid table: sparse array")
82+
end
83+
-- Encode
84+
for i, v in ipairs(val) do
85+
table.insert(res, encode(v, stack))
9986
end
10087
stack[val] = nil
10188
return "[" .. table.concat(res, ",") .. "]"
102-
elseif types["string"] then
103-
-- Treat as object
104-
for k, v in _quarto.utils.table.sortedPairs(val) do
105-
table.insert(res, encode_string(k) .. ":" .. encode(v, stack))
89+
90+
else
91+
-- Treat as an object
92+
for k, v in pairs(val) do
93+
if type(k) ~= "string" then
94+
error("invalid table: mixed or invalid key types")
95+
end
96+
table.insert(res, encode(k, stack) .. ":" .. encode(v, stack))
10697
end
10798
stack[val] = nil
10899
return "{" .. table.concat(res, ",") .. "}"
109-
else
110-
return "[]"
111100
end
112101
end
113102

103+
104+
local function encode_string(val)
105+
return '"' .. val:gsub('[%z\1-\31\\"]', escape_char) .. '"'
106+
end
107+
108+
114109
local function encode_number(val)
115110
-- Check for NaN, -inf and inf
116111
if val ~= val or val <= -math.huge or val >= math.huge then
@@ -139,7 +134,7 @@ encode = function(val, stack)
139134
end
140135

141136

142-
local function jsonEncode(val)
137+
function json.encode(val)
143138
return ( encode(val) )
144139
end
145140

@@ -205,7 +200,7 @@ local function codepoint_to_utf8(n)
205200
return string.char(f(n / 4096) + 224, f(n % 4096 / 64) + 128, n % 64 + 128)
206201
elseif n <= 0x10ffff then
207202
return string.char(f(n / 262144) + 240, f(n % 262144 / 4096) + 128,
208-
f(n % 4096 / 64) + 128, n % 64 + 128)
203+
f(n % 4096 / 64) + 128, n % 64 + 128)
209204
end
210205
error( string.format("invalid unicode codepoint '%x'", n) )
211206
end
@@ -214,7 +209,7 @@ end
214209
local function parse_unicode_escape(s)
215210
local n1 = tonumber( s:sub(1, 4), 16 )
216211
local n2 = tonumber( s:sub(7, 10), 16 )
217-
-- Surrogate pair?
212+
-- Surrogate pair?
218213
if n2 then
219214
return codepoint_to_utf8((n1 - 0xd800) * 0x400 + (n2 - 0xdc00) + 0x10000)
220215
else
@@ -240,8 +235,8 @@ local function parse_string(str, i)
240235
local c = str:sub(j, j)
241236
if c == "u" then
242237
local hex = str:match("^[dD][89aAbB]%x%x\\u%x%x%x%x", j + 1)
243-
or str:match("^%x%x%x%x", j + 1)
244-
or decode_error(str, j - 1, "invalid unicode escape in string")
238+
or str:match("^%x%x%x%x", j + 1)
239+
or decode_error(str, j - 1, "invalid unicode escape in string")
245240
res = res .. parse_unicode_escape(hex)
246241
j = j + #hex
247242
else
@@ -380,7 +375,7 @@ parse = function(str, idx)
380375
end
381376

382377

383-
local function jsonDecode(str)
378+
function json.decode(str)
384379
if type(str) ~= "string" then
385380
error("expected argument of type string, got " .. type(str))
386381
end
@@ -393,7 +388,4 @@ local function jsonDecode(str)
393388
end
394389

395390

396-
return {
397-
encode = jsonEncode,
398-
decode = jsonDecode
399-
}
391+
return json

0 commit comments

Comments
 (0)