Advertisement
Guest User

Untitled

a guest
Aug 2nd, 2015
183
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 16.59 KB | None | 0 0
  1. -----------------------------------------------------------------------------
  2. -- JSON4Lua: JSON encoding / decoding support for the Lua language.
  3. -- json Module.
  4. -- Author: Craig Mason-Jones
  5. -- Homepage: http://github.com/craigmj/json4lua/
  6. -- Version: 1.0.0
  7. -- This module is released under the MIT License (MIT).
  8. -- Please see LICENCE.txt for details.
  9. --
  10. -- USAGE:
  11. -- This module exposes two functions:
  12. -- json.encode(o)
  13. -- Returns the table / string / boolean / number / nil / json.null value as a JSON-encoded string.
  14. -- json.decode(json_string)
  15. -- Returns a Lua object populated with the data encoded in the JSON string json_string.
  16. --
  17. -- REQUIREMENTS:
  18. -- compat-5.1 if using Lua 5.0
  19. --
  20. -- CHANGELOG
  21. -- 0.9.20 Introduction of local Lua functions for private functions (removed _ function prefix).
  22. -- Fixed Lua 5.1 compatibility issues.
  23. -- Introduced json.null to have null values in associative arrays.
  24. -- json.encode() performance improvement (more than 50%) through table.concat rather than ..
  25. -- Introduced decode ability to ignore /**/ comments in the JSON string.
  26. -- 0.9.10 Fix to array encoding / decoding to correctly manage nil/null values in arrays.
  27. -----------------------------------------------------------------------------
  28.  
  29. -----------------------------------------------------------------------------
  30. -- Imports and dependencies
  31. -----------------------------------------------------------------------------
  32. local math = require('math')
  33. local string = require("string")
  34. local table = require("table")
  35.  
  36. -----------------------------------------------------------------------------
  37. -- Module declaration
  38. -----------------------------------------------------------------------------
  39. local json = {} -- Public namespace
  40. local json_private = {} -- Private namespace
  41.  
  42. -- Public functions
  43.  
  44. -- Private functions
  45. local decode_scanArray
  46. local decode_scanComment
  47. local decode_scanConstant
  48. local decode_scanNumber
  49. local decode_scanObject
  50. local decode_scanString
  51. local decode_scanWhitespace
  52. local encodeString
  53. local isArray
  54. local isEncodable
  55.  
  56. -----------------------------------------------------------------------------
  57. -- PUBLIC FUNCTIONS
  58. -----------------------------------------------------------------------------
  59. --- Encodes an arbitrary Lua object / variable.
  60. -- @param v The Lua object / variable to be JSON encoded.
  61. -- @return String containing the JSON encoding in internal Lua string format (i.e. not unicode)
  62. function json.encode (v)
  63. -- Handle nil values
  64. if v==nil then
  65. return "null"
  66. end
  67.  
  68. local vtype = type(v)
  69.  
  70. -- Handle strings
  71. if vtype=='string' then
  72. return '"' .. json_private.encodeString(v) .. '"' -- Need to handle encoding in string
  73. end
  74.  
  75. -- Handle booleans
  76. if vtype=='number' or vtype=='boolean' then
  77. return tostring(v)
  78. end
  79.  
  80. -- Handle tables
  81. if vtype=='table' then
  82. local rval = {}
  83. -- Consider arrays separately
  84. local bArray, maxCount = isArray(v)
  85. if bArray then
  86. for i = 1,maxCount do
  87. table.insert(rval, json.encode(v[i]))
  88. end
  89. else -- An object, not an array
  90. for i,j in pairs(v) do
  91. if isEncodable(i) and isEncodable(j) then
  92. table.insert(rval, '"' .. json_private.encodeString(i) .. '":' .. json.encode(j))
  93. end
  94. end
  95. end
  96. if bArray then
  97. return '[' .. table.concat(rval,',') ..']'
  98. else
  99. return '{' .. table.concat(rval,',') .. '}'
  100. end
  101. end
  102.  
  103. -- Handle null values
  104. if vtype=='function' and v==null then
  105. return 'null'
  106. end
  107.  
  108. assert(false,'encode attempt to encode unsupported type ' .. vtype .. ':' .. tostring(v))
  109. end
  110.  
  111.  
  112. --- Decodes a JSON string and returns the decoded value as a Lua data structure / value.
  113. -- @param s The string to scan.
  114. -- @param [startPos] Optional starting position where the JSON string is located. Defaults to 1.
  115. -- @param Lua object, number The object that was scanned, as a Lua table / string / number / boolean or nil,
  116. -- and the position of the first character after
  117. -- the scanned JSON object.
  118. function json.decode(s, startPos)
  119. startPos = startPos and startPos or 1
  120. startPos = decode_scanWhitespace(s,startPos)
  121. assert(startPos<=string.len(s), 'Unterminated JSON encoded object found at position in [' .. s .. ']')
  122. local curChar = string.sub(s,startPos,startPos)
  123. -- Object
  124. if curChar=='{' then
  125. return decode_scanObject(s,startPos)
  126. end
  127. -- Array
  128. if curChar=='[' then
  129. return decode_scanArray(s,startPos)
  130. end
  131. -- Number
  132. if string.find("+-0123456789.e", curChar, 1, true) then
  133. return decode_scanNumber(s,startPos)
  134. end
  135. -- String
  136. if curChar==[["]] or curChar==[[']] then
  137. return decode_scanString(s,startPos)
  138. end
  139. if string.sub(s,startPos,startPos+1)=='/*' then
  140. return decode(s, decode_scanComment(s,startPos))
  141. end
  142. -- Otherwise, it must be a constant
  143. return decode_scanConstant(s,startPos)
  144. end
  145.  
  146. --- The null function allows one to specify a null value in an associative array (which is otherwise
  147. -- discarded if you set the value with 'nil' in Lua. Simply set t = { first=json.null }
  148. function null()
  149. return null -- so json.null() will also return null ;-)
  150. end
  151. -----------------------------------------------------------------------------
  152. -- Internal, PRIVATE functions.
  153. -- Following a Python-like convention, I have prefixed all these 'PRIVATE'
  154. -- functions with an underscore.
  155. -----------------------------------------------------------------------------
  156.  
  157. --- Scans an array from JSON into a Lua object
  158. -- startPos begins at the start of the array.
  159. -- Returns the array and the next starting position
  160. -- @param s The string being scanned.
  161. -- @param startPos The starting position for the scan.
  162. -- @return table, int The scanned array as a table, and the position of the next character to scan.
  163. function decode_scanArray(s,startPos)
  164. local array = {} -- The return value
  165. local stringLen = string.len(s)
  166. assert(string.sub(s,startPos,startPos)=='[','decode_scanArray called but array does not start at position ' .. startPos .. ' in string:\n'..s )
  167. startPos = startPos + 1
  168. -- Infinite loop for array elements
  169. repeat
  170. startPos = decode_scanWhitespace(s,startPos)
  171. assert(startPos<=stringLen,'JSON String ended unexpectedly scanning array.')
  172. local curChar = string.sub(s,startPos,startPos)
  173. if (curChar==']') then
  174. return array, startPos+1
  175. end
  176. if (curChar==',') then
  177. startPos = decode_scanWhitespace(s,startPos+1)
  178. end
  179. assert(startPos<=stringLen, 'JSON String ended unexpectedly scanning array.')
  180. object, startPos = json.decode(s,startPos)
  181. table.insert(array,object)
  182. until false
  183. end
  184.  
  185. --- Scans a comment and discards the comment.
  186. -- Returns the position of the next character following the comment.
  187. -- @param string s The JSON string to scan.
  188. -- @param int startPos The starting position of the comment
  189. function decode_scanComment(s, startPos)
  190. assert( string.sub(s,startPos,startPos+1)=='/*', "decode_scanComment called but comment does not start at position " .. startPos)
  191. local endPos = string.find(s,'*/',startPos+2)
  192. assert(endPos~=nil, "Unterminated comment in string at " .. startPos)
  193. return endPos+2
  194. end
  195.  
  196. --- Scans for given constants: true, false or null
  197. -- Returns the appropriate Lua type, and the position of the next character to read.
  198. -- @param s The string being scanned.
  199. -- @param startPos The position in the string at which to start scanning.
  200. -- @return object, int The object (true, false or nil) and the position at which the next character should be
  201. -- scanned.
  202. function decode_scanConstant(s, startPos)
  203. local consts = { ["true"] = true, ["false"] = false, ["null"] = nil }
  204. local constNames = {"true","false","null"}
  205.  
  206. for i,k in pairs(constNames) do
  207. if string.sub(s,startPos, startPos + string.len(k) -1 )==k then
  208. return consts[k], startPos + string.len(k)
  209. end
  210. end
  211. assert(nil, 'Failed to scan constant from string ' .. s .. ' at starting position ' .. startPos)
  212. end
  213.  
  214. --- Scans a number from the JSON encoded string.
  215. -- (in fact, also is able to scan numeric +- eqns, which is not
  216. -- in the JSON spec.)
  217. -- Returns the number, and the position of the next character
  218. -- after the number.
  219. -- @param s The string being scanned.
  220. -- @param startPos The position at which to start scanning.
  221. -- @return number, int The extracted number and the position of the next character to scan.
  222. function decode_scanNumber(s,startPos)
  223. local endPos = startPos+1
  224. local stringLen = string.len(s)
  225. local acceptableChars = "+-0123456789.e"
  226. while (string.find(acceptableChars, string.sub(s,endPos,endPos), 1, true)
  227. and endPos<=stringLen
  228. ) do
  229. endPos = endPos + 1
  230. end
  231. local stringValue = 'return ' .. string.sub(s,startPos, endPos-1)
  232. local stringEval = loadstring(stringValue)
  233. assert(stringEval, 'Failed to scan number [ ' .. stringValue .. '] in JSON string at position ' .. startPos .. ' : ' .. endPos)
  234. return stringEval(), endPos
  235. end
  236.  
  237. --- Scans a JSON object into a Lua object.
  238. -- startPos begins at the start of the object.
  239. -- Returns the object and the next starting position.
  240. -- @param s The string being scanned.
  241. -- @param startPos The starting position of the scan.
  242. -- @return table, int The scanned object as a table and the position of the next character to scan.
  243. function decode_scanObject(s,startPos)
  244. local object = {}
  245. local stringLen = string.len(s)
  246. local key, value
  247. assert(string.sub(s,startPos,startPos)=='{','decode_scanObject called but object does not start at position ' .. startPos .. ' in string:\n' .. s)
  248. startPos = startPos + 1
  249. repeat
  250. startPos = decode_scanWhitespace(s,startPos)
  251. assert(startPos<=stringLen, 'JSON string ended unexpectedly while scanning object.')
  252. local curChar = string.sub(s,startPos,startPos)
  253. if (curChar=='}') then
  254. return object,startPos+1
  255. end
  256. if (curChar==',') then
  257. startPos = decode_scanWhitespace(s,startPos+1)
  258. end
  259. assert(startPos<=stringLen, 'JSON string ended unexpectedly scanning object.')
  260. -- Scan the key
  261. key, startPos = json.decode(s,startPos)
  262. assert(startPos<=stringLen, 'JSON string ended unexpectedly searching for value of key ' .. key)
  263. startPos = decode_scanWhitespace(s,startPos)
  264. assert(startPos<=stringLen, 'JSON string ended unexpectedly searching for value of key ' .. key)
  265. assert(string.sub(s,startPos,startPos)==':','JSON object key-value assignment mal-formed at ' .. startPos)
  266. startPos = decode_scanWhitespace(s,startPos+1)
  267. assert(startPos<=stringLen, 'JSON string ended unexpectedly searching for value of key ' .. key)
  268. value, startPos = json.decode(s,startPos)
  269. object[key]=value
  270. until false -- infinite loop while key-value pairs are found
  271. end
  272.  
  273. -- START SoniEx2
  274. -- Initialize some things used by decode_scanString
  275. -- You know, for efficiency
  276. local escapeSequences = {
  277. ["\\t"] = "\t",
  278. ["\\f"] = "\f",
  279. ["\\r"] = "\r",
  280. ["\\n"] = "\n",
  281. ["\\b"] = "\b"
  282. }
  283. setmetatable(escapeSequences, {__index = function(t,k)
  284. -- skip "\" aka strip escape
  285. return string.sub(k,2)
  286. end})
  287. -- END SoniEx2
  288.  
  289. --- Scans a JSON string from the opening inverted comma or single quote to the
  290. -- end of the string.
  291. -- Returns the string extracted as a Lua string,
  292. -- and the position of the next non-string character
  293. -- (after the closing inverted comma or single quote).
  294. -- @param s The string being scanned.
  295. -- @param startPos The starting position of the scan.
  296. -- @return string, int The extracted string as a Lua string, and the next character to parse.
  297. function decode_scanString(s,startPos)
  298. assert(startPos, 'decode_scanString(..) called without start position')
  299. local startChar = string.sub(s,startPos,startPos)
  300. -- START SoniEx2
  301. -- PS: I don't think single quotes are valid JSON
  302. assert(startChar == [["]] or startChar == [[']],'decode_scanString called for a non-string')
  303. --assert(startPos, "String decoding failed: missing closing " .. startChar .. " for string at position " .. oldStart)
  304. local t = {}
  305. local i,j = startPos,startPos
  306. while string.find(s, startChar, j+1) ~= j+1 do
  307. local oldj = j
  308. i,j = string.find(s, "\\.", j+1)
  309. local x,y = string.find(s, startChar, oldj+1)
  310. if not i or x < i then
  311. i,j = x,y-1
  312. end
  313. table.insert(t, string.sub(s, oldj+1, i-1))
  314. if string.sub(s, i, j) == "\\u" then
  315. local a = string.sub(s,j+1,j+4)
  316. j = j + 4
  317. local n = tonumber(a, 16)
  318. assert(n, "String decoding failed: bad Unicode escape " .. a .. " at position " .. i .. " : " .. j)
  319. -- math.floor(x/2^y) == lazy right shift
  320. -- a % 2^b == bitwise_and(a, (2^b)-1)
  321. -- 64 = 2^6
  322. -- 4096 = 2^12 (or 2^6 * 2^6)
  323. local x
  324. if n < 0x80 then
  325. x = string.char(n % 0x80)
  326. elseif n < 0x800 then
  327. -- [110x xxxx] [10xx xxxx]
  328. x = string.char(0xC0 + (math.floor(n/64) % 0x20), 0x80 + (n % 0x40))
  329. else
  330. -- [1110 xxxx] [10xx xxxx] [10xx xxxx]
  331. x = string.char(0xE0 + (math.floor(n/4096) % 0x10), 0x80 + (math.floor(n/64) % 0x40), 0x80 + (n % 0x40))
  332. end
  333. table.insert(t, x)
  334. else
  335. table.insert(t, escapeSequences[string.sub(s, i, j)])
  336. end
  337. end
  338. table.insert(t,string.sub(j, j+1))
  339. assert(string.find(s, startChar, j+1), "String decoding failed: missing closing " .. startChar .. " at position " .. j .. "(for string at position " .. startPos .. ")")
  340. return table.concat(t,""), j+2
  341. -- END SoniEx2
  342. end
  343.  
  344. --- Scans a JSON string skipping all whitespace from the current start position.
  345. -- Returns the position of the first non-whitespace character, or nil if the whole end of string is reached.
  346. -- @param s The string being scanned
  347. -- @param startPos The starting position where we should begin removing whitespace.
  348. -- @return int The first position where non-whitespace was encountered, or string.len(s)+1 if the end of string
  349. -- was reached.
  350. function decode_scanWhitespace(s,startPos)
  351. local whitespace=" \n\r\t"
  352. local stringLen = string.len(s)
  353. while ( string.find(whitespace, string.sub(s,startPos,startPos), 1, true) and startPos <= stringLen) do
  354. startPos = startPos + 1
  355. end
  356. return startPos
  357. end
  358.  
  359. --- Encodes a string to be JSON-compatible.
  360. -- This just involves back-quoting inverted commas, back-quotes and newlines, I think ;-)
  361. -- @param s The string to return as a JSON encoded (i.e. backquoted string)
  362. -- @return The string appropriately escaped.
  363.  
  364. local escapeList = {
  365. ['"'] = '\\"',
  366. ['\\'] = '\\\\',
  367. ['/'] = '\\/',
  368. ['\b'] = '\\b',
  369. ['\f'] = '\\f',
  370. ['\n'] = '\\n',
  371. ['\r'] = '\\r',
  372. ['\t'] = '\\t'
  373. }
  374.  
  375. function json_private.encodeString(s)
  376. local s = tostring(s)
  377. return s:gsub(".", function(c) return escapeList[c] end) -- SoniEx2: 5.0 compat
  378. end
  379.  
  380. -- Determines whether the given Lua type is an array or a table / dictionary.
  381. -- We consider any table an array if it has indexes 1..n for its n items, and no
  382. -- other data in the table.
  383. -- I think this method is currently a little 'flaky', but can't think of a good way around it yet...
  384. -- @param t The table to evaluate as an array
  385. -- @return boolean, number True if the table can be represented as an array, false otherwise. If true,
  386. -- the second returned value is the maximum
  387. -- number of indexed elements in the array.
  388. function isArray(t)
  389. -- Next we count all the elements, ensuring that any non-indexed elements are not-encodable
  390. -- (with the possible exception of 'n')
  391. local maxIndex = 0
  392. for k,v in pairs(t) do
  393. if (type(k)=='number' and math.floor(k)==k and 1<=k) then -- k,v is an indexed pair
  394. if (not isEncodable(v)) then return false end -- All array elements must be encodable
  395. maxIndex = math.max(maxIndex,k)
  396. else
  397. if (k=='n') then
  398. if v ~= table.getn(t) then return false end -- False if n does not hold the number of elements
  399. else -- Else of (k=='n')
  400. if isEncodable(v) then return false end
  401. end -- End of (k~='n')
  402. end -- End of k,v not an indexed pair
  403. end -- End of loop across all pairs
  404. return true, maxIndex
  405. end
  406.  
  407. --- Determines whether the given Lua object / table / variable can be JSON encoded. The only
  408. -- types that are JSON encodable are: string, boolean, number, nil, table and json.null.
  409. -- In this implementation, all other types are ignored.
  410. -- @param o The object to examine.
  411. -- @return boolean True if the object should be JSON encoded, false if it should be ignored.
  412. function isEncodable(o)
  413. local t = type(o)
  414. return (t=='string' or t=='boolean' or t=='number' or t=='nil' or t=='table') or (t=='function' and o==null)
  415. end
  416.  
  417. return json
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement