@@ -50,8 +50,8 @@ local function kind_of(obj)
5050end
5151
5252local function escape_str (s )
53- local in_char = {' \\ ' , ' "' , ' /' , ' \b ' , ' \f ' , ' \n ' , ' \r ' , ' \t ' }
54- local out_char = {' \\ ' , ' "' , ' /' , ' b' , ' f' , ' n' , ' r' , ' t' }
53+ local in_char = { ' \\ ' , ' "' , ' /' , ' \b ' , ' \f ' , ' \n ' , ' \r ' , ' \t ' }
54+ local out_char = { ' \\ ' , ' "' , ' /' , ' b' , ' f' , ' n' , ' r' , ' t' }
5555 for i , c in ipairs (in_char ) do
5656 s = s :gsub (c , ' \\ ' .. out_char [i ])
5757 end
@@ -80,10 +80,10 @@ local function parse_str_val(str, pos, val)
8080 local early_end_error = ' End of input found while parsing string.'
8181 if pos > # str then error (early_end_error ) end
8282 local c = str :sub (pos , pos )
83- if c == ' "' then return val , pos + 1 end
83+ if c == ' "' then return val , pos + 1 end
8484 if c ~= ' \\ ' then return parse_str_val (str , pos + 1 , val .. c ) end
8585 -- We must have a \ character.
86- local esc_map = {b = ' \b ' , f = ' \f ' , n = ' \n ' , r = ' \r ' , t = ' \t ' }
86+ local esc_map = { b = ' \b ' , f = ' \f ' , n = ' \n ' , r = ' \r ' , t = ' \t ' }
8787 local nextc = str :sub (pos + 1 , pos + 1 )
8888 if not nextc then error (early_end_error ) end
8989 return parse_str_val (str , pos + 2 , val .. (esc_map [nextc ] or nextc ))
101101-- Public values and functions.
102102
103103function json .stringify (obj , as_key )
104- local s = {} -- We'll build the string as an array of strings to be concatenated.
105- local kind = kind_of (obj ) -- This is 'array' if it's an array or type(obj) otherwise.
104+ local s = {} -- We'll build the string as an array of strings to be concatenated.
105+ local kind = kind_of (obj ) -- This is 'array' if it's an array or type(obj) otherwise.
106106 if kind == ' array' then
107107 if as_key then error (' Can\' t encode array as key.' ) end
108108 s [# s + 1 ] = ' ['
@@ -136,25 +136,25 @@ function json.stringify(obj, as_key)
136136 return table.concat (s )
137137end
138138
139- json .null = {} -- This is a one-off table to represent the null value.
139+ json .null = {} -- This is a one-off table to represent the null value.
140140
141141function json .parse (str , pos , end_delim )
142142 pos = pos or 1
143143 if pos > # str then error (' Reached unexpected end of input.' ) end
144- local pos = pos + # str :match (' ^%s*' , pos ) -- Skip whitespace.
144+ local pos = pos + # str :match (' ^%s*' , pos ) -- Skip whitespace.
145145 local first = str :sub (pos , pos )
146- if first == ' {' then -- Parse an object.
146+ if first == ' {' then -- Parse an object.
147147 local obj , key , delim_found = {}, true , true
148148 pos = pos + 1
149149 while true do
150150 key , pos = json .parse (str , pos , ' }' )
151151 if key == nil then return obj , pos end
152152 if not delim_found then error (' Comma missing between object items.' ) end
153- pos = skip_delim (str , pos , ' :' , true ) -- true -> error if missing.
153+ pos = skip_delim (str , pos , ' :' , true ) -- true -> error if missing.
154154 obj [key ], pos = json .parse (str , pos )
155155 pos , delim_found = skip_delim (str , pos , ' ,' )
156156 end
157- elseif first == ' [' then -- Parse an array.
157+ elseif first == ' [' then -- Parse an array.
158158 local arr , val , delim_found = {}, true , true
159159 pos = pos + 1
160160 while true do
@@ -164,14 +164,14 @@ function json.parse(str, pos, end_delim)
164164 arr [# arr + 1 ] = val
165165 pos , delim_found = skip_delim (str , pos , ' ,' )
166166 end
167- elseif first == ' "' then -- Parse a string.
167+ elseif first == ' "' then -- Parse a string.
168168 return parse_str_val (str , pos + 1 )
169- elseif first == ' -' or first :match (' %d' ) then -- Parse a number.
169+ elseif first == ' -' or first :match (' %d' ) then -- Parse a number.
170170 return parse_num_val (str , pos )
171- elseif first == end_delim then -- End of an object or array.
171+ elseif first == end_delim then -- End of an object or array.
172172 return nil , pos + 1
173- else -- Parse true, false, or null.
174- local literals = {[' true' ] = true , [' false' ] = false , [' null' ] = json .null }
173+ else -- Parse true, false, or null.
174+ local literals = { [' true' ] = true , [' false' ] = false , [' null' ] = json .null }
175175 for lit_str , lit_val in pairs (literals ) do
176176 local lit_end = pos + # lit_str - 1
177177 if str :sub (pos , lit_end ) == lit_str then return lit_val , lit_end + 1 end
@@ -181,4 +181,4 @@ function json.parse(str, pos, end_delim)
181181 end
182182end
183183
184- return json
184+ return json
0 commit comments