Advertisement
Guest User

JSON API

a guest
Jul 6th, 2018
169
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 63.29 KB | None | 0 0
  1. -- -*- coding: utf-8 -*-
  2. --
  3. -- Simple JSON encoding and decoding in pure Lua.
  4. --
  5. -- Copyright 2010-2017 Jeffrey Friedl
  6. -- http://regex.info/blog/
  7. -- Latest version: http://regex.info/blog/lua/json
  8. --
  9. -- This code is released under a Creative Commons CC-BY "Attribution" License:
  10. -- http://creativecommons.org/licenses/by/3.0/deed.en_US
  11. --
  12. -- It can be used for any purpose so long as:
  13. --    1) the copyright notice above is maintained
  14. --    2) the web-page links above are maintained
  15. --    3) the 'AUTHOR_NOTE' string below is maintained
  16. --
  17. local VERSION = '20170927.26' -- version history at end of file
  18. local AUTHOR_NOTE = "-[ JSON.lua package by Jeffrey Friedl (http://regex.info/blog/lua/json) version 20170927.26 ]-"
  19.  
  20. --
  21. -- The 'AUTHOR_NOTE' variable exists so that information about the source
  22. -- of the package is maintained even in compiled versions. It's also
  23. -- included in OBJDEF below mostly to quiet warnings about unused variables.
  24. --
  25. local OBJDEF = {
  26.    VERSION      = VERSION,
  27.    AUTHOR_NOTE  = AUTHOR_NOTE,
  28. }
  29.  
  30.  
  31. --
  32. -- Simple JSON encoding and decoding in pure Lua.
  33. -- JSON definition: http://www.json.org/
  34. --
  35. --
  36. --   JSON = assert(loadfile "JSON.lua")() -- one-time load of the routines
  37. --
  38. --   local lua_value = JSON:decode(raw_json_text)
  39. --
  40. --   local raw_json_text    = JSON:encode(lua_table_or_value)
  41. --   local pretty_json_text = JSON:encode_pretty(lua_table_or_value) -- "pretty printed" version for human readability
  42. --
  43. --
  44. --
  45. -- DECODING (from a JSON string to a Lua table)
  46. --
  47. --
  48. --   JSON = assert(loadfile "JSON.lua")() -- one-time load of the routines
  49. --
  50. --   local lua_value = JSON:decode(raw_json_text)
  51. --
  52. --   If the JSON text is for an object or an array, e.g.
  53. --     { "what": "books", "count": 3 }
  54. --   or
  55. --     [ "Larry", "Curly", "Moe" ]
  56. --
  57. --   the result is a Lua table, e.g.
  58. --     { what = "books", count = 3 }
  59. --   or
  60. --     { "Larry", "Curly", "Moe" }
  61. --
  62. --
  63. --   The encode and decode routines accept an optional second argument,
  64. --   "etc", which is not used during encoding or decoding, but upon error
  65. --   is passed along to error handlers. It can be of any type (including nil).
  66. --
  67. --
  68. --
  69. -- ERROR HANDLING DURING DECODE
  70. --
  71. --   With most errors during decoding, this code calls
  72. --
  73. --      JSON:onDecodeError(message, text, location, etc)
  74. --
  75. --   with a message about the error, and if known, the JSON text being
  76. --   parsed and the byte count where the problem was discovered. You can
  77. --   replace the default JSON:onDecodeError() with your own function.
  78. --
  79. --   The default onDecodeError() merely augments the message with data
  80. --   about the text and the location (and, an 'etc' argument had been
  81. --   provided to decode(), its value is tacked onto the message as well),
  82. --   and then calls JSON.assert(), which itself defaults to Lua's built-in
  83. --   assert(), and can also be overridden.
  84. --
  85. --   For example, in an Adobe Lightroom plugin, you might use something like
  86. --
  87. --          function JSON:onDecodeError(message, text, location, etc)
  88. --             LrErrors.throwUserError("Internal Error: invalid JSON data")
  89. --          end
  90. --
  91. --   or even just
  92. --
  93. --          function JSON.assert(message)
  94. --             LrErrors.throwUserError("Internal Error: " .. message)
  95. --          end
  96. --
  97. --   If JSON:decode() is passed a nil, this is called instead:
  98. --
  99. --      JSON:onDecodeOfNilError(message, nil, nil, etc)
  100. --
  101. --   and if JSON:decode() is passed HTML instead of JSON, this is called:
  102. --
  103. --      JSON:onDecodeOfHTMLError(message, text, nil, etc)
  104. --
  105. --   The use of the 'etc' argument allows stronger coordination between
  106. --   decoding and error reporting, especially when you provide your own
  107. --   error-handling routines. Continuing with the the Adobe Lightroom
  108. --   plugin example:
  109. --
  110. --          function JSON:onDecodeError(message, text, location, etc)
  111. --             local note = "Internal Error: invalid JSON data"
  112. --             if type(etc) = 'table' and etc.photo then
  113. --                note = note .. " while processing for " .. etc.photo:getFormattedMetadata('fileName')
  114. --             end
  115. --             LrErrors.throwUserError(note)
  116. --          end
  117. --
  118. --            :
  119. --            :
  120. --
  121. --          for i, photo in ipairs(photosToProcess) do
  122. --               :
  123. --               :
  124. --               local data = JSON:decode(someJsonText, { photo = photo })
  125. --               :
  126. --               :
  127. --          end
  128. --
  129. --
  130. --
  131. --   If the JSON text passed to decode() has trailing garbage (e.g. as with the JSON "[123]xyzzy"),
  132. --   the method
  133. --
  134. --       JSON:onTrailingGarbage(json_text, location, parsed_value, etc)
  135. --
  136. --   is invoked, where:
  137. --
  138. --       'json_text' is the original JSON text being parsed,
  139. --       'location' is the count of bytes into 'json_text' where the garbage starts (6 in the example),
  140. --       'parsed_value' is the Lua result of what was successfully parsed ({123} in the example),
  141. --       'etc' is as above.
  142. --
  143. --   If JSON:onTrailingGarbage() does not abort, it should return the value decode() should return,
  144. --   or nil + an error message.
  145. --
  146. --     local new_value, error_message = JSON:onTrailingGarbage()
  147. --
  148. --   The default JSON:onTrailingGarbage() simply invokes JSON:onDecodeError("trailing garbage"...),
  149. --   but you can have this package ignore trailing garbage via
  150. --
  151. --      function JSON:onTrailingGarbage(json_text, location, parsed_value, etc)
  152. --         return parsed_value
  153. --      end
  154. --
  155. --
  156. -- DECODING AND STRICT TYPES
  157. --
  158. --   Because both JSON objects and JSON arrays are converted to Lua tables,
  159. --   it's not normally possible to tell which original JSON type a
  160. --   particular Lua table was derived from, or guarantee decode-encode
  161. --   round-trip equivalency.
  162. --
  163. --   However, if you enable strictTypes, e.g.
  164. --
  165. --      JSON = assert(loadfile "JSON.lua")() --load the routines
  166. --      JSON.strictTypes = true
  167. --
  168. --   then the Lua table resulting from the decoding of a JSON object or
  169. --   JSON array is marked via Lua metatable, so that when re-encoded with
  170. --   JSON:encode() it ends up as the appropriate JSON type.
  171. --
  172. --   (This is not the default because other routines may not work well with
  173. --   tables that have a metatable set, for example, Lightroom API calls.)
  174. --
  175. --
  176. -- ENCODING (from a lua table to a JSON string)
  177. --
  178. --   JSON = assert(loadfile "JSON.lua")() -- one-time load of the routines
  179. --
  180. --   local raw_json_text    = JSON:encode(lua_table_or_value)
  181. --   local pretty_json_text = JSON:encode_pretty(lua_table_or_value) -- "pretty printed" version for human readability
  182. --   local custom_pretty    = JSON:encode(lua_table_or_value, etc, { pretty = true, indent = "|  ", align_keys = false })
  183. --
  184. --   On error during encoding, this code calls:
  185. --
  186. --     JSON:onEncodeError(message, etc)
  187. --
  188. --   which you can override in your local JSON object. Also see "HANDLING UNSUPPORTED VALUE TYPES" below.
  189. --
  190. --   The 'etc' in the error call is the second argument to encode() and encode_pretty(), or nil if it wasn't provided.
  191. --
  192. --
  193. --
  194. --
  195. -- ENCODING OPTIONS
  196. --
  197. --   An optional third argument, a table of options, can be provided to encode().
  198. --
  199. --       encode_options =  {
  200. --           -- options for making "pretty" human-readable JSON (see "PRETTY-PRINTING" below)
  201. --           pretty         = true,   -- turn pretty formatting on
  202. --           indent         = "   ",  -- use this indent for each level of an array/object
  203. --           align_keys     = false,  -- if true, align the keys in a way that sounds like it should be nice, but is actually ugly
  204. --           array_newline  = false,  -- if true, array elements become one to a line rather than inline
  205. --
  206. --           -- other output-related options
  207. --           null           = "\0",   -- see "ENCODING JSON NULL VALUES" below
  208. --           stringsAreUtf8 = false,  -- see "HANDLING UNICODE LINE AND PARAGRAPH SEPARATORS FOR JAVA" below
  209. --       }
  210. --
  211. --       json_string = JSON:encode(mytable, etc, encode_options)
  212. --
  213. --
  214. --
  215. -- For reference, the defaults are:
  216. --
  217. --           pretty         = false
  218. --           null           = nil,
  219. --           stringsAreUtf8 = false,
  220. --
  221. --
  222. --
  223. -- PRETTY-PRINTING
  224. --
  225. --   Enabling the 'pretty' encode option helps generate human-readable JSON.
  226. --
  227. --     pretty = JSON:encode(val, etc, {
  228. --                                       pretty = true,
  229. --                                       indent = "   ",
  230. --                                       align_keys = false,
  231. --                                     })
  232. --
  233. --   encode_pretty() is also provided: it's identical to encode() except
  234. --   that encode_pretty() provides a default options table if none given in the call:
  235. --
  236. --       { pretty = true, indent = "  ", align_keys = false, array_newline = false }
  237. --
  238. --   For example, if
  239. --
  240. --      JSON:encode(data)
  241. --
  242. --   produces:
  243. --
  244. --      {"city":"Kyoto","climate":{"avg_temp":16,"humidity":"high","snowfall":"minimal"},"country":"Japan","wards":11}
  245. --
  246. --   then
  247. --
  248. --      JSON:encode_pretty(data)
  249. --
  250. --   produces:
  251. --
  252. --      {
  253. --        "city": "Kyoto",
  254. --        "climate": {
  255. --          "avg_temp": 16,
  256. --          "humidity": "high",
  257. --          "snowfall": "minimal"
  258. --        },
  259. --        "country": "Japan",
  260. --        "wards": 11
  261. --      }
  262. --
  263. --   The following lines all return identical strings:
  264. --       JSON:encode_pretty(data)
  265. --       JSON:encode_pretty(data, nil, { pretty = true, indent = "  ", align_keys = false, array_newline = false})
  266. --       JSON:encode_pretty(data, nil, { pretty = true, indent = "  " })
  267. --       JSON:encode       (data, nil, { pretty = true, indent = "  " })
  268. --
  269. --   An example of setting your own indent string:
  270. --
  271. --     JSON:encode_pretty(data, nil, { pretty = true, indent = "|    " })
  272. --
  273. --   produces:
  274. --
  275. --      {
  276. --      |    "city": "Kyoto",
  277. --      |    "climate": {
  278. --      |    |    "avg_temp": 16,
  279. --      |    |    "humidity": "high",
  280. --      |    |    "snowfall": "minimal"
  281. --      |    },
  282. --      |    "country": "Japan",
  283. --      |    "wards": 11
  284. --      }
  285. --
  286. --   An example of setting align_keys to true:
  287. --
  288. --     JSON:encode_pretty(data, nil, { pretty = true, indent = "  ", align_keys = true })
  289. --
  290. --   produces:
  291. --
  292. --      {
  293. --           "city": "Kyoto",
  294. --        "climate": {
  295. --                     "avg_temp": 16,
  296. --                     "humidity": "high",
  297. --                     "snowfall": "minimal"
  298. --                   },
  299. --        "country": "Japan",
  300. --          "wards": 11
  301. --      }
  302. --
  303. --   which I must admit is kinda ugly, sorry. This was the default for
  304. --   encode_pretty() prior to version 20141223.14.
  305. --
  306. --
  307. --  HANDLING UNICODE LINE AND PARAGRAPH SEPARATORS FOR JAVA
  308. --
  309. --    If the 'stringsAreUtf8' encode option is set to true, consider Lua strings not as a sequence of bytes,
  310. --    but as a sequence of UTF-8 characters.
  311. --
  312. --    Currently, the only practical effect of setting this option is that Unicode LINE and PARAGRAPH
  313. --    separators, if found in a string, are encoded with a JSON escape instead of being dumped as is.
  314. --    The JSON is valid either way, but encoding this way, apparently, allows the resulting JSON
  315. --    to also be valid Java.
  316. --
  317. --  AMBIGUOUS SITUATIONS DURING THE ENCODING
  318. --
  319. --   During the encode, if a Lua table being encoded contains both string
  320. --   and numeric keys, it fits neither JSON's idea of an object, nor its
  321. --   idea of an array. To get around this, when any string key exists (or
  322. --   when non-positive numeric keys exist), numeric keys are converted to
  323. --   strings.
  324. --
  325. --   For example,
  326. --     JSON:encode({ "one", "two", "three", SOMESTRING = "some string" }))
  327. --   produces the JSON object
  328. --     {"1":"one","2":"two","3":"three","SOMESTRING":"some string"}
  329. --
  330. --   To prohibit this conversion and instead make it an error condition, set
  331. --      JSON.noKeyConversion = true
  332. --
  333. --
  334. -- ENCODING JSON NULL VALUES
  335. --
  336. --   Lua tables completely omit keys whose value is nil, so without special handling there's
  337. --   no way to represent JSON object's null value in a Lua table.  For example
  338. --      JSON:encode({ username = "admin", password = nil })
  339. --
  340. --   produces:
  341. --
  342. --      {"username":"admin"}
  343. --
  344. --   In order to actually produce
  345. --
  346. --      {"username":"admin", "password":null}
  347. --
  348.  
  349. --   one can include a string value for a "null" field in the options table passed to encode()....
  350. --   any Lua table entry with that value becomes null in the JSON output:
  351. --
  352. --      JSON:encode({ username = "admin", password = "xyzzy" }, -- First arg is the Lua table to encode as JSON.
  353. --                  nil,                                        -- Second arg is the 'etc' value, ignored here
  354. --                  { null = "xyzzy" })                         -- Third arg is th options table
  355. --
  356. --   produces:
  357. --
  358. --      {"username":"admin", "password":null}
  359. --
  360. --   Just be sure to use a string that is otherwise unlikely to appear in your data.
  361. --   The string "\0" (a string with one null byte) may well be appropriate for many applications.
  362. --
  363. --   The "null" options also applies to Lua tables that become JSON arrays.
  364. --      JSON:encode({ "one", "two", nil, nil })
  365. --
  366. --   produces
  367. --
  368. --      ["one","two"]
  369. --
  370. --   while
  371. --
  372. --      NullPlaceholder = "\0"
  373. --      encode_options = { null = NullPlaceholder }
  374. --      JSON:encode({ "one", "two", NullPlaceholder, NullPlaceholder}, nil, encode_options)
  375. --   produces
  376. --
  377. --      ["one","two",null,null]
  378. --
  379. --
  380. --
  381. -- HANDLING LARGE AND/OR PRECISE NUMBERS
  382. --
  383. --
  384. --   Without special handling, numbers in JSON can lose precision in Lua.
  385. --   For example:
  386. --
  387. --      T = JSON:decode('{  "small":12345, "big":12345678901234567890123456789, "precise":9876.67890123456789012345  }')
  388. --
  389. --      print("small:   ",  type(T.small),    T.small)
  390. --      print("big:     ",  type(T.big),      T.big)
  391. --      print("precise: ",  type(T.precise),  T.precise)
  392. --
  393. --   produces
  394. --
  395. --      small:          number  12345
  396. --      big:            number  1.2345678901235e+28
  397. --      precise:        number  9876.6789012346
  398. --
  399. --   Precision is lost with both 'big' and 'precise'.
  400. --
  401. --   This package offers ways to try to handle this better (for some definitions of "better")...
  402. --
  403. --   The most precise method is by setting the global:
  404. --
  405. --      JSON.decodeNumbersAsObjects = true
  406. --
  407. --   When this is set, numeric JSON data is encoded into Lua in a form that preserves the exact
  408. --   JSON numeric presentation when re-encoded back out to JSON, or accessed in Lua as a string.
  409. --
  410. --   This is done by encoding the numeric data with a Lua table/metatable that returns
  411. --   the possibly-imprecise numeric form when accessed numerically, but the original precise
  412. --   representation when accessed as a string.
  413. --
  414. --   Consider the example above, with this option turned on:
  415. --
  416. --      JSON.decodeNumbersAsObjects = true
  417. --
  418. --      T = JSON:decode('{  "small":12345, "big":12345678901234567890123456789, "precise":9876.67890123456789012345  }')
  419. --
  420. --      print("small:   ",  type(T.small),    T.small)
  421. --      print("big:     ",  type(T.big),      T.big)
  422. --      print("precise: ",  type(T.precise),  T.precise)
  423. --
  424. --   This now produces:
  425. --
  426. --      small:          table   12345
  427. --      big:            table   12345678901234567890123456789
  428. --      precise:        table   9876.67890123456789012345
  429. --
  430. --   However, within Lua you can still use the values (e.g. T.precise in the example above) in numeric
  431. --   contexts. In such cases you'll get the possibly-imprecise numeric version, but in string contexts
  432. --   and when the data finds its way to this package's encode() function, the original full-precision
  433. --   representation is used.
  434. --
  435. --   You can force access to the string or numeric version via
  436. --        JSON:forceString()
  437. --        JSON:forceNumber()
  438. --   For example,
  439. --        local probably_okay = JSON:forceNumber(T.small) -- 'probably_okay' is a number
  440. --
  441. --   Code the inspects the JSON-turned-Lua data using type() can run into troubles because what used to
  442. --   be a number can now be a table (e.g. as the small/big/precise example above shows). Update these
  443. --   situations to use JSON:isNumber(item), which returns nil if the item is neither a number nor one
  444. --   of these number objects. If it is either, it returns the number itself. For completeness there's
  445. --   also JSON:isString(item).
  446. --
  447. --   If you want to try to avoid the hassles of this "number as an object" kludge for all but really
  448. --   big numbers, you can set JSON.decodeNumbersAsObjects and then also set one or both of
  449. --            JSON:decodeIntegerObjectificationLength
  450. --            JSON:decodeDecimalObjectificationLength
  451. --   They refer to the length of the part of the number before and after a decimal point. If they are
  452. --   set and their part is at least that number of digits, objectification occurs. If both are set,
  453. --   objectification occurs when either length is met.
  454. --
  455. --   -----------------------
  456. --
  457. --   Even without using the JSON.decodeNumbersAsObjects option, you can encode numbers in your Lua
  458. --   table that retain high precision upon encoding to JSON, by using the JSON:asNumber() function:
  459. --
  460. --      T = {
  461. --         imprecise =                123456789123456789.123456789123456789,
  462. --         precise   = JSON:asNumber("123456789123456789.123456789123456789")
  463. --      }
  464. --
  465. --      print(JSON:encode_pretty(T))
  466. --
  467. --   This produces:
  468. --
  469. --      {
  470. --         "precise": 123456789123456789.123456789123456789,
  471. --         "imprecise": 1.2345678912346e+17
  472. --      }
  473. --
  474. --
  475. --   -----------------------
  476. --
  477. --   A different way to handle big/precise JSON numbers is to have decode() merely return the exact
  478. --   string representation of the number instead of the number itself. This approach might be useful
  479. --   when the numbers are merely some kind of opaque object identifier and you want to work with them
  480. --   in Lua as strings anyway.
  481. --
  482. --   This approach is enabled by setting
  483. --
  484. --      JSON.decodeIntegerStringificationLength = 10
  485. --
  486. --   The value is the number of digits (of the integer part of the number) at which to stringify numbers.
  487. --   NOTE: this setting is ignored if JSON.decodeNumbersAsObjects is true, as that takes precedence.
  488. --
  489. --   Consider our previous example with this option set to 10:
  490. --
  491. --      JSON.decodeIntegerStringificationLength = 10
  492. --
  493. --      T = JSON:decode('{  "small":12345, "big":12345678901234567890123456789, "precise":9876.67890123456789012345  }')
  494. --
  495. --      print("small:   ",  type(T.small),    T.small)
  496. --      print("big:     ",  type(T.big),      T.big)
  497. --      print("precise: ",  type(T.precise),  T.precise)
  498. --
  499. --   This produces:
  500. --
  501. --      small:          number  12345
  502. --      big:            string  12345678901234567890123456789
  503. --      precise:        number  9876.6789012346
  504. --
  505. --   The long integer of the 'big' field is at least JSON.decodeIntegerStringificationLength digits
  506. --   in length, so it's converted not to a Lua integer but to a Lua string. Using a value of 0 or 1 ensures
  507. --   that all JSON numeric data becomes strings in Lua.
  508. --
  509. --   Note that unlike
  510. --      JSON.decodeNumbersAsObjects = true
  511. --   this stringification is simple and unintelligent: the JSON number simply becomes a Lua string, and that's the end of it.
  512. --   If the string is then converted back to JSON, it's still a string. After running the code above, adding
  513. --      print(JSON:encode(T))
  514. --   produces
  515. --      {"big":"12345678901234567890123456789","precise":9876.6789012346,"small":12345}
  516. --   which is unlikely to be desired.
  517. --
  518. --   There's a comparable option for the length of the decimal part of a number:
  519. --
  520. --      JSON.decodeDecimalStringificationLength
  521. --
  522. --   This can be used alone or in conjunction with
  523. --
  524. --      JSON.decodeIntegerStringificationLength
  525. --
  526. --   to trip stringification on precise numbers with at least JSON.decodeIntegerStringificationLength digits after
  527. --   the decimal point. (Both are ignored if JSON.decodeNumbersAsObjects is true.)
  528. --
  529. --   This example:
  530. --
  531. --      JSON.decodeIntegerStringificationLength = 10
  532. --      JSON.decodeDecimalStringificationLength =  5
  533. --
  534. --      T = JSON:decode('{  "small":12345, "big":12345678901234567890123456789, "precise":9876.67890123456789012345  }')
  535. --
  536. --      print("small:   ",  type(T.small),    T.small)
  537. --      print("big:     ",  type(T.big),      T.big)
  538. --      print("precise: ",  type(T.precise),  T.precise)
  539. --
  540. --  produces:
  541. --
  542. --      small:          number  12345
  543. --      big:            string  12345678901234567890123456789
  544. --      precise:        string  9876.67890123456789012345
  545. --
  546. --
  547. --  HANDLING UNSUPPORTED VALUE TYPES
  548. --
  549. --   Among the encoding errors that might be raised is an attempt to convert a table value that has a type
  550. --   that this package hasn't accounted for: a function, userdata, or a thread. You can handle these types as table
  551. --   values (but not as table keys) if you supply a JSON:unsupportedTypeEncoder() method along the lines of the
  552. --   following example:
  553. --
  554. --        function JSON:unsupportedTypeEncoder(value_of_unsupported_type)
  555. --           if type(value_of_unsupported_type) == 'function' then
  556. --              return "a function value"
  557. --           else
  558. --              return nil
  559. --           end
  560. --        end
  561. --
  562. --   Your unsupportedTypeEncoder() method is actually called with a bunch of arguments:
  563. --
  564. --      self:unsupportedTypeEncoder(value, parents, etc, options, indent, for_key)
  565. --
  566. --   The 'value' is the function, thread, or userdata to be converted to JSON.
  567. --
  568. --   The 'etc' and 'options' arguments are those passed to the original encode(). The other arguments are
  569. --   probably of little interest; see the source code. (Note that 'for_key' is never true, as this function
  570. --   is invoked only on table values; table keys of these types still trigger the onEncodeError method.)
  571. --
  572. --   If your unsupportedTypeEncoder() method returns a string, it's inserted into the JSON as is.
  573. --   If it returns nil plus an error message, that error message is passed through to an onEncodeError invocation.
  574. --   If it returns only nil, processing falls through to a default onEncodeError invocation.
  575. --
  576. --   If you want to handle everything in a simple way:
  577. --
  578. --        function JSON:unsupportedTypeEncoder(value)
  579. --           return tostring(value)
  580. --        end
  581. --
  582. --
  583. -- SUMMARY OF METHODS YOU CAN OVERRIDE IN YOUR LOCAL LUA JSON OBJECT
  584. --
  585. --    assert
  586. --    onDecodeError
  587. --    onDecodeOfNilError
  588. --    onDecodeOfHTMLError
  589. --    onTrailingGarbage
  590. --    onEncodeError
  591. --    unsupportedTypeEncoder
  592. --
  593. --  If you want to create a separate Lua JSON object with its own error handlers,
  594. --  you can reload JSON.lua or use the :new() method.
  595. --
  596. ---------------------------------------------------------------------------
  597.  
  598. local default_pretty_indent  = "  "
  599. local default_pretty_options = { pretty = true, indent = default_pretty_indent, align_keys = false, array_newline = false }
  600.  
  601. local isArray  = { __tostring = function() return "JSON array"         end }  isArray.__index  = isArray
  602. local isObject = { __tostring = function() return "JSON object"        end }  isObject.__index = isObject
  603.  
  604. function OBJDEF:newArray(tbl)
  605.    return setmetatable(tbl or {}, isArray)
  606. end
  607.  
  608. function OBJDEF:newObject(tbl)
  609.    return setmetatable(tbl or {}, isObject)
  610. end
  611.  
  612.  
  613.  
  614.  
  615. local function getnum(op)
  616.    return type(op) == 'number' and op or op.N
  617. end
  618.  
  619. local isNumber = {
  620.    __tostring = function(T)  return T.S        end,
  621.    __unm      = function(op) return getnum(op) end,
  622.  
  623.    __concat   = function(op1, op2) return tostring(op1) .. tostring(op2) end,
  624.    __add      = function(op1, op2) return getnum(op1)   +   getnum(op2)  end,
  625.    __sub      = function(op1, op2) return getnum(op1)   -   getnum(op2)  end,
  626.    __mul      = function(op1, op2) return getnum(op1)   *   getnum(op2)  end,
  627.    __div      = function(op1, op2) return getnum(op1)   /   getnum(op2)  end,
  628.    __mod      = function(op1, op2) return getnum(op1)   %   getnum(op2)  end,
  629.    __pow      = function(op1, op2) return getnum(op1)   ^   getnum(op2)  end,
  630.    __lt       = function(op1, op2) return getnum(op1)   <   getnum(op2)  end,
  631.    __eq       = function(op1, op2) return getnum(op1)   ==  getnum(op2)  end,
  632.    __le       = function(op1, op2) return getnum(op1)   <=  getnum(op2)  end,
  633. }
  634. isNumber.__index = isNumber
  635.  
  636. function OBJDEF:asNumber(item)
  637.  
  638.    if getmetatable(item) == isNumber then
  639.       -- it's already a JSON number object.
  640.       return item
  641.    elseif type(item) == 'table' and type(item.S) == 'string' and type(item.N) == 'number' then
  642.       -- it's a number-object table that lost its metatable, so give it one
  643.       return setmetatable(item, isNumber)
  644.    else
  645.       -- the normal situation... given a number or a string representation of a number....
  646.       local holder = {
  647.          S = tostring(item), -- S is the representation of the number as a string, which remains precise
  648.          N = tonumber(item), -- N is the number as a Lua number.
  649.       }
  650.       return setmetatable(holder, isNumber)
  651.    end
  652. end
  653.  
  654. --
  655. -- Given an item that might be a normal string or number, or might be an 'isNumber' object defined above,
  656. -- return the string version. This shouldn't be needed often because the 'isNumber' object should autoconvert
  657. -- to a string in most cases, but it's here to allow it to be forced when needed.
  658. --
  659. function OBJDEF:forceString(item)
  660.    if type(item) == 'table' and type(item.S) == 'string' then
  661.       return item.S
  662.    else
  663.       return tostring(item)
  664.    end
  665. end
  666.  
  667. --
  668. -- Given an item that might be a normal string or number, or might be an 'isNumber' object defined above,
  669. -- return the numeric version.
  670. --
  671. function OBJDEF:forceNumber(item)
  672.    if type(item) == 'table' and type(item.N) == 'number' then
  673.       return item.N
  674.    else
  675.       return tonumber(item)
  676.    end
  677. end
  678.  
  679. --
  680. -- If the given item is a number, return it. Otherwise, return nil.
  681. -- This, this can be used both in a conditional and to access the number when you're not sure its form.
  682. --
  683. function OBJDEF:isNumber(item)
  684.    if type(item) == 'number' then
  685.       return item
  686.    elseif type(item) == 'table' and type(item.N) == 'number' then
  687.       return item.N
  688.    else
  689.       return nil
  690.    end
  691. end
  692.  
  693. function OBJDEF:isString(item)
  694.    if type(item) == 'string' then
  695.       return item
  696.    elseif type(item) == 'table' and type(item.S) == 'string' then
  697.       return item.S
  698.    else
  699.       return nil
  700.    end
  701. end
  702.  
  703.  
  704. local function unicode_codepoint_as_utf8(codepoint)
  705.    --
  706.    -- codepoint is a number
  707.    --
  708.    if codepoint <= 127 then
  709.       return string.char(codepoint)
  710.  
  711.    elseif codepoint <= 2047 then
  712.       --
  713.       -- 110yyyxx 10xxxxxx         <-- useful notation from http://en.wikipedia.org/wiki/Utf8
  714.       --
  715.       local highpart = math.floor(codepoint / 0x40)
  716.       local lowpart  = codepoint - (0x40 * highpart)
  717.       return string.char(0xC0 + highpart,
  718.                          0x80 + lowpart)
  719.  
  720.    elseif codepoint <= 65535 then
  721.       --
  722.       -- 1110yyyy 10yyyyxx 10xxxxxx
  723.       --
  724.       local highpart  = math.floor(codepoint / 0x1000)
  725.       local remainder = codepoint - 0x1000 * highpart
  726.       local midpart   = math.floor(remainder / 0x40)
  727.       local lowpart   = remainder - 0x40 * midpart
  728.  
  729.       highpart = 0xE0 + highpart
  730.       midpart  = 0x80 + midpart
  731.       lowpart  = 0x80 + lowpart
  732.  
  733.       --
  734.       -- Check for an invalid character (thanks Andy R. at Adobe).
  735.       -- See table 3.7, page 93, in http://www.unicode.org/versions/Unicode5.2.0/ch03.pdf#G28070
  736.       --
  737.       if ( highpart == 0xE0 and midpart < 0xA0 ) or
  738.          ( highpart == 0xED and midpart > 0x9F ) or
  739.          ( highpart == 0xF0 and midpart < 0x90 ) or
  740.          ( highpart == 0xF4 and midpart > 0x8F )
  741.       then
  742.          return "?"
  743.       else
  744.          return string.char(highpart,
  745.                             midpart,
  746.                             lowpart)
  747.       end
  748.  
  749.    else
  750.       --
  751.       -- 11110zzz 10zzyyyy 10yyyyxx 10xxxxxx
  752.       --
  753.       local highpart  = math.floor(codepoint / 0x40000)
  754.       local remainder = codepoint - 0x40000 * highpart
  755.       local midA      = math.floor(remainder / 0x1000)
  756.       remainder       = remainder - 0x1000 * midA
  757.       local midB      = math.floor(remainder / 0x40)
  758.       local lowpart   = remainder - 0x40 * midB
  759.  
  760.       return string.char(0xF0 + highpart,
  761.                          0x80 + midA,
  762.                          0x80 + midB,
  763.                          0x80 + lowpart)
  764.    end
  765. end
  766.  
  767. function OBJDEF:onDecodeError(message, text, location, etc)
  768.    if text then
  769.       if location then
  770.          message = string.format("%s at byte %d of: %s", message, location, text)
  771.       else
  772.          message = string.format("%s: %s", message, text)
  773.       end
  774.    end
  775.  
  776.    if etc ~= nil then
  777.       message = message .. " (" .. OBJDEF:encode(etc) .. ")"
  778.    end
  779.  
  780.    if self.assert then
  781.       self.assert(false, message)
  782.    else
  783.       assert(false, message)
  784.    end
  785. end
  786.  
  787. function OBJDEF:onTrailingGarbage(json_text, location, parsed_value, etc)
  788.    return self:onDecodeError("trailing garbage", json_text, location, etc)
  789. end
  790.  
  791. OBJDEF.onDecodeOfNilError  = OBJDEF.onDecodeError
  792. OBJDEF.onDecodeOfHTMLError = OBJDEF.onDecodeError
  793.  
  794. function OBJDEF:onEncodeError(message, etc)
  795.    if etc ~= nil then
  796.       message = message .. " (" .. OBJDEF:encode(etc) .. ")"
  797.    end
  798.  
  799.    if self.assert then
  800.       self.assert(false, message)
  801.    else
  802.       assert(false, message)
  803.    end
  804. end
  805.  
  806. local function grok_number(self, text, start, options)
  807.    --
  808.    -- Grab the integer part
  809.    --
  810.    local integer_part = text:match('^-?[1-9]%d*', start)
  811.                      or text:match("^-?0",        start)
  812.  
  813.    if not integer_part then
  814.       self:onDecodeError("expected number", text, start, options.etc)
  815.       return nil, start -- in case the error method doesn't abort, return something sensible
  816.    end
  817.  
  818.    local i = start + integer_part:len()
  819.  
  820.    --
  821.    -- Grab an optional decimal part
  822.    --
  823.    local decimal_part = text:match('^%.%d+', i) or ""
  824.  
  825.    i = i + decimal_part:len()
  826.  
  827.    --
  828.    -- Grab an optional exponential part
  829.    --
  830.    local exponent_part = text:match('^[eE][-+]?%d+', i) or ""
  831.  
  832.    i = i + exponent_part:len()
  833.  
  834.    local full_number_text = integer_part .. decimal_part .. exponent_part
  835.  
  836.    if options.decodeNumbersAsObjects then
  837.  
  838.       local objectify = false
  839.  
  840.       if not options.decodeIntegerObjectificationLength and not options.decodeDecimalObjectificationLength then
  841.          -- no options, so objectify
  842.          objectify = true
  843.  
  844.       elseif (options.decodeIntegerObjectificationLength
  845.           and
  846.          (integer_part:len() >= options.decodeIntegerObjectificationLength or exponent_part:len() > 0))
  847.  
  848.           or
  849.          (options.decodeDecimalObjectificationLength
  850.           and
  851.           (decimal_part:len() >= options.decodeDecimalObjectificationLength  or exponent_part:len() > 0))
  852.       then
  853.          -- have options and they are triggered, so objectify
  854.          objectify = true
  855.       end
  856.  
  857.       if objectify then
  858.          return OBJDEF:asNumber(full_number_text), i
  859.       end
  860.       -- else, fall through to try to return as a straight-up number
  861.  
  862.    else
  863.  
  864.       -- Not always decoding numbers as objects, so perhaps encode as strings?
  865.  
  866.       --
  867.       -- If we're told to stringify only under certain conditions, so do.
  868.       -- We punt a bit when there's an exponent by just stringifying no matter what.
  869.       -- I suppose we should really look to see whether the exponent is actually big enough one
  870.       -- way or the other to trip stringification, but I'll be lazy about it until someone asks.
  871.       --
  872.       if (options.decodeIntegerStringificationLength
  873.           and
  874.          (integer_part:len() >= options.decodeIntegerStringificationLength or exponent_part:len() > 0))
  875.  
  876.           or
  877.  
  878.          (options.decodeDecimalStringificationLength
  879.           and
  880.           (decimal_part:len() >= options.decodeDecimalStringificationLength or exponent_part:len() > 0))
  881.       then
  882.          return full_number_text, i -- this returns the exact string representation seen in the original JSON
  883.       end
  884.  
  885.    end
  886.  
  887.  
  888.    local as_number = tonumber(full_number_text)
  889.  
  890.    if not as_number then
  891.       self:onDecodeError("bad number", text, start, options.etc)
  892.       return nil, start -- in case the error method doesn't abort, return something sensible
  893.    end
  894.  
  895.    return as_number, i
  896. end
  897.  
  898.  
  899. local function grok_string(self, text, start, options)
  900.  
  901.    if text:sub(start,start) ~= '"' then
  902.       self:onDecodeError("expected string's opening quote", text, start, options.etc)
  903.       return nil, start -- in case the error method doesn't abort, return something sensible
  904.    end
  905.  
  906.    local i = start + 1 -- +1 to bypass the initial quote
  907.    local text_len = text:len()
  908.    local VALUE = ""
  909.    while i <= text_len do
  910.       local c = text:sub(i,i)
  911.       if c == '"' then
  912.          return VALUE, i + 1
  913.       end
  914.       if c ~= '\\' then
  915.          VALUE = VALUE .. c
  916.          i = i + 1
  917.       elseif text:match('^\\b', i) then
  918.          VALUE = VALUE .. "\b"
  919.          i = i + 2
  920.       elseif text:match('^\\f', i) then
  921.          VALUE = VALUE .. "\f"
  922.          i = i + 2
  923.       elseif text:match('^\\n', i) then
  924.          VALUE = VALUE .. "\n"
  925.          i = i + 2
  926.       elseif text:match('^\\r', i) then
  927.          VALUE = VALUE .. "\r"
  928.          i = i + 2
  929.       elseif text:match('^\\t', i) then
  930.          VALUE = VALUE .. "\t"
  931.          i = i + 2
  932.       else
  933.          local hex = text:match('^\\u([0123456789aAbBcCdDeEfF][0123456789aAbBcCdDeEfF][0123456789aAbBcCdDeEfF][0123456789aAbBcCdDeEfF])', i)
  934.          if hex then
  935.             i = i + 6 -- bypass what we just read
  936.  
  937.             -- We have a Unicode codepoint. It could be standalone, or if in the proper range and
  938.             -- followed by another in a specific range, it'll be a two-code surrogate pair.
  939.             local codepoint = tonumber(hex, 16)
  940.             if codepoint >= 0xD800 and codepoint <= 0xDBFF then
  941.                -- it's a hi surrogate... see whether we have a following low
  942.                local lo_surrogate = text:match('^\\u([dD][cdefCDEF][0123456789aAbBcCdDeEfF][0123456789aAbBcCdDeEfF])', i)
  943.                if lo_surrogate then
  944.                   i = i + 6 -- bypass the low surrogate we just read
  945.                   codepoint = 0x2400 + (codepoint - 0xD800) * 0x400 + tonumber(lo_surrogate, 16)
  946.                else
  947.                   -- not a proper low, so we'll just leave the first codepoint as is and spit it out.
  948.                end
  949.             end
  950.             VALUE = VALUE .. unicode_codepoint_as_utf8(codepoint)
  951.  
  952.          else
  953.  
  954.             -- just pass through what's escaped
  955.             VALUE = VALUE .. text:match('^\\(.)', i)
  956.             i = i + 2
  957.          end
  958.       end
  959.    end
  960.  
  961.    self:onDecodeError("unclosed string", text, start, options.etc)
  962.    return nil, start -- in case the error method doesn't abort, return something sensible
  963. end
  964.  
  965. local function skip_whitespace(text, start)
  966.  
  967.    local _, match_end = text:find("^[ \n\r\t]+", start) -- [http://www.ietf.org/rfc/rfc4627.txt] Section 2
  968.    if match_end then
  969.       return match_end + 1
  970.    else
  971.       return start
  972.    end
  973. end
  974.  
  975. local grok_one -- assigned later
  976.  
  977. local function grok_object(self, text, start, options)
  978.  
  979.    if text:sub(start,start) ~= '{' then
  980.       self:onDecodeError("expected '{'", text, start, options.etc)
  981.       return nil, start -- in case the error method doesn't abort, return something sensible
  982.    end
  983.  
  984.    local i = skip_whitespace(text, start + 1) -- +1 to skip the '{'
  985.  
  986.    local VALUE = self.strictTypes and self:newObject { } or { }
  987.  
  988.    if text:sub(i,i) == '}' then
  989.       return VALUE, i + 1
  990.    end
  991.    local text_len = text:len()
  992.    while i <= text_len do
  993.       local key, new_i = grok_string(self, text, i, options)
  994.  
  995.       i = skip_whitespace(text, new_i)
  996.  
  997.       if text:sub(i, i) ~= ':' then
  998.          self:onDecodeError("expected colon", text, i, options.etc)
  999.          return nil, i -- in case the error method doesn't abort, return something sensible
  1000.       end
  1001.  
  1002.       i = skip_whitespace(text, i + 1)
  1003.  
  1004.       local new_val, new_i = grok_one(self, text, i, options)
  1005.  
  1006.       VALUE[key] = new_val
  1007.  
  1008.       --
  1009.       -- Expect now either '}' to end things, or a ',' to allow us to continue.
  1010.       --
  1011.       i = skip_whitespace(text, new_i)
  1012.  
  1013.       local c = text:sub(i,i)
  1014.  
  1015.       if c == '}' then
  1016.          return VALUE, i + 1
  1017.       end
  1018.  
  1019.       if text:sub(i, i) ~= ',' then
  1020.          self:onDecodeError("expected comma or '}'", text, i, options.etc)
  1021.          return nil, i -- in case the error method doesn't abort, return something sensible
  1022.       end
  1023.  
  1024.       i = skip_whitespace(text, i + 1)
  1025.    end
  1026.  
  1027.    self:onDecodeError("unclosed '{'", text, start, options.etc)
  1028.    return nil, start -- in case the error method doesn't abort, return something sensible
  1029. end
  1030.  
  1031. local function grok_array(self, text, start, options)
  1032.    if text:sub(start,start) ~= '[' then
  1033.       self:onDecodeError("expected '['", text, start, options.etc)
  1034.       return nil, start -- in case the error method doesn't abort, return something sensible
  1035.    end
  1036.  
  1037.    local i = skip_whitespace(text, start + 1) -- +1 to skip the '['
  1038.    local VALUE = self.strictTypes and self:newArray { } or { }
  1039.    if text:sub(i,i) == ']' then
  1040.       return VALUE, i + 1
  1041.    end
  1042.  
  1043.    local VALUE_INDEX = 1
  1044.  
  1045.    local text_len = text:len()
  1046.    while i <= text_len do
  1047.       local val, new_i = grok_one(self, text, i, options)
  1048.  
  1049.       -- can't table.insert(VALUE, val) here because it's a no-op if val is nil
  1050.       VALUE[VALUE_INDEX] = val
  1051.       VALUE_INDEX = VALUE_INDEX + 1
  1052.  
  1053.       i = skip_whitespace(text, new_i)
  1054.  
  1055.       --
  1056.       -- Expect now either ']' to end things, or a ',' to allow us to continue.
  1057.       --
  1058.       local c = text:sub(i,i)
  1059.       if c == ']' then
  1060.          return VALUE, i + 1
  1061.       end
  1062.       if text:sub(i, i) ~= ',' then
  1063.          self:onDecodeError("expected comma or ']'", text, i, options.etc)
  1064.          return nil, i -- in case the error method doesn't abort, return something sensible
  1065.       end
  1066.       i = skip_whitespace(text, i + 1)
  1067.    end
  1068.    self:onDecodeError("unclosed '['", text, start, options.etc)
  1069.    return nil, i -- in case the error method doesn't abort, return something sensible
  1070. end
  1071.  
  1072.  
  1073. grok_one = function(self, text, start, options)
  1074.    -- Skip any whitespace
  1075.    start = skip_whitespace(text, start)
  1076.  
  1077.    if start > text:len() then
  1078.       self:onDecodeError("unexpected end of string", text, nil, options.etc)
  1079.       return nil, start -- in case the error method doesn't abort, return something sensible
  1080.    end
  1081.  
  1082.    if text:find('^"', start) then
  1083.       return grok_string(self, text, start, options.etc)
  1084.  
  1085.    elseif text:find('^[-0123456789 ]', start) then
  1086.       return grok_number(self, text, start, options)
  1087.  
  1088.    elseif text:find('^%{', start) then
  1089.       return grok_object(self, text, start, options)
  1090.  
  1091.    elseif text:find('^%[', start) then
  1092.       return grok_array(self, text, start, options)
  1093.  
  1094.    elseif text:find('^true', start) then
  1095.       return true, start + 4
  1096.  
  1097.    elseif text:find('^false', start) then
  1098.       return false, start + 5
  1099.  
  1100.    elseif text:find('^null', start) then
  1101.       return options.null, start + 4
  1102.  
  1103.    else
  1104.       self:onDecodeError("can't parse JSON", text, start, options.etc)
  1105.       return nil, 1 -- in case the error method doesn't abort, return something sensible
  1106.    end
  1107. end
  1108.  
  1109. function OBJDEF:decode(text, etc, options)
  1110.    --
  1111.    -- If the user didn't pass in a table of decode options, make an empty one.
  1112.    --
  1113.    if type(options) ~= 'table' then
  1114.       options = {}
  1115.    end
  1116.  
  1117.    --
  1118.    -- If they passed in an 'etc' argument, stuff it into the options.
  1119.    -- (If not, any 'etc' field in the options they passed in remains to be used)
  1120.    --
  1121.    if etc ~= nil then
  1122.       options.etc = etc
  1123.    end
  1124.  
  1125.  
  1126.    if type(self) ~= 'table' or self.__index ~= OBJDEF then
  1127.       local error_message = "JSON:decode must be called in method format"
  1128.       OBJDEF:onDecodeError(error_message, nil, nil, options.etc)
  1129.       return nil, error_message -- in case the error method doesn't abort, return something sensible
  1130.    end
  1131.  
  1132.    if text == nil then
  1133.       local error_message = "nil passed to JSON:decode()"
  1134.       self:onDecodeOfNilError(error_message, nil, nil, options.etc)
  1135.       return nil, error_message -- in case the error method doesn't abort, return something sensible
  1136.  
  1137.    elseif type(text) ~= 'string' then
  1138.       local error_message = "expected string argument to JSON:decode()"
  1139.       self:onDecodeError(string.format("%s, got %s", error_message, type(text)), nil, nil, options.etc)
  1140.       return nil, error_message -- in case the error method doesn't abort, return something sensible
  1141.    end
  1142.  
  1143.    if text:match('^%s*$') then
  1144.       -- an empty string is nothing, but not an error
  1145.       return nil
  1146.    end
  1147.  
  1148.    if text:match('^%s*<') then
  1149.       -- Can't be JSON... we'll assume it's HTML
  1150.       local error_message = "HTML passed to JSON:decode()"
  1151.       self:onDecodeOfHTMLError(error_message, text, nil, options.etc)
  1152.       return nil, error_message -- in case the error method doesn't abort, return something sensible
  1153.    end
  1154.  
  1155.    --
  1156.    -- Ensure that it's not UTF-32 or UTF-16.
  1157.    -- Those are perfectly valid encodings for JSON (as per RFC 4627 section 3),
  1158.    -- but this package can't handle them.
  1159.    --
  1160.    if text:sub(1,1):byte() == 0 or (text:len() >= 2 and text:sub(2,2):byte() == 0) then
  1161.       local error_message = "JSON package groks only UTF-8, sorry"
  1162.       self:onDecodeError(error_message, text, nil, options.etc)
  1163.       return nil, error_message -- in case the error method doesn't abort, return something sensible
  1164.    end
  1165.  
  1166.    --
  1167.    -- apply global options
  1168.    --
  1169.    if options.decodeNumbersAsObjects == nil then
  1170.       options.decodeNumbersAsObjects = self.decodeNumbersAsObjects
  1171.    end
  1172.    if options.decodeIntegerObjectificationLength == nil then
  1173.       options.decodeIntegerObjectificationLength = self.decodeIntegerObjectificationLength
  1174.    end
  1175.    if options.decodeDecimalObjectificationLength == nil then
  1176.       options.decodeDecimalObjectificationLength = self.decodeDecimalObjectificationLength
  1177.    end
  1178.    if options.decodeIntegerStringificationLength == nil then
  1179.       options.decodeIntegerStringificationLength = self.decodeIntegerStringificationLength
  1180.    end
  1181.    if options.decodeDecimalStringificationLength == nil then
  1182.       options.decodeDecimalStringificationLength = self.decodeDecimalStringificationLength
  1183.    end
  1184.  
  1185.  
  1186.    --
  1187.    -- Finally, go parse it
  1188.    --
  1189.    local success, value, next_i = pcall(grok_one, self, text, 1, options)
  1190.  
  1191.    if success then
  1192.  
  1193.       local error_message = nil
  1194.       if next_i ~= #text + 1 then
  1195.          -- something's left over after we parsed the first thing.... whitespace is allowed.
  1196.          next_i = skip_whitespace(text, next_i)
  1197.  
  1198.          -- if we have something left over now, it's trailing garbage
  1199.          if next_i ~= #text + 1 then
  1200.             value, error_message = self:onTrailingGarbage(text, next_i, value, options.etc)
  1201.          end
  1202.       end
  1203.       return value, error_message
  1204.  
  1205.    else
  1206.  
  1207.       -- If JSON:onDecodeError() didn't abort out of the pcall, we'll have received
  1208.       -- the error message here as "value", so pass it along as an assert.
  1209.       local error_message = value
  1210.       if self.assert then
  1211.          self.assert(false, error_message)
  1212.       else
  1213.          assert(false, error_message)
  1214.       end
  1215.       -- ...and if we're still here (because the assert didn't throw an error),
  1216.       -- return a nil and throw the error message on as a second arg
  1217.       return nil, error_message
  1218.  
  1219.    end
  1220. end
  1221.  
  1222. local function backslash_replacement_function(c)
  1223.    if c == "\n" then
  1224.       return "\\n"
  1225.    elseif c == "\r" then
  1226.       return "\\r"
  1227.    elseif c == "\t" then
  1228.       return "\\t"
  1229.    elseif c == "\b" then
  1230.       return "\\b"
  1231.    elseif c == "\f" then
  1232.       return "\\f"
  1233.    elseif c == '"' then
  1234.       return '\\"'
  1235.    elseif c == '\\' then
  1236.       return '\\\\'
  1237.    else
  1238.       return string.format("\\u%04x", c:byte())
  1239.    end
  1240. end
  1241.  
  1242. local chars_to_be_escaped_in_JSON_string
  1243.    = '['
  1244.    ..    '"'    -- class sub-pattern to match a double quote
  1245.    ..    '%\\'  -- class sub-pattern to match a backslash
  1246.    ..    '%z'   -- class sub-pattern to match a null
  1247.    ..    '\001' .. '-' .. '\031' -- class sub-pattern to match control characters
  1248.    .. ']'
  1249.  
  1250.  
  1251. local LINE_SEPARATOR_as_utf8      = unicode_codepoint_as_utf8(0x2028)
  1252. local PARAGRAPH_SEPARATOR_as_utf8 = unicode_codepoint_as_utf8(0x2029)
  1253. local function json_string_literal(value, options)
  1254.    local newval = value:gsub(chars_to_be_escaped_in_JSON_string, backslash_replacement_function)
  1255.    if options.stringsAreUtf8 then
  1256.       --
  1257.       -- This feels really ugly to just look into a string for the sequence of bytes that we know to be a particular utf8 character,
  1258.       -- but utf8 was designed purposefully to make this kind of thing possible. Still, feels dirty.
  1259.       -- I'd rather decode the byte stream into a character stream, but it's not technically needed so
  1260.       -- not technically worth it.
  1261.       --
  1262.       newval = newval:gsub(LINE_SEPARATOR_as_utf8, '\\u2028'):gsub(PARAGRAPH_SEPARATOR_as_utf8,'\\u2029')
  1263.    end
  1264.    return '"' .. newval .. '"'
  1265. end
  1266.  
  1267. local function object_or_array(self, T, etc)
  1268.    --
  1269.    -- We need to inspect all the keys... if there are any strings, we'll convert to a JSON
  1270.    -- object. If there are only numbers, it's a JSON array.
  1271.    --
  1272.    -- If we'll be converting to a JSON object, we'll want to sort the keys so that the
  1273.    -- end result is deterministic.
  1274.    --
  1275.    local string_keys = { }
  1276.    local number_keys = { }
  1277.    local number_keys_must_be_strings = false
  1278.    local maximum_number_key
  1279.  
  1280.    for key in pairs(T) do
  1281.       if type(key) == 'string' then
  1282.          table.insert(string_keys, key)
  1283.       elseif type(key) == 'number' then
  1284.          table.insert(number_keys, key)
  1285.          if key <= 0 or key >= math.huge then
  1286.             number_keys_must_be_strings = true
  1287.          elseif not maximum_number_key or key > maximum_number_key then
  1288.             maximum_number_key = key
  1289.          end
  1290.       elseif type(key) == 'boolean' then
  1291.          table.insert(string_keys, tostring(key))
  1292.       else
  1293.          self:onEncodeError("can't encode table with a key of type " .. type(key), etc)
  1294.       end
  1295.    end
  1296.  
  1297.    if #string_keys == 0 and not number_keys_must_be_strings then
  1298.       --
  1299.       -- An empty table, or a numeric-only array
  1300.       --
  1301.       if #number_keys > 0 then
  1302.          return nil, maximum_number_key -- an array
  1303.       elseif tostring(T) == "JSON array" then
  1304.          return nil
  1305.       elseif tostring(T) == "JSON object" then
  1306.          return { }
  1307.       else
  1308.          -- have to guess, so we'll pick array, since empty arrays are likely more common than empty objects
  1309.          return nil
  1310.       end
  1311.    end
  1312.  
  1313.    table.sort(string_keys)
  1314.  
  1315.    local map
  1316.    if #number_keys > 0 then
  1317.       --
  1318.       -- If we're here then we have either mixed string/number keys, or numbers inappropriate for a JSON array
  1319.       -- It's not ideal, but we'll turn the numbers into strings so that we can at least create a JSON object.
  1320.       --
  1321.  
  1322.       if self.noKeyConversion then
  1323.          self:onEncodeError("a table with both numeric and string keys could be an object or array; aborting", etc)
  1324.       end
  1325.  
  1326.       --
  1327.       -- Have to make a shallow copy of the source table so we can remap the numeric keys to be strings
  1328.       --
  1329.       map = { }
  1330.       for key, val in pairs(T) do
  1331.          map[key] = val
  1332.       end
  1333.  
  1334.       table.sort(number_keys)
  1335.  
  1336.       --
  1337.       -- Throw numeric keys in there as strings
  1338.       --
  1339.       for _, number_key in ipairs(number_keys) do
  1340.          local string_key = tostring(number_key)
  1341.          if map[string_key] == nil then
  1342.             table.insert(string_keys , string_key)
  1343.             map[string_key] = T[number_key]
  1344.          else
  1345.             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)
  1346.          end
  1347.       end
  1348.    end
  1349.  
  1350.    return string_keys, nil, map
  1351. end
  1352.  
  1353. --
  1354. -- Encode
  1355. --
  1356. -- 'options' is nil, or a table with possible keys:
  1357. --
  1358. --    pretty         -- If true, return a pretty-printed version.
  1359. --
  1360. --    indent         -- A string (usually of spaces) used to indent each nested level.
  1361. --
  1362. --    align_keys     -- If true, align all the keys when formatting a table. The result is uglier than one might at first imagine.
  1363. --                      Results are undefined if 'align_keys' is true but 'pretty' is not.
  1364. --
  1365. --    array_newline  -- If true, array elements are formatted each to their own line. The default is to all fall inline.
  1366. --                      Results are undefined if 'array_newline' is true but 'pretty' is not.
  1367. --
  1368. --    null           -- If this exists with a string value, table elements with this value are output as JSON null.
  1369. --
  1370. --    stringsAreUtf8 -- If true, consider Lua strings not as a sequence of bytes, but as a sequence of UTF-8 characters.
  1371. --                      (Currently, the only practical effect of setting this option is that Unicode LINE and PARAGRAPH
  1372. --                       separators, if found in a string, are encoded with a JSON escape instead of as raw UTF-8.
  1373. --                       The JSON is valid either way, but encoding this way, apparently, allows the resulting JSON
  1374. --                       to also be valid Java.)
  1375. --
  1376. --
  1377. local function encode_value(self, value, parents, etc, options, indent, for_key)
  1378.  
  1379.    --
  1380.    -- keys in a JSON object can never be null, so we don't even consider options.null when converting a key value
  1381.    --
  1382.    if value == nil or (not for_key and options and options.null and value == options.null) then
  1383.       return 'null'
  1384.  
  1385.    elseif type(value) == 'string' then
  1386.       return json_string_literal(value, options)
  1387.  
  1388.    elseif type(value) == 'number' then
  1389.       if value ~= value then
  1390.          --
  1391.          -- NaN (Not a Number).
  1392.          -- JSON has no NaN, so we have to fudge the best we can. This should really be a package option.
  1393.          --
  1394.          return "null"
  1395.       elseif value >= math.huge then
  1396.          --
  1397.          -- Positive infinity. JSON has no INF, so we have to fudge the best we can. This should
  1398.          -- really be a package option. Note: at least with some implementations, positive infinity
  1399.          -- is both ">= math.huge" and "<= -math.huge", which makes no sense but that's how it is.
  1400.          -- Negative infinity is properly "<= -math.huge". So, we must be sure to check the ">="
  1401.          -- case first.
  1402.          --
  1403.          return "1e+9999"
  1404.       elseif value <= -math.huge then
  1405.          --
  1406.          -- Negative infinity.
  1407.          -- JSON has no INF, so we have to fudge the best we can. This should really be a package option.
  1408.          --
  1409.          return "-1e+9999"
  1410.       else
  1411.          return tostring(value)
  1412.       end
  1413.  
  1414.    elseif type(value) == 'boolean' then
  1415.       return tostring(value)
  1416.  
  1417.    elseif type(value) ~= 'table' then
  1418.  
  1419.       if self.unsupportedTypeEncoder then
  1420.          local user_value, user_error = self:unsupportedTypeEncoder(value, parents, etc, options, indent, for_key)
  1421.          -- If the user's handler returns a string, use that. If it returns nil plus an error message, bail with that.
  1422.          -- If only nil returned, fall through to the default error handler.
  1423.          if type(user_value) == 'string' then
  1424.             return user_value
  1425.          elseif user_value ~= nil then
  1426.             self:onEncodeError("unsupportedTypeEncoder method returned a " .. type(user_value), etc)
  1427.          elseif user_error then
  1428.             self:onEncodeError(tostring(user_error), etc)
  1429.          end
  1430.       end
  1431.  
  1432.       self:onEncodeError("can't convert " .. type(value) .. " to JSON", etc)
  1433.  
  1434.    elseif getmetatable(value) == isNumber then
  1435.       return tostring(value)
  1436.    else
  1437.       --
  1438.       -- A table to be converted to either a JSON object or array.
  1439.       --
  1440.       local T = value
  1441.  
  1442.       if type(options) ~= 'table' then
  1443.          options = {}
  1444.       end
  1445.       if type(indent) ~= 'string' then
  1446.          indent = ""
  1447.       end
  1448.  
  1449.       if parents[T] then
  1450.          self:onEncodeError("table " .. tostring(T) .. " is a child of itself", etc)
  1451.       else
  1452.          parents[T] = true
  1453.       end
  1454.  
  1455.       local result_value
  1456.  
  1457.       local object_keys, maximum_number_key, map = object_or_array(self, T, etc)
  1458.       if maximum_number_key then
  1459.          --
  1460.          -- An array...
  1461.          --
  1462.          local key_indent
  1463.          if options.array_newline then
  1464.             key_indent = indent .. tostring(options.indent or "")
  1465.          else
  1466.             key_indent = indent
  1467.          end
  1468.  
  1469.          local ITEMS = { }
  1470.          for i = 1, maximum_number_key do
  1471.             table.insert(ITEMS, encode_value(self, T[i], parents, etc, options, key_indent))
  1472.          end
  1473.  
  1474.          if options.array_newline then
  1475.             result_value = "[\n" .. key_indent .. table.concat(ITEMS, ",\n" .. key_indent) .. "\n" .. indent .. "]"
  1476.          elseif options.pretty then
  1477.             result_value = "[ " .. table.concat(ITEMS, ", ") .. " ]"
  1478.          else
  1479.             result_value = "["  .. table.concat(ITEMS, ",")  .. "]"
  1480.          end
  1481.  
  1482.       elseif object_keys then
  1483.          --
  1484.          -- An object
  1485.          --
  1486.          local TT = map or T
  1487.  
  1488.          if options.pretty then
  1489.  
  1490.             local KEYS = { }
  1491.             local max_key_length = 0
  1492.             for _, key in ipairs(object_keys) do
  1493.                local encoded = encode_value(self, tostring(key), parents, etc, options, indent, true)
  1494.                if options.align_keys then
  1495.                   max_key_length = math.max(max_key_length, #encoded)
  1496.                end
  1497.                table.insert(KEYS, encoded)
  1498.             end
  1499.             local key_indent = indent .. tostring(options.indent or "")
  1500.             local subtable_indent = key_indent .. string.rep(" ", max_key_length) .. (options.align_keys and "  " or "")
  1501.             local FORMAT = "%s%" .. string.format("%d", max_key_length) .. "s: %s"
  1502.  
  1503.             local COMBINED_PARTS = { }
  1504.             for i, key in ipairs(object_keys) do
  1505.                local encoded_val = encode_value(self, TT[key], parents, etc, options, subtable_indent)
  1506.                table.insert(COMBINED_PARTS, string.format(FORMAT, key_indent, KEYS[i], encoded_val))
  1507.             end
  1508.             result_value = "{\n" .. table.concat(COMBINED_PARTS, ",\n") .. "\n" .. indent .. "}"
  1509.  
  1510.          else
  1511.  
  1512.             local PARTS = { }
  1513.             for _, key in ipairs(object_keys) do
  1514.                local encoded_val = encode_value(self, TT[key],       parents, etc, options, indent)
  1515.                local encoded_key = encode_value(self, tostring(key), parents, etc, options, indent, true)
  1516.                table.insert(PARTS, string.format("%s:%s", encoded_key, encoded_val))
  1517.             end
  1518.             result_value = "{" .. table.concat(PARTS, ",") .. "}"
  1519.  
  1520.          end
  1521.       else
  1522.          --
  1523.          -- An empty array/object... we'll treat it as an array, though it should really be an option
  1524.          --
  1525.          result_value = "[]"
  1526.       end
  1527.  
  1528.       parents[T] = false
  1529.       return result_value
  1530.    end
  1531. end
  1532.  
  1533. local function top_level_encode(self, value, etc, options)
  1534.    local val = encode_value(self, value, {}, etc, options)
  1535.    if val == nil then
  1536.       --PRIVATE("may need to revert to the previous public verison if I can't figure out what the guy wanted")
  1537.       return val
  1538.    else
  1539.       return val
  1540.    end
  1541. end
  1542.  
  1543. function OBJDEF:encode(value, etc, options)
  1544.    if type(self) ~= 'table' or self.__index ~= OBJDEF then
  1545.       OBJDEF:onEncodeError("JSON:encode must be called in method format", etc)
  1546.    end
  1547.  
  1548.    --
  1549.    -- If the user didn't pass in a table of decode options, make an empty one.
  1550.    --
  1551.    if type(options) ~= 'table' then
  1552.       options = {}
  1553.    end
  1554.  
  1555.    return top_level_encode(self, value, etc, options)
  1556. end
  1557.  
  1558. function OBJDEF:encode_pretty(value, etc, options)
  1559.    if type(self) ~= 'table' or self.__index ~= OBJDEF then
  1560.       OBJDEF:onEncodeError("JSON:encode_pretty must be called in method format", etc)
  1561.    end
  1562.  
  1563.    --
  1564.    -- If the user didn't pass in a table of decode options, use the default pretty ones
  1565.    --
  1566.    if type(options) ~= 'table' then
  1567.       options = default_pretty_options
  1568.    end
  1569.  
  1570.    return top_level_encode(self, value, etc, options)
  1571. end
  1572.  
  1573. function OBJDEF.__tostring()
  1574.    return "JSON encode/decode package"
  1575. end
  1576.  
  1577. OBJDEF.__index = OBJDEF
  1578.  
  1579. function OBJDEF:new(args)
  1580.    local new = { }
  1581.  
  1582.    if args then
  1583.       for key, val in pairs(args) do
  1584.          new[key] = val
  1585.       end
  1586.    end
  1587.  
  1588.    return setmetatable(new, OBJDEF)
  1589. end
  1590.  
  1591. return OBJDEF:new()
  1592.  
  1593. --
  1594. -- Version history:
  1595. --
  1596. --   20170927.26   Use option.null in decoding as well. Thanks to Max Sindwani for the bump, and sorry to Oliver Hitz
  1597. --                 whose first mention of it four years ago was completely missed by me.
  1598. --
  1599. --   20170823.25   Added support for JSON:unsupportedTypeEncoder().
  1600. --                 Thanks to Chronos Phaenon Eosphoros (https://github.com/cpeosphoros) for the idea.
  1601. --
  1602. --   20170819.24   Added support for boolean keys in tables.
  1603. --
  1604. --   20170416.23   Added the "array_newline" formatting option suggested by yurenchen (http://www.yurenchen.com/)
  1605. --
  1606. --   20161128.22   Added:
  1607. --                   JSON:isString()
  1608. --                   JSON:isNumber()
  1609. --                   JSON:decodeIntegerObjectificationLength
  1610. --                   JSON:decodeDecimalObjectificationLength
  1611. --
  1612. --   20161109.21   Oops, had a small boo-boo in the previous update.
  1613. --
  1614. --   20161103.20   Used to silently ignore trailing garbage when decoding. Now fails via JSON:onTrailingGarbage()
  1615. --                 http://seriot.ch/parsing_json.php
  1616. --
  1617. --                 Built-in error message about "expected comma or ']'" had mistakenly referred to '['
  1618. --
  1619. --                 Updated the built-in error reporting to refer to bytes rather than characters.
  1620. --
  1621. --                 The decode() method no longer assumes that error handlers abort.
  1622. --
  1623. --                 Made the VERSION string a string instead of a number
  1624. --
  1625.  
  1626. --   20160916.19   Fixed the isNumber.__index assignment (thanks to Jack Taylor)
  1627. --
  1628. --   20160730.18   Added JSON:forceString() and JSON:forceNumber()
  1629. --
  1630. --   20160728.17   Added concatenation to the metatable for JSON:asNumber()
  1631. --
  1632. --   20160709.16   Could crash if not passed an options table (thanks jarno heikkinen <jarnoh@capturemonkey.com>).
  1633. --
  1634. --                 Made JSON:asNumber() a bit more resilient to being passed the results of itself.
  1635. --
  1636. --   20160526.15   Added the ability to easily encode null values in JSON, via the new "null" encoding option.
  1637. --                 (Thanks to Adam B for bringing up the issue.)
  1638. --
  1639. --                 Added some support for very large numbers and precise floats via
  1640. --                    JSON.decodeNumbersAsObjects
  1641. --                    JSON.decodeIntegerStringificationLength
  1642. --                    JSON.decodeDecimalStringificationLength
  1643. --
  1644. --                 Added the "stringsAreUtf8" encoding option. (Hat tip to http://lua-users.org/wiki/JsonModules )
  1645. --
  1646. --   20141223.14   The encode_pretty() routine produced fine results for small datasets, but isn't really
  1647. --                 appropriate for anything large, so with help from Alex Aulbach I've made the encode routines
  1648. --                 more flexible, and changed the default encode_pretty() to be more generally useful.
  1649. --
  1650. --                 Added a third 'options' argument to the encode() and encode_pretty() routines, to control
  1651. --                 how the encoding takes place.
  1652. --
  1653. --                 Updated docs to add assert() call to the loadfile() line, just as good practice so that
  1654. --                 if there is a problem loading JSON.lua, the appropriate error message will percolate up.
  1655. --
  1656. --   20140920.13   Put back (in a way that doesn't cause warnings about unused variables) the author string,
  1657. --                 so that the source of the package, and its version number, are visible in compiled copies.
  1658. --
  1659. --   20140911.12   Minor lua cleanup.
  1660. --                 Fixed internal reference to 'JSON.noKeyConversion' to reference 'self' instead of 'JSON'.
  1661. --                 (Thanks to SmugMug's David Parry for these.)
  1662. --
  1663. --   20140418.11   JSON nulls embedded within an array were being ignored, such that
  1664. --                     ["1",null,null,null,null,null,"seven"],
  1665. --                 would return
  1666. --                     {1,"seven"}
  1667. --                 It's now fixed to properly return
  1668. --                     {1, nil, nil, nil, nil, nil, "seven"}
  1669. --                 Thanks to "haddock" for catching the error.
  1670. --
  1671. --   20140116.10   The user's JSON.assert() wasn't always being used. Thanks to "blue" for the heads up.
  1672. --
  1673. --   20131118.9    Update for Lua 5.3... it seems that tostring(2/1) produces "2.0" instead of "2",
  1674. --                 and this caused some problems.
  1675. --
  1676. --   20131031.8    Unified the code for encode() and encode_pretty(); they had been stupidly separate,
  1677. --                 and had of course diverged (encode_pretty didn't get the fixes that encode got, so
  1678. --                 sometimes produced incorrect results; thanks to Mattie for the heads up).
  1679. --
  1680. --                 Handle encoding tables with non-positive numeric keys (unlikely, but possible).
  1681. --
  1682. --                 If a table has both numeric and string keys, or its numeric keys are inappropriate
  1683. --                 (such as being non-positive or infinite), the numeric keys are turned into
  1684. --                 string keys appropriate for a JSON object. So, as before,
  1685. --                         JSON:encode({ "one", "two", "three" })
  1686. --                 produces the array
  1687. --                         ["one","two","three"]
  1688. --                 but now something with mixed key types like
  1689. --                         JSON:encode({ "one", "two", "three", SOMESTRING = "some string" }))
  1690. --                 instead of throwing an error produces an object:
  1691. --                         {"1":"one","2":"two","3":"three","SOMESTRING":"some string"}
  1692. --
  1693. --                 To maintain the prior throw-an-error semantics, set
  1694. --                      JSON.noKeyConversion = true
  1695. --
  1696. --   20131004.7    Release under a Creative Commons CC-BY license, which I should have done from day one, sorry.
  1697. --
  1698. --   20130120.6    Comment update: added a link to the specific page on my blog where this code can
  1699. --                 be found, so that folks who come across the code outside of my blog can find updates
  1700. --                 more easily.
  1701. --
  1702. --   20111207.5    Added support for the 'etc' arguments, for better error reporting.
  1703. --
  1704. --   20110731.4    More feedback from David Kolf on how to make the tests for Nan/Infinity system independent.
  1705. --
  1706. --   20110730.3    Incorporated feedback from David Kolf at http://lua-users.org/wiki/JsonModules:
  1707. --
  1708. --                   * When encoding lua for JSON, Sparse numeric arrays are now handled by
  1709. --                     spitting out full arrays, such that
  1710. --                        JSON:encode({"one", "two", [10] = "ten"})
  1711. --                     returns
  1712. --                        ["one","two",null,null,null,null,null,null,null,"ten"]
  1713. --
  1714. --                     In 20100810.2 and earlier, only up to the first non-null value would have been retained.
  1715. --
  1716. --                   * When encoding lua for JSON, numeric value NaN gets spit out as null, and infinity as "1+e9999".
  1717. --                     Version 20100810.2 and earlier created invalid JSON in both cases.
  1718. --
  1719. --                   * Unicode surrogate pairs are now detected when decoding JSON.
  1720. --
  1721. --   20100810.2    added some checking to ensure that an invalid Unicode character couldn't leak in to the UTF-8 encoding
  1722. --
  1723. --   20100731.1    initial public release
  1724. --
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement