4
4
-- Copyright (c) 2020 rxi
5
5
-- https://github.com/rxi/json.lua
6
6
--
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
9
8
--
10
9
-- Permission is hereby granted, free of charge, to any person obtaining a copy of
11
10
-- this software and associated documentation files (the "Software"), to deal in
26
25
-- SOFTWARE.
27
26
--
28
27
29
- local json = { _version = " 0.1.2" }
28
+ local json = { _version = " 0.1.2-quarto " }
30
29
31
30
---- ---------------------------------------------------------------------------
32
31
-- Encode
@@ -59,9 +58,6 @@ local function encode_nil(val)
59
58
return " null"
60
59
end
61
60
62
- local function encode_string (val )
63
- return ' "' .. val :gsub (' [%z\1 -\31 \\ "]' , escape_char ) .. ' "'
64
- end
65
61
66
62
local function encode_table (val , stack )
67
63
local res = {}
@@ -72,45 +68,44 @@ local function encode_table(val, stack)
72
68
73
69
stack [val ] = true
74
70
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
87
74
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 " )
90
77
end
78
+ n = n + 1
91
79
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 ))
99
86
end
100
87
stack [val ] = nil
101
88
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 ))
106
97
end
107
98
stack [val ] = nil
108
99
return " {" .. table.concat (res , " ," ) .. " }"
109
- else
110
- return " []"
111
100
end
112
101
end
113
102
103
+
104
+ local function encode_string (val )
105
+ return ' "' .. val :gsub (' [%z\1 -\31 \\ "]' , escape_char ) .. ' "'
106
+ end
107
+
108
+
114
109
local function encode_number (val )
115
110
-- Check for NaN, -inf and inf
116
111
if val ~= val or val <= - math.huge or val >= math.huge then
@@ -139,7 +134,7 @@ encode = function(val, stack)
139
134
end
140
135
141
136
142
- local function jsonEncode (val )
137
+ function json . encode (val )
143
138
return ( encode (val ) )
144
139
end
145
140
@@ -205,7 +200,7 @@ local function codepoint_to_utf8(n)
205
200
return string.char (f (n / 4096 ) + 224 , f (n % 4096 / 64 ) + 128 , n % 64 + 128 )
206
201
elseif n <= 0x10ffff then
207
202
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 )
209
204
end
210
205
error ( string.format (" invalid unicode codepoint '%x'" , n ) )
211
206
end
214
209
local function parse_unicode_escape (s )
215
210
local n1 = tonumber ( s :sub (1 , 4 ), 16 )
216
211
local n2 = tonumber ( s :sub (7 , 10 ), 16 )
217
- -- Surrogate pair?
212
+ -- Surrogate pair?
218
213
if n2 then
219
214
return codepoint_to_utf8 ((n1 - 0xd800 ) * 0x400 + (n2 - 0xdc00 ) + 0x10000 )
220
215
else
@@ -240,8 +235,8 @@ local function parse_string(str, i)
240
235
local c = str :sub (j , j )
241
236
if c == " u" then
242
237
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" )
245
240
res = res .. parse_unicode_escape (hex )
246
241
j = j + # hex
247
242
else
@@ -380,7 +375,7 @@ parse = function(str, idx)
380
375
end
381
376
382
377
383
- local function jsonDecode (str )
378
+ function json . decode (str )
384
379
if type (str ) ~= " string" then
385
380
error (" expected argument of type string, got " .. type (str ))
386
381
end
@@ -393,7 +388,4 @@ local function jsonDecode(str)
393
388
end
394
389
395
390
396
- return {
397
- encode = jsonEncode ,
398
- decode = jsonDecode
399
- }
391
+ return json
0 commit comments