You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

1053 lines
34 KiB

  1. -- -*- coding: utf-8 -*-
  2. --
  3. -- Simple JSON encoding and decoding in pure Lua.
  4. --
  5. -- Copyright 2010-2014 Jeffrey Friedl
  6. -- http://regex.info/blog/
  7. --
  8. -- Latest version: http://regex.info/blog/lua/json
  9. --
  10. -- This code is released under a Creative Commons CC-BY "Attribution" License:
  11. -- http://creativecommons.org/licenses/by/3.0/deed.en_US
  12. --
  13. -- It can be used for any purpose so long as the copyright notice above,
  14. -- the web-page links above, and the 'AUTHOR_NOTE' string below are
  15. -- maintained. Enjoy.
  16. --
  17. local VERSION = 20141223.14 -- version history at end of file
  18. local AUTHOR_NOTE = "-[ JSON.lua package by Jeffrey Friedl (http://regex.info/blog/lua/json) version 20141223.14 ]-"
  19. --
  20. -- The 'AUTHOR_NOTE' variable exists so that information about the source
  21. -- of the package is maintained even in compiled versions. It's also
  22. -- included in OBJDEF below mostly to quiet warnings about unused variables.
  23. --
  24. local OBJDEF = {
  25. VERSION = VERSION,
  26. AUTHOR_NOTE = AUTHOR_NOTE,
  27. }
  28. --
  29. -- Simple JSON encoding and decoding in pure Lua.
  30. -- http://www.json.org/
  31. --
  32. --
  33. -- JSON = assert(loadfile "JSON.lua")() -- one-time load of the routines
  34. --
  35. -- local lua_value = JSON:decode(raw_json_text)
  36. --
  37. -- local raw_json_text = JSON:encode(lua_table_or_value)
  38. -- local pretty_json_text = JSON:encode_pretty(lua_table_or_value) -- "pretty printed" version for human readability
  39. --
  40. --
  41. --
  42. -- DECODING (from a JSON string to a Lua table)
  43. --
  44. --
  45. -- JSON = assert(loadfile "JSON.lua")() -- one-time load of the routines
  46. --
  47. -- local lua_value = JSON:decode(raw_json_text)
  48. --
  49. -- If the JSON text is for an object or an array, e.g.
  50. -- { "what": "books", "count": 3 }
  51. -- or
  52. -- [ "Larry", "Curly", "Moe" ]
  53. --
  54. -- the result is a Lua table, e.g.
  55. -- { what = "books", count = 3 }
  56. -- or
  57. -- { "Larry", "Curly", "Moe" }
  58. --
  59. --
  60. -- The encode and decode routines accept an optional second argument,
  61. -- "etc", which is not used during encoding or decoding, but upon error
  62. -- is passed along to error handlers. It can be of any type (including nil).
  63. --
  64. --
  65. --
  66. -- ERROR HANDLING
  67. --
  68. -- With most errors during decoding, this code calls
  69. --
  70. -- JSON:onDecodeError(message, text, location, etc)
  71. --
  72. -- with a message about the error, and if known, the JSON text being
  73. -- parsed and the byte count where the problem was discovered. You can
  74. -- replace the default JSON:onDecodeError() with your own function.
  75. --
  76. -- The default onDecodeError() merely augments the message with data
  77. -- about the text and the location if known (and if a second 'etc'
  78. -- argument had been provided to decode(), its value is tacked onto the
  79. -- message as well), and then calls JSON.assert(), which itself defaults
  80. -- to Lua's built-in assert(), and can also be overridden.
  81. --
  82. -- For example, in an Adobe Lightroom plugin, you might use something like
  83. --
  84. -- function JSON:onDecodeError(message, text, location, etc)
  85. -- LrErrors.throwUserError("Internal Error: invalid JSON data")
  86. -- end
  87. --
  88. -- or even just
  89. --
  90. -- function JSON.assert(message)
  91. -- LrErrors.throwUserError("Internal Error: " .. message)
  92. -- end
  93. --
  94. -- If JSON:decode() is passed a nil, this is called instead:
  95. --
  96. -- JSON:onDecodeOfNilError(message, nil, nil, etc)
  97. --
  98. -- and if JSON:decode() is passed HTML instead of JSON, this is called:
  99. --
  100. -- JSON:onDecodeOfHTMLError(message, text, nil, etc)
  101. --
  102. -- The use of the fourth 'etc' argument allows stronger coordination
  103. -- between decoding and error reporting, especially when you provide your
  104. -- own error-handling routines. Continuing with the the Adobe Lightroom
  105. -- plugin example:
  106. --
  107. -- function JSON:onDecodeError(message, text, location, etc)
  108. -- local note = "Internal Error: invalid JSON data"
  109. -- if type(etc) = 'table' and etc.photo then
  110. -- note = note .. " while processing for " .. etc.photo:getFormattedMetadata('fileName')
  111. -- end
  112. -- LrErrors.throwUserError(note)
  113. -- end
  114. --
  115. -- :
  116. -- :
  117. --
  118. -- for i, photo in ipairs(photosToProcess) do
  119. -- :
  120. -- :
  121. -- local data = JSON:decode(someJsonText, { photo = photo })
  122. -- :
  123. -- :
  124. -- end
  125. --
  126. --
  127. --
  128. --
  129. --
  130. -- DECODING AND STRICT TYPES
  131. --
  132. -- Because both JSON objects and JSON arrays are converted to Lua tables,
  133. -- it's not normally possible to tell which original JSON type a
  134. -- particular Lua table was derived from, or guarantee decode-encode
  135. -- round-trip equivalency.
  136. --
  137. -- However, if you enable strictTypes, e.g.
  138. --
  139. -- JSON = assert(loadfile "JSON.lua")() --load the routines
  140. -- JSON.strictTypes = true
  141. --
  142. -- then the Lua table resulting from the decoding of a JSON object or
  143. -- JSON array is marked via Lua metatable, so that when re-encoded with
  144. -- JSON:encode() it ends up as the appropriate JSON type.
  145. --
  146. -- (This is not the default because other routines may not work well with
  147. -- tables that have a metatable set, for example, Lightroom API calls.)
  148. --
  149. --
  150. -- ENCODING (from a lua table to a JSON string)
  151. --
  152. -- JSON = assert(loadfile "JSON.lua")() -- one-time load of the routines
  153. --
  154. -- local raw_json_text = JSON:encode(lua_table_or_value)
  155. -- local pretty_json_text = JSON:encode_pretty(lua_table_or_value) -- "pretty printed" version for human readability
  156. -- local custom_pretty = JSON:encode(lua_table_or_value, etc, { pretty = true, indent = "| ", align_keys = false })
  157. --
  158. -- On error during encoding, this code calls:
  159. --
  160. -- JSON:onEncodeError(message, etc)
  161. --
  162. -- which you can override in your local JSON object.
  163. --
  164. -- The 'etc' in the error call is the second argument to encode()
  165. -- and encode_pretty(), or nil if it wasn't provided.
  166. --
  167. --
  168. -- PRETTY-PRINTING
  169. --
  170. -- An optional third argument, a table of options, allows a bit of
  171. -- configuration about how the encoding takes place:
  172. --
  173. -- pretty = JSON:encode(val, etc, {
  174. -- pretty = true, -- if false, no other options matter
  175. -- indent = " ", -- this provides for a three-space indent per nesting level
  176. -- align_keys = false, -- see below
  177. -- })
  178. --
  179. -- encode() and encode_pretty() are identical except that encode_pretty()
  180. -- provides a default options table if none given in the call:
  181. --
  182. -- { pretty = true, align_keys = false, indent = " " }
  183. --
  184. -- For example, if
  185. --
  186. -- JSON:encode(data)
  187. --
  188. -- produces:
  189. --
  190. -- {"city":"Kyoto","climate":{"avg_temp":16,"humidity":"high","snowfall":"minimal"},"country":"Japan","wards":11}
  191. --
  192. -- then
  193. --
  194. -- JSON:encode_pretty(data)
  195. --
  196. -- produces:
  197. --
  198. -- {
  199. -- "city": "Kyoto",
  200. -- "climate": {
  201. -- "avg_temp": 16,
  202. -- "humidity": "high",
  203. -- "snowfall": "minimal"
  204. -- },
  205. -- "country": "Japan",
  206. -- "wards": 11
  207. -- }
  208. --
  209. -- The following three lines return identical results:
  210. -- JSON:encode_pretty(data)
  211. -- JSON:encode_pretty(data, nil, { pretty = true, align_keys = false, indent = " " })
  212. -- JSON:encode (data, nil, { pretty = true, align_keys = false, indent = " " })
  213. --
  214. -- An example of setting your own indent string:
  215. --
  216. -- JSON:encode_pretty(data, nil, { pretty = true, indent = "| " })
  217. --
  218. -- produces:
  219. --
  220. -- {
  221. -- | "city": "Kyoto",
  222. -- | "climate": {
  223. -- | | "avg_temp": 16,
  224. -- | | "humidity": "high",
  225. -- | | "snowfall": "minimal"
  226. -- | },
  227. -- | "country": "Japan",
  228. -- | "wards": 11
  229. -- }
  230. --
  231. -- An example of setting align_keys to true:
  232. --
  233. -- JSON:encode_pretty(data, nil, { pretty = true, indent = " ", align_keys = true })
  234. --
  235. -- produces:
  236. --
  237. -- {
  238. -- "city": "Kyoto",
  239. -- "climate": {
  240. -- "avg_temp": 16,
  241. -- "humidity": "high",
  242. -- "snowfall": "minimal"
  243. -- },
  244. -- "country": "Japan",
  245. -- "wards": 11
  246. -- }
  247. --
  248. -- which I must admit is kinda ugly, sorry. This was the default for
  249. -- encode_pretty() prior to version 20141223.14.
  250. --
  251. --
  252. -- AMBIGUOUS SITUATIONS DURING THE ENCODING
  253. --
  254. -- During the encode, if a Lua table being encoded contains both string
  255. -- and numeric keys, it fits neither JSON's idea of an object, nor its
  256. -- idea of an array. To get around this, when any string key exists (or
  257. -- when non-positive numeric keys exist), numeric keys are converted to
  258. -- strings.
  259. --
  260. -- For example,
  261. -- JSON:encode({ "one", "two", "three", SOMESTRING = "some string" }))
  262. -- produces the JSON object
  263. -- {"1":"one","2":"two","3":"three","SOMESTRING":"some string"}
  264. --
  265. -- To prohibit this conversion and instead make it an error condition, set
  266. -- JSON.noKeyConversion = true
  267. --
  268. --
  269. -- SUMMARY OF METHODS YOU CAN OVERRIDE IN YOUR LOCAL LUA JSON OBJECT
  270. --
  271. -- assert
  272. -- onDecodeError
  273. -- onDecodeOfNilError
  274. -- onDecodeOfHTMLError
  275. -- onEncodeError
  276. --
  277. -- If you want to create a separate Lua JSON object with its own error handlers,
  278. -- you can reload JSON.lua or use the :new() method.
  279. --
  280. ---------------------------------------------------------------------------
  281. local default_pretty_indent = " "
  282. local default_pretty_options = { pretty = true, align_keys = false, indent = default_pretty_indent }
  283. local isArray = { __tostring = function() return "JSON array" end } isArray.__index = isArray
  284. local isObject = { __tostring = function() return "JSON object" end } isObject.__index = isObject
  285. function OBJDEF:newArray(tbl)
  286. return setmetatable(tbl or {}, isArray)
  287. end
  288. function OBJDEF:newObject(tbl)
  289. return setmetatable(tbl or {}, isObject)
  290. end
  291. local function unicode_codepoint_as_utf8(codepoint)
  292. --
  293. -- codepoint is a number
  294. --
  295. if codepoint <= 127 then
  296. return string.char(codepoint)
  297. elseif codepoint <= 2047 then
  298. --
  299. -- 110yyyxx 10xxxxxx <-- useful notation from http://en.wikipedia.org/wiki/Utf8
  300. --
  301. local highpart = math.floor(codepoint / 0x40)
  302. local lowpart = codepoint - (0x40 * highpart)
  303. return string.char(0xC0 + highpart,
  304. 0x80 + lowpart)
  305. elseif codepoint <= 65535 then
  306. --
  307. -- 1110yyyy 10yyyyxx 10xxxxxx
  308. --
  309. local highpart = math.floor(codepoint / 0x1000)
  310. local remainder = codepoint - 0x1000 * highpart
  311. local midpart = math.floor(remainder / 0x40)
  312. local lowpart = remainder - 0x40 * midpart
  313. highpart = 0xE0 + highpart
  314. midpart = 0x80 + midpart
  315. lowpart = 0x80 + lowpart
  316. --
  317. -- Check for an invalid character (thanks Andy R. at Adobe).
  318. -- See table 3.7, page 93, in http://www.unicode.org/versions/Unicode5.2.0/ch03.pdf#G28070
  319. --
  320. if ( highpart == 0xE0 and midpart < 0xA0 ) or
  321. ( highpart == 0xED and midpart > 0x9F ) or
  322. ( highpart == 0xF0 and midpart < 0x90 ) or
  323. ( highpart == 0xF4 and midpart > 0x8F )
  324. then
  325. return "?"
  326. else
  327. return string.char(highpart,
  328. midpart,
  329. lowpart)
  330. end
  331. else
  332. --
  333. -- 11110zzz 10zzyyyy 10yyyyxx 10xxxxxx
  334. --
  335. local highpart = math.floor(codepoint / 0x40000)
  336. local remainder = codepoint - 0x40000 * highpart
  337. local midA = math.floor(remainder / 0x1000)
  338. remainder = remainder - 0x1000 * midA
  339. local midB = math.floor(remainder / 0x40)
  340. local lowpart = remainder - 0x40 * midB
  341. return string.char(0xF0 + highpart,
  342. 0x80 + midA,
  343. 0x80 + midB,
  344. 0x80 + lowpart)
  345. end
  346. end
  347. function OBJDEF:onDecodeError(message, text, location, etc)
  348. if text then
  349. if location then
  350. message = string.format("%s at char %d of: %s", message, location, text)
  351. else
  352. message = string.format("%s: %s", message, text)
  353. end
  354. end
  355. if etc ~= nil then
  356. message = message .. " (" .. OBJDEF:encode(etc) .. ")"
  357. end
  358. if self.assert then
  359. self.assert(false, message)
  360. else
  361. assert(false, message)
  362. end
  363. end
  364. OBJDEF.onDecodeOfNilError = OBJDEF.onDecodeError
  365. OBJDEF.onDecodeOfHTMLError = OBJDEF.onDecodeError
  366. function OBJDEF:onEncodeError(message, etc)
  367. if etc ~= nil then
  368. message = message .. " (" .. OBJDEF:encode(etc) .. ")"
  369. end
  370. if self.assert then
  371. self.assert(false, message)
  372. else
  373. assert(false, message)
  374. end
  375. end
  376. local function grok_number(self, text, start, etc)
  377. --
  378. -- Grab the integer part
  379. --
  380. local integer_part = text:match('^-?[1-9]%d*', start)
  381. or text:match("^-?0", start)
  382. if not integer_part then
  383. self:onDecodeError("expected number", text, start, etc)
  384. end
  385. local i = start + integer_part:len()
  386. --
  387. -- Grab an optional decimal part
  388. --
  389. local decimal_part = text:match('^%.%d+', i) or ""
  390. i = i + decimal_part:len()
  391. --
  392. -- Grab an optional exponential part
  393. --
  394. local exponent_part = text:match('^[eE][-+]?%d+', i) or ""
  395. i = i + exponent_part:len()
  396. local full_number_text = integer_part .. decimal_part .. exponent_part
  397. local as_number = tonumber(full_number_text)
  398. if not as_number then
  399. self:onDecodeError("bad number", text, start, etc)
  400. end
  401. return as_number, i
  402. end
  403. local function grok_string(self, text, start, etc)
  404. if text:sub(start,start) ~= '"' then
  405. self:onDecodeError("expected string's opening quote", text, start, etc)
  406. end
  407. local i = start + 1 -- +1 to bypass the initial quote
  408. local text_len = text:len()
  409. local VALUE = ""
  410. while i <= text_len do
  411. local c = text:sub(i,i)
  412. if c == '"' then
  413. return VALUE, i + 1
  414. end
  415. if c ~= '\\' then
  416. VALUE = VALUE .. c
  417. i = i + 1
  418. elseif text:match('^\\b', i) then
  419. VALUE = VALUE .. "\b"
  420. i = i + 2
  421. elseif text:match('^\\f', i) then
  422. VALUE = VALUE .. "\f"
  423. i = i + 2
  424. elseif text:match('^\\n', i) then
  425. VALUE = VALUE .. "\n"
  426. i = i + 2
  427. elseif text:match('^\\r', i) then
  428. VALUE = VALUE .. "\r"
  429. i = i + 2
  430. elseif text:match('^\\t', i) then
  431. VALUE = VALUE .. "\t"
  432. i = i + 2
  433. else
  434. local hex = text:match('^\\u([0123456789aAbBcCdDeEfF][0123456789aAbBcCdDeEfF][0123456789aAbBcCdDeEfF][0123456789aAbBcCdDeEfF])', i)
  435. if hex then
  436. i = i + 6 -- bypass what we just read
  437. -- We have a Unicode codepoint. It could be standalone, or if in the proper range and
  438. -- followed by another in a specific range, it'll be a two-code surrogate pair.
  439. local codepoint = tonumber(hex, 16)
  440. if codepoint >= 0xD800 and codepoint <= 0xDBFF then
  441. -- it's a hi surrogate... see whether we have a following low
  442. local lo_surrogate = text:match('^\\u([dD][cdefCDEF][0123456789aAbBcCdDeEfF][0123456789aAbBcCdDeEfF])', i)
  443. if lo_surrogate then
  444. i = i + 6 -- bypass the low surrogate we just read
  445. codepoint = 0x2400 + (codepoint - 0xD800) * 0x400 + tonumber(lo_surrogate, 16)
  446. else
  447. -- not a proper low, so we'll just leave the first codepoint as is and spit it out.
  448. end
  449. end
  450. VALUE = VALUE .. unicode_codepoint_as_utf8(codepoint)
  451. else
  452. -- just pass through what's escaped
  453. VALUE = VALUE .. text:match('^\\(.)', i)
  454. i = i + 2
  455. end
  456. end
  457. end
  458. self:onDecodeError("unclosed string", text, start, etc)
  459. end
  460. local function skip_whitespace(text, start)
  461. local _, match_end = text:find("^[ \n\r\t]+", start) -- [http://www.ietf.org/rfc/rfc4627.txt] Section 2
  462. if match_end then
  463. return match_end + 1
  464. else
  465. return start
  466. end
  467. end
  468. local grok_one -- assigned later
  469. local function grok_object(self, text, start, etc)
  470. if text:sub(start,start) ~= '{' then
  471. self:onDecodeError("expected '{'", text, start, etc)
  472. end
  473. local i = skip_whitespace(text, start + 1) -- +1 to skip the '{'
  474. local VALUE = self.strictTypes and self:newObject { } or { }
  475. if text:sub(i,i) == '}' then
  476. return VALUE, i + 1
  477. end
  478. local text_len = text:len()
  479. while i <= text_len do
  480. local key, new_i = grok_string(self, text, i, etc)
  481. i = skip_whitespace(text, new_i)
  482. if text:sub(i, i) ~= ':' then
  483. self:onDecodeError("expected colon", text, i, etc)
  484. end
  485. i = skip_whitespace(text, i + 1)
  486. local new_val, new_i = grok_one(self, text, i)
  487. VALUE[key] = new_val
  488. --
  489. -- Expect now either '}' to end things, or a ',' to allow us to continue.
  490. --
  491. i = skip_whitespace(text, new_i)
  492. local c = text:sub(i,i)
  493. if c == '}' then
  494. return VALUE, i + 1
  495. end
  496. if text:sub(i, i) ~= ',' then
  497. self:onDecodeError("expected comma or '}'", text, i, etc)
  498. end
  499. i = skip_whitespace(text, i + 1)
  500. end
  501. self:onDecodeError("unclosed '{'", text, start, etc)
  502. end
  503. local function grok_array(self, text, start, etc)
  504. if text:sub(start,start) ~= '[' then
  505. self:onDecodeError("expected '['", text, start, etc)
  506. end
  507. local i = skip_whitespace(text, start + 1) -- +1 to skip the '['
  508. local VALUE = self.strictTypes and self:newArray { } or { }
  509. if text:sub(i,i) == ']' then
  510. return VALUE, i + 1
  511. end
  512. local VALUE_INDEX = 1
  513. local text_len = text:len()
  514. while i <= text_len do
  515. local val, new_i = grok_one(self, text, i)
  516. -- can't table.insert(VALUE, val) here because it's a no-op if val is nil
  517. VALUE[VALUE_INDEX] = val
  518. VALUE_INDEX = VALUE_INDEX + 1
  519. i = skip_whitespace(text, new_i)
  520. --
  521. -- Expect now either ']' to end things, or a ',' to allow us to continue.
  522. --
  523. local c = text:sub(i,i)
  524. if c == ']' then
  525. return VALUE, i + 1
  526. end
  527. if text:sub(i, i) ~= ',' then
  528. self:onDecodeError("expected comma or '['", text, i, etc)
  529. end
  530. i = skip_whitespace(text, i + 1)
  531. end
  532. self:onDecodeError("unclosed '['", text, start, etc)
  533. end
  534. grok_one = function(self, text, start, etc)
  535. -- Skip any whitespace
  536. start = skip_whitespace(text, start)
  537. if start > text:len() then
  538. self:onDecodeError("unexpected end of string", text, nil, etc)
  539. end
  540. if text:find('^"', start) then
  541. return grok_string(self, text, start, etc)
  542. elseif text:find('^[-0123456789 ]', start) then
  543. return grok_number(self, text, start, etc)
  544. elseif text:find('^%{', start) then
  545. return grok_object(self, text, start, etc)
  546. elseif text:find('^%[', start) then
  547. return grok_array(self, text, start, etc)
  548. elseif text:find('^true', start) then
  549. return true, start + 4
  550. elseif text:find('^false', start) then
  551. return false, start + 5
  552. elseif text:find('^null', start) then
  553. return nil, start + 4
  554. else
  555. self:onDecodeError("can't parse JSON", text, start, etc)
  556. end
  557. end
  558. function OBJDEF:decode(text, etc)
  559. if type(self) ~= 'table' or self.__index ~= OBJDEF then
  560. OBJDEF:onDecodeError("JSON:decode must be called in method format", nil, nil, etc)
  561. end
  562. if text == nil then
  563. self:onDecodeOfNilError(string.format("nil passed to JSON:decode()"), nil, nil, etc)
  564. elseif type(text) ~= 'string' then
  565. self:onDecodeError(string.format("expected string argument to JSON:decode(), got %s", type(text)), nil, nil, etc)
  566. end
  567. if text:match('^%s*$') then
  568. return nil
  569. end
  570. if text:match('^%s*<') then
  571. -- Can't be JSON... we'll assume it's HTML
  572. self:onDecodeOfHTMLError(string.format("html passed to JSON:decode()"), text, nil, etc)
  573. end
  574. --
  575. -- Ensure that it's not UTF-32 or UTF-16.
  576. -- Those are perfectly valid encodings for JSON (as per RFC 4627 section 3),
  577. -- but this package can't handle them.
  578. --
  579. if text:sub(1,1):byte() == 0 or (text:len() >= 2 and text:sub(2,2):byte() == 0) then
  580. self:onDecodeError("JSON package groks only UTF-8, sorry", text, nil, etc)
  581. end
  582. local success, value = pcall(grok_one, self, text, 1, etc)
  583. if success then
  584. return value
  585. else
  586. -- if JSON:onDecodeError() didn't abort out of the pcall, we'll have received the error message here as "value", so pass it along as an assert.
  587. if self.assert then
  588. self.assert(false, value)
  589. else
  590. assert(false, value)
  591. end
  592. -- and if we're still here, return a nil and throw the error message on as a second arg
  593. return nil, value
  594. end
  595. end
  596. local function backslash_replacement_function(c)
  597. if c == "\n" then
  598. return "\\n"
  599. elseif c == "\r" then
  600. return "\\r"
  601. elseif c == "\t" then
  602. return "\\t"
  603. elseif c == "\b" then
  604. return "\\b"
  605. elseif c == "\f" then
  606. return "\\f"
  607. elseif c == '"' then
  608. return '\\"'
  609. elseif c == '\\' then
  610. return '\\\\'
  611. else
  612. return string.format("\\u%04x", c:byte())
  613. end
  614. end
  615. local chars_to_be_escaped_in_JSON_string
  616. = '['
  617. .. '"' -- class sub-pattern to match a double quote
  618. .. '%\\' -- class sub-pattern to match a backslash
  619. .. '%z' -- class sub-pattern to match a null
  620. .. '\001' .. '-' .. '\031' -- class sub-pattern to match control characters
  621. .. ']'
  622. local function json_string_literal(value)
  623. local newval = value:gsub(chars_to_be_escaped_in_JSON_string, backslash_replacement_function)
  624. return '"' .. newval .. '"'
  625. end
  626. local function object_or_array(self, T, etc)
  627. --
  628. -- We need to inspect all the keys... if there are any strings, we'll convert to a JSON
  629. -- object. If there are only numbers, it's a JSON array.
  630. --
  631. -- If we'll be converting to a JSON object, we'll want to sort the keys so that the
  632. -- end result is deterministic.
  633. --
  634. local string_keys = { }
  635. local number_keys = { }
  636. local number_keys_must_be_strings = false
  637. local maximum_number_key
  638. for key in pairs(T) do
  639. if type(key) == 'string' then
  640. table.insert(string_keys, key)
  641. elseif type(key) == 'number' then
  642. table.insert(number_keys, key)
  643. if key <= 0 or key >= math.huge then
  644. number_keys_must_be_strings = true
  645. elseif not maximum_number_key or key > maximum_number_key then
  646. maximum_number_key = key
  647. end
  648. else
  649. self:onEncodeError("can't encode table with a key of type " .. type(key), etc)
  650. end
  651. end
  652. if #string_keys == 0 and not number_keys_must_be_strings then
  653. --
  654. -- An empty table, or a numeric-only array
  655. --
  656. if #number_keys > 0 then
  657. return nil, maximum_number_key -- an array
  658. elseif tostring(T) == "JSON array" then
  659. return nil
  660. elseif tostring(T) == "JSON object" then
  661. return { }
  662. else
  663. -- have to guess, so we'll pick array, since empty arrays are likely more common than empty objects
  664. return nil
  665. end
  666. end
  667. table.sort(string_keys)
  668. local map
  669. if #number_keys > 0 then
  670. --
  671. -- If we're here then we have either mixed string/number keys, or numbers inappropriate for a JSON array
  672. -- It's not ideal, but we'll turn the numbers into strings so that we can at least create a JSON object.
  673. --
  674. if self.noKeyConversion then
  675. self:onEncodeError("a table with both numeric and string keys could be an object or array; aborting", etc)
  676. end
  677. --
  678. -- Have to make a shallow copy of the source table so we can remap the numeric keys to be strings
  679. --
  680. map = { }
  681. for key, val in pairs(T) do
  682. map[key] = val
  683. end
  684. table.sort(number_keys)
  685. --
  686. -- Throw numeric keys in there as strings
  687. --
  688. for _, number_key in ipairs(number_keys) do
  689. local string_key = tostring(number_key)
  690. if map[string_key] == nil then
  691. table.insert(string_keys , string_key)
  692. map[string_key] = T[number_key]
  693. else
  694. self:onEncodeError("conflict converting table with mixed-type keys into a JSON object: key " .. number_key .. " exists both as a string and a number.", etc)
  695. end
  696. end
  697. end
  698. return string_keys, nil, map
  699. end
  700. --
  701. -- Encode
  702. --
  703. -- 'options' is nil, or a table with possible keys:
  704. -- pretty -- if true, return a pretty-printed version
  705. -- indent -- a string (usually of spaces) used to indent each nested level
  706. -- align_keys -- if true, align all the keys when formatting a table
  707. --
  708. local encode_value -- must predeclare because it calls itself
  709. function encode_value(self, value, parents, etc, options, indent)
  710. if value == nil then
  711. return 'null'
  712. elseif type(value) == 'string' then
  713. return json_string_literal(value)
  714. elseif type(value) == 'number' then
  715. if value ~= value then
  716. --
  717. -- NaN (Not a Number).
  718. -- JSON has no NaN, so we have to fudge the best we can. This should really be a package option.
  719. --
  720. return "null"
  721. elseif value >= math.huge then
  722. --
  723. -- Positive infinity. JSON has no INF, so we have to fudge the best we can. This should
  724. -- really be a package option. Note: at least with some implementations, positive infinity
  725. -- is both ">= math.huge" and "<= -math.huge", which makes no sense but that's how it is.
  726. -- Negative infinity is properly "<= -math.huge". So, we must be sure to check the ">="
  727. -- case first.
  728. --
  729. return "1e+9999"
  730. elseif value <= -math.huge then
  731. --
  732. -- Negative infinity.
  733. -- JSON has no INF, so we have to fudge the best we can. This should really be a package option.
  734. --
  735. return "-1e+9999"
  736. else
  737. return tostring(value)
  738. end
  739. elseif type(value) == 'boolean' then
  740. return tostring(value)
  741. elseif type(value) ~= 'table' then
  742. self:onEncodeError("can't convert " .. type(value) .. " to JSON", etc)
  743. else
  744. --
  745. -- A table to be converted to either a JSON object or array.
  746. --
  747. local T = value
  748. if type(options) ~= 'table' then
  749. options = {}
  750. end
  751. if type(indent) ~= 'string' then
  752. indent = ""
  753. end
  754. if parents[T] then
  755. self:onEncodeError("table " .. tostring(T) .. " is a child of itself", etc)
  756. else
  757. parents[T] = true
  758. end
  759. local result_value
  760. local object_keys, maximum_number_key, map = object_or_array(self, T, etc)
  761. if maximum_number_key then
  762. --
  763. -- An array...
  764. --
  765. local ITEMS = { }
  766. for i = 1, maximum_number_key do
  767. table.insert(ITEMS, encode_value(self, T[i], parents, etc, options, indent))
  768. end
  769. if options.pretty then
  770. result_value = "[ " .. table.concat(ITEMS, ", ") .. " ]"
  771. else
  772. result_value = "[" .. table.concat(ITEMS, ",") .. "]"
  773. end
  774. elseif object_keys then
  775. --
  776. -- An object
  777. --
  778. local TT = map or T
  779. if options.pretty then
  780. local KEYS = { }
  781. local max_key_length = 0
  782. for _, key in ipairs(object_keys) do
  783. local encoded = encode_value(self, tostring(key), parents, etc, options, indent)
  784. if options.align_keys then
  785. max_key_length = math.max(max_key_length, #encoded)
  786. end
  787. table.insert(KEYS, encoded)
  788. end
  789. local key_indent = indent .. tostring(options.indent or "")
  790. local subtable_indent = key_indent .. string.rep(" ", max_key_length) .. (options.align_keys and " " or "")
  791. local FORMAT = "%s%" .. string.format("%d", max_key_length) .. "s: %s"
  792. local COMBINED_PARTS = { }
  793. for i, key in ipairs(object_keys) do
  794. local encoded_val = encode_value(self, TT[key], parents, etc, options, subtable_indent)
  795. table.insert(COMBINED_PARTS, string.format(FORMAT, key_indent, KEYS[i], encoded_val))
  796. end
  797. result_value = "{\n" .. table.concat(COMBINED_PARTS, ",\n") .. "\n" .. indent .. "}"
  798. else
  799. local PARTS = { }
  800. for _, key in ipairs(object_keys) do
  801. local encoded_val = encode_value(self, TT[key], parents, etc, options, indent)
  802. local encoded_key = encode_value(self, tostring(key), parents, etc, options, indent)
  803. table.insert(PARTS, string.format("%s:%s", encoded_key, encoded_val))
  804. end
  805. result_value = "{" .. table.concat(PARTS, ",") .. "}"
  806. end
  807. else
  808. --
  809. -- An empty array/object... we'll treat it as an array, though it should really be an option
  810. --
  811. result_value = "[]"
  812. end
  813. parents[T] = false
  814. return result_value
  815. end
  816. end
  817. function OBJDEF:encode(value, etc, options)
  818. if type(self) ~= 'table' or self.__index ~= OBJDEF then
  819. OBJDEF:onEncodeError("JSON:encode must be called in method format", etc)
  820. end
  821. return encode_value(self, value, {}, etc, options or nil)
  822. end
  823. function OBJDEF:encode_pretty(value, etc, options)
  824. if type(self) ~= 'table' or self.__index ~= OBJDEF then
  825. OBJDEF:onEncodeError("JSON:encode_pretty must be called in method format", etc)
  826. end
  827. return encode_value(self, value, {}, etc, options or default_pretty_options)
  828. end
  829. function OBJDEF.__tostring()
  830. return "JSON encode/decode package"
  831. end
  832. OBJDEF.__index = OBJDEF
  833. function OBJDEF:new(args)
  834. local new = { }
  835. if args then
  836. for key, val in pairs(args) do
  837. new[key] = val
  838. end
  839. end
  840. return setmetatable(new, OBJDEF)
  841. end
  842. return OBJDEF:new()
  843. --
  844. -- Version history:
  845. --
  846. -- 20141223.14 The encode_pretty() routine produced fine results for small datasets, but isn't really
  847. -- appropriate for anything large, so with help from Alex Aulbach I've made the encode routines
  848. -- more flexible, and changed the default encode_pretty() to be more generally useful.
  849. --
  850. -- Added a third 'options' argument to the encode() and encode_pretty() routines, to control
  851. -- how the encoding takes place.
  852. --
  853. -- Updated docs to add assert() call to the loadfile() line, just as good practice so that
  854. -- if there is a problem loading JSON.lua, the appropriate error message will percolate up.
  855. --
  856. -- 20140920.13 Put back (in a way that doesn't cause warnings about unused variables) the author string,
  857. -- so that the source of the package, and its version number, are visible in compiled copies.
  858. --
  859. -- 20140911.12 Minor lua cleanup.
  860. -- Fixed internal reference to 'JSON.noKeyConversion' to reference 'self' instead of 'JSON'.
  861. -- (Thanks to SmugMug's David Parry for these.)
  862. --
  863. -- 20140418.11 JSON nulls embedded within an array were being ignored, such that
  864. -- ["1",null,null,null,null,null,"seven"],
  865. -- would return
  866. -- {1,"seven"}
  867. -- It's now fixed to properly return
  868. -- {1, nil, nil, nil, nil, nil, "seven"}
  869. -- Thanks to "haddock" for catching the error.
  870. --
  871. -- 20140116.10 The user's JSON.assert() wasn't always being used. Thanks to "blue" for the heads up.
  872. --
  873. -- 20131118.9 Update for Lua 5.3... it seems that tostring(2/1) produces "2.0" instead of "2",
  874. -- and this caused some problems.
  875. --
  876. -- 20131031.8 Unified the code for encode() and encode_pretty(); they had been stupidly separate,
  877. -- and had of course diverged (encode_pretty didn't get the fixes that encode got, so
  878. -- sometimes produced incorrect results; thanks to Mattie for the heads up).
  879. --
  880. -- Handle encoding tables with non-positive numeric keys (unlikely, but possible).
  881. --
  882. -- If a table has both numeric and string keys, or its numeric keys are inappropriate
  883. -- (such as being non-positive or infinite), the numeric keys are turned into
  884. -- string keys appropriate for a JSON object. So, as before,
  885. -- JSON:encode({ "one", "two", "three" })
  886. -- produces the array
  887. -- ["one","two","three"]
  888. -- but now something with mixed key types like
  889. -- JSON:encode({ "one", "two", "three", SOMESTRING = "some string" }))
  890. -- instead of throwing an error produces an object:
  891. -- {"1":"one","2":"two","3":"three","SOMESTRING":"some string"}
  892. --
  893. -- To maintain the prior throw-an-error semantics, set
  894. -- JSON.noKeyConversion = true
  895. --
  896. -- 20131004.7 Release under a Creative Commons CC-BY license, which I should have done from day one, sorry.
  897. --
  898. -- 20130120.6 Comment update: added a link to the specific page on my blog where this code can
  899. -- be found, so that folks who come across the code outside of my blog can find updates
  900. -- more easily.
  901. --
  902. -- 20111207.5 Added support for the 'etc' arguments, for better error reporting.
  903. --
  904. -- 20110731.4 More feedback from David Kolf on how to make the tests for Nan/Infinity system independent.
  905. --
  906. -- 20110730.3 Incorporated feedback from David Kolf at http://lua-users.org/wiki/JsonModules:
  907. --
  908. -- * When encoding lua for JSON, Sparse numeric arrays are now handled by
  909. -- spitting out full arrays, such that
  910. -- JSON:encode({"one", "two", [10] = "ten"})
  911. -- returns
  912. -- ["one","two",null,null,null,null,null,null,null,"ten"]
  913. --
  914. -- In 20100810.2 and earlier, only up to the first non-null value would have been retained.
  915. --
  916. -- * When encoding lua for JSON, numeric value NaN gets spit out as null, and infinity as "1+e9999".
  917. -- Version 20100810.2 and earlier created invalid JSON in both cases.
  918. --
  919. -- * Unicode surrogate pairs are now detected when decoding JSON.
  920. --
  921. -- 20100810.2 added some checking to ensure that an invalid Unicode character couldn't leak in to the UTF-8 encoding
  922. --
  923. -- 20100731.1 initial public release
  924. --