@@ -54,6 +54,7 @@ local decode_scanObject
54
54
local decode_scanString
55
55
local decode_scanWhitespace
56
56
local encodeString
57
+ local decodeString
57
58
local isArray
58
59
local isEncodable
59
60
@@ -141,6 +142,16 @@ function encodeString(s)
141
142
return tostring (s ):gsub (' ["\\\r\n\t ]' ,qrep )
142
143
end
143
144
145
+ --- Decodes a given JSON string back to a Lua string
146
+ -- The characters, escapable in JSON (see above function) are automatically
147
+ -- escaped by Lua. Nevertheless, the "/" (slash) character CAN be escaped in
148
+ -- JSON but CANNOT be escaped in Lua.
149
+ -- @param s The string to return as proper Lua string
150
+ -- @return A JSON encoded string
151
+ function decodeString (s )
152
+ return (base .load (" return " .. tostring (s ):gsub (" \\ /" , " /" ))) ()
153
+ end
154
+
144
155
-- Determines whether the given Lua type is an array or a table / dictionary.
145
156
-- We consider any table an array if it has indexes 1..n for its n items, and no
146
157
-- other data in the table.
253
264
-- escapechar[i] = "\\" .. string.char(i)
254
265
end
255
266
267
+ charstounescape = " \"\'\\ bfnrt/"
256
268
--[[
257
- charstounescape = "\"\'\\bfnrt/";
258
269
unescapechars = "\"'\\\b\f\n\r\t\/";
259
270
for i=1,#charstounescape do
260
271
escapechar[ charstounescape:byte(i) ] = unescapechars:sub(i,i)
383
394
if t == c_esc then
384
395
-- table.insert(returnString, js_string:sub(start, pos-2))
385
396
-- table.insert(returnString, escapechar[ js_string:byte(pos) ])
397
+ local c = js_string :sub (pos , pos )
398
+ if (not charstounescape :find (c )) then
399
+ error (" invalid escape sequence '\\ " .. c .. " ' (" .. location ().. " )" )
400
+ end
386
401
pos = pos + 1
387
402
-- start = pos
388
403
end -- jump over escaped chars, no matter what
389
404
until t == true
390
- return ( base . loadstring ( " return " .. js_string :sub (start - 1 , pos - 1 ) ) ( ))
405
+ return decodeString ( js_string :sub (start - 1 , pos - 1 ))
391
406
392
407
-- We consider the situation where no escaped chars were encountered separately,
393
408
-- and use the fastest possible return in this case.
0 commit comments