json.lua 60 KB

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