json.lua 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380
  1. --
  2. -- json.lua
  3. --
  4. -- Copyright (c) 2015 rxi
  5. --
  6. -- This library is free software; you can redistribute it and/or modify it
  7. -- under the terms of the MIT license. See LICENSE for details.
  8. --
  9. local json = { _version = "0.1.0" }
  10. -------------------------------------------------------------------------------
  11. -- Encode
  12. -------------------------------------------------------------------------------
  13. local encode
  14. local escape_char_map = {
  15. [ "\\" ] = "\\\\",
  16. [ "\"" ] = "\\\"",
  17. [ "\b" ] = "\\b",
  18. [ "\f" ] = "\\f",
  19. [ "\n" ] = "\\n",
  20. [ "\r" ] = "\\r",
  21. [ "\t" ] = "\\t",
  22. }
  23. local escape_char_map_inv = { [ "\\/" ] = "/" }
  24. for k, v in pairs(escape_char_map) do
  25. escape_char_map_inv[v] = k
  26. end
  27. local function escape_char(c)
  28. return escape_char_map[c] or string.format("\\u%04x", c:byte())
  29. end
  30. local function encode_nil(val)
  31. return "null"
  32. end
  33. local function encode_table(val, stack)
  34. local res = {}
  35. stack = stack or {}
  36. -- Circular reference?
  37. if stack[val] then error("circular reference") end
  38. stack[val] = true
  39. if val[1] ~= nil or next(val) == nil then
  40. -- Treat as array -- check keys are valid and it is not sparse
  41. local n = 0
  42. for k in pairs(val) do
  43. if type(k) ~= "number" then
  44. error("invalid table: mixed or invalid key types")
  45. end
  46. n = n + 1
  47. end
  48. if n ~= #val then
  49. error("invalid table: sparse array")
  50. end
  51. -- Encode
  52. for i, v in ipairs(val) do
  53. table.insert(res, encode(v, stack))
  54. end
  55. stack[val] = nil
  56. return "[" .. table.concat(res, ",") .. "]"
  57. else
  58. -- Treat as an object
  59. for k, v in pairs(val) do
  60. if type(k) ~= "string" then
  61. error("invalid table: mixed or invalid key types")
  62. end
  63. table.insert(res, encode(k, stack) .. ":" .. encode(v, stack))
  64. end
  65. stack[val] = nil
  66. return "{" .. table.concat(res, ",") .. "}"
  67. end
  68. end
  69. local function encode_string(val)
  70. return '"' .. val:gsub('[%z\1-\31\\"]', escape_char) .. '"'
  71. end
  72. local function encode_number(val)
  73. -- Check for NaN, -inf and inf
  74. if val ~= val or val <= -math.huge or val >= math.huge then
  75. error("unexpected number value '" .. tostring(val) .. "'")
  76. end
  77. return string.format("%.14g", val)
  78. end
  79. local type_func_map = {
  80. [ "nil" ] = encode_nil,
  81. [ "table" ] = encode_table,
  82. [ "string" ] = encode_string,
  83. [ "number" ] = encode_number,
  84. [ "boolean" ] = tostring,
  85. }
  86. encode = function(val, stack)
  87. local t = type(val)
  88. local f = type_func_map[t]
  89. if f then
  90. return f(val, stack)
  91. end
  92. error("unexpected type '" .. t .. "'")
  93. end
  94. function json.encode(val)
  95. return ( encode(val) )
  96. end
  97. -------------------------------------------------------------------------------
  98. -- Decode
  99. -------------------------------------------------------------------------------
  100. local parse
  101. local function create_set(...)
  102. local res = {}
  103. for i = 1, select("#", ...) do
  104. res[ select(i, ...) ] = true
  105. end
  106. return res
  107. end
  108. local space_chars = create_set(" ", "\t", "\r", "\n")
  109. local delim_chars = create_set(" ", "\t", "\r", "\n", "]", "}", ",")
  110. local escape_chars = create_set("\\", "/", '"', "b", "f", "n", "r", "t", "u")
  111. local literals = create_set("true", "false", "null")
  112. local literal_map = {
  113. [ "true" ] = true,
  114. [ "false" ] = false,
  115. [ "null" ] = nil,
  116. }
  117. local function next_char(str, idx, set, negate)
  118. for i = idx, #str do
  119. if set[str:sub(i, i)] ~= negate then
  120. return i
  121. end
  122. end
  123. return #str + 1
  124. end
  125. local function decode_error(str, idx, msg)
  126. local line_count = 1
  127. local col_count = 1
  128. for i = 1, idx - 1 do
  129. col_count = col_count + 1
  130. if str:sub(i, i) == "\n" then
  131. line_count = line_count + 1
  132. col_count = 1
  133. end
  134. end
  135. error( string.format("%s at line %d col %d", msg, line_count, col_count) )
  136. end
  137. local function codepoint_to_utf8(n)
  138. -- http://scripts.sil.org/cms/scripts/page.php?site_id=nrsi&id=iws-appendixa
  139. local f = math.floor
  140. if n <= 0x7f then
  141. return string.char(n)
  142. elseif n <= 0x7ff then
  143. return string.char(f(n / 64) + 192, n % 64 + 128)
  144. elseif n <= 0xffff then
  145. return string.char(f(n / 4096) + 224, f(n % 4096 / 64) + 128, n % 64 + 128)
  146. elseif n <= 0x10ffff then
  147. return string.char(f(n / 262144) + 240, f(n % 262144 / 4096) + 128,
  148. f(n % 4096 / 64) + 128, n % 64 + 128)
  149. end
  150. error( string.format("invalid unicode codepoint '%x'", n) )
  151. end
  152. local function parse_unicode_escape(s)
  153. local n1 = tonumber( s:sub(3, 6), 16 )
  154. local n2 = tonumber( s:sub(9, 12), 16 )
  155. -- Surrogate pair?
  156. if n2 then
  157. return codepoint_to_utf8((n1 - 0xd800) * 0x400 + (n2 - 0xdc00) + 0x10000)
  158. else
  159. return codepoint_to_utf8(n1)
  160. end
  161. end
  162. local function parse_string(str, i)
  163. local has_unicode_escape = false
  164. local has_surrogate_escape = false
  165. local has_escape = false
  166. local last
  167. for j = i + 1, #str do
  168. local x = str:byte(j)
  169. if x < 32 then
  170. decode_error(str, j, "control character in string")
  171. end
  172. if last == 92 then -- "\\" (escape char)
  173. if x == 117 then -- "u" (unicode escape sequence)
  174. local hex = str:sub(j + 1, j + 5)
  175. if not hex:find("%x%x%x%x") then
  176. decode_error(str, j, "invalid unicode escape in string")
  177. end
  178. if hex:find("^[dD][89aAbB]") then
  179. has_surrogate_escape = true
  180. else
  181. has_unicode_escape = true
  182. end
  183. else
  184. local c = string.char(x)
  185. if not escape_chars[c] then
  186. decode_error(str, j, "invalid escape char '" .. c .. "' in string")
  187. end
  188. has_escape = true
  189. end
  190. last = nil
  191. elseif x == 34 then -- '"' (end of string)
  192. local s = str:sub(i + 1, j - 1)
  193. if has_surrogate_escape then
  194. s = s:gsub("\\u[dD][89aAbB]..\\u....", parse_unicode_escape)
  195. end
  196. if has_unicode_escape then
  197. s = s:gsub("\\u....", parse_unicode_escape)
  198. end
  199. if has_escape then
  200. s = s:gsub("\\.", escape_char_map_inv)
  201. end
  202. return s, j + 1
  203. else
  204. last = x
  205. end
  206. end
  207. decode_error(str, i, "expected closing quote for string")
  208. end
  209. local function parse_number(str, i)
  210. local x = next_char(str, i, delim_chars)
  211. local s = str:sub(i, x - 1)
  212. local n = tonumber(s)
  213. if not n then
  214. decode_error(str, i, "invalid number '" .. s .. "'")
  215. end
  216. return n, x
  217. end
  218. local function parse_literal(str, i)
  219. local x = next_char(str, i, delim_chars)
  220. local word = str:sub(i, x - 1)
  221. if not literals[word] then
  222. decode_error(str, i, "invalid literal '" .. word .. "'")
  223. end
  224. return literal_map[word], x
  225. end
  226. local function parse_array(str, i)
  227. local res = {}
  228. local n = 1
  229. i = i + 1
  230. while 1 do
  231. local x
  232. i = next_char(str, i, space_chars, true)
  233. -- Empty / end of array?
  234. if str:sub(i, i) == "]" then
  235. i = i + 1
  236. break
  237. end
  238. -- Read token
  239. x, i = parse(str, i)
  240. res[n] = x
  241. n = n + 1
  242. -- Next token
  243. i = next_char(str, i, space_chars, true)
  244. local chr = str:sub(i, i)
  245. i = i + 1
  246. if chr == "]" then break end
  247. if chr ~= "," then decode_error(str, i, "expected ']' or ','") end
  248. end
  249. return res, i
  250. end
  251. local function parse_object(str, i)
  252. local res = {}
  253. i = i + 1
  254. while 1 do
  255. local key, val
  256. i = next_char(str, i, space_chars, true)
  257. -- Empty / end of object?
  258. if str:sub(i, i) == "}" then
  259. i = i + 1
  260. break
  261. end
  262. -- Read key
  263. if str:sub(i, i) ~= '"' then
  264. decode_error(str, i, "expected string for key")
  265. end
  266. key, i = parse(str, i)
  267. -- Read ':' delimiter
  268. i = next_char(str, i, space_chars, true)
  269. if str:sub(i, i) ~= ":" then
  270. decode_error(str, i, "expected ':' after key")
  271. end
  272. i = next_char(str, i + 1, space_chars, true)
  273. -- Read value
  274. val, i = parse(str, i)
  275. -- Set
  276. res[key] = val
  277. -- Next token
  278. i = next_char(str, i, space_chars, true)
  279. local chr = str:sub(i, i)
  280. i = i + 1
  281. if chr == "}" then break end
  282. if chr ~= "," then decode_error(str, i, "expected '}' or ','") end
  283. end
  284. return res, i
  285. end
  286. local char_func_map = {
  287. [ '"' ] = parse_string,
  288. [ "0" ] = parse_number,
  289. [ "1" ] = parse_number,
  290. [ "2" ] = parse_number,
  291. [ "3" ] = parse_number,
  292. [ "4" ] = parse_number,
  293. [ "5" ] = parse_number,
  294. [ "6" ] = parse_number,
  295. [ "7" ] = parse_number,
  296. [ "8" ] = parse_number,
  297. [ "9" ] = parse_number,
  298. [ "-" ] = parse_number,
  299. [ "t" ] = parse_literal,
  300. [ "f" ] = parse_literal,
  301. [ "n" ] = parse_literal,
  302. [ "[" ] = parse_array,
  303. [ "{" ] = parse_object,
  304. }
  305. parse = function(str, idx)
  306. local chr = str:sub(idx, idx)
  307. local f = char_func_map[chr]
  308. if f then
  309. return f(str, idx)
  310. end
  311. decode_error(str, idx, "unexpected character '" .. chr .. "'")
  312. end
  313. function json.decode(str)
  314. if type(str) ~= "string" then
  315. error("expected argument of type string, got " .. type(str))
  316. end
  317. return ( parse(str, next_char(str, 1, space_chars, true)) )
  318. end
  319. return json