JSON.lua 58 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652
  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) ~= 'table' then
  1226. self:onEncodeError("can't convert " .. type(value) .. " to JSON", etc)
  1227. elseif getmetatable(value) == isNumber then
  1228. return tostring(value)
  1229. else
  1230. --
  1231. -- A table to be converted to either a JSON object or array.
  1232. --
  1233. local T = value
  1234. if type(options) ~= 'table' then
  1235. options = {}
  1236. end
  1237. if type(indent) ~= 'string' then
  1238. indent = ""
  1239. end
  1240. if parents[T] then
  1241. self:onEncodeError("table " .. tostring(T) .. " is a child of itself", etc)
  1242. else
  1243. parents[T] = true
  1244. end
  1245. local result_value
  1246. local object_keys, maximum_number_key, map = object_or_array(self, T, etc)
  1247. if maximum_number_key then
  1248. --
  1249. -- An array...
  1250. --
  1251. local key_indent
  1252. if options.array_newline then
  1253. key_indent = indent .. tostring(options.indent or "")
  1254. else
  1255. key_indent = indent
  1256. end
  1257. local ITEMS = { }
  1258. for i = 1, maximum_number_key do
  1259. table.insert(ITEMS, encode_value(self, T[i], parents, etc, options, key_indent))
  1260. end
  1261. if options.array_newline then
  1262. result_value = "[\n" .. key_indent .. table.concat(ITEMS, ",\n" .. key_indent) .. "\n" .. indent .. "]"
  1263. elseif options.pretty then
  1264. result_value = "[ " .. table.concat(ITEMS, ", ") .. " ]"
  1265. else
  1266. result_value = "[" .. table.concat(ITEMS, ",") .. "]"
  1267. end
  1268. elseif object_keys then
  1269. --
  1270. -- An object
  1271. --
  1272. local TT = map or T
  1273. if options.pretty then
  1274. local KEYS = { }
  1275. local max_key_length = 0
  1276. for _, key in ipairs(object_keys) do
  1277. local encoded = encode_value(self, tostring(key), parents, etc, options, indent, true)
  1278. if options.align_keys then
  1279. max_key_length = math.max(max_key_length, #encoded)
  1280. end
  1281. table.insert(KEYS, encoded)
  1282. end
  1283. local key_indent = indent .. tostring(options.indent or "")
  1284. local subtable_indent = key_indent .. string.rep(" ", max_key_length) .. (options.align_keys and " " or "")
  1285. local FORMAT = "%s%" .. string.format("%d", max_key_length) .. "s: %s"
  1286. local COMBINED_PARTS = { }
  1287. for i, key in ipairs(object_keys) do
  1288. local encoded_val = encode_value(self, TT[key], parents, etc, options, subtable_indent)
  1289. table.insert(COMBINED_PARTS, string.format(FORMAT, key_indent, KEYS[i], encoded_val))
  1290. end
  1291. result_value = "{\n" .. table.concat(COMBINED_PARTS, ",\n") .. "\n" .. indent .. "}"
  1292. else
  1293. local PARTS = { }
  1294. for _, key in ipairs(object_keys) do
  1295. local encoded_val = encode_value(self, TT[key], parents, etc, options, indent)
  1296. local encoded_key = encode_value(self, tostring(key), parents, etc, options, indent, true)
  1297. table.insert(PARTS, string.format("%s:%s", encoded_key, encoded_val))
  1298. end
  1299. result_value = "{" .. table.concat(PARTS, ",") .. "}"
  1300. end
  1301. else
  1302. --
  1303. -- An empty array/object... we'll treat it as an array, though it should really be an option
  1304. --
  1305. result_value = "[]"
  1306. end
  1307. parents[T] = false
  1308. return result_value
  1309. end
  1310. end
  1311. local function top_level_encode(self, value, etc, options)
  1312. local val = encode_value(self, value, {}, etc, options)
  1313. if val == nil then
  1314. --PRIVATE("may need to revert to the previous public verison if I can't figure out what the guy wanted")
  1315. return val
  1316. else
  1317. return val
  1318. end
  1319. end
  1320. function OBJDEF:encode(value, etc, options)
  1321. if type(self) ~= 'table' or self.__index ~= OBJDEF then
  1322. OBJDEF:onEncodeError("JSON:encode must be called in method format", etc)
  1323. end
  1324. --
  1325. -- If the user didn't pass in a table of decode options, make an empty one.
  1326. --
  1327. if type(options) ~= 'table' then
  1328. options = {}
  1329. end
  1330. return top_level_encode(self, value, etc, options)
  1331. end
  1332. function OBJDEF:encode_pretty(value, etc, options)
  1333. if type(self) ~= 'table' or self.__index ~= OBJDEF then
  1334. OBJDEF:onEncodeError("JSON:encode_pretty must be called in method format", etc)
  1335. end
  1336. --
  1337. -- If the user didn't pass in a table of decode options, use the default pretty ones
  1338. --
  1339. if type(options) ~= 'table' then
  1340. options = default_pretty_options
  1341. end
  1342. return top_level_encode(self, value, etc, options)
  1343. end
  1344. function OBJDEF.__tostring()
  1345. return "JSON encode/decode package"
  1346. end
  1347. OBJDEF.__index = OBJDEF
  1348. function OBJDEF:new(args)
  1349. local new = { }
  1350. if args then
  1351. for key, val in pairs(args) do
  1352. new[key] = val
  1353. end
  1354. end
  1355. return setmetatable(new, OBJDEF)
  1356. end
  1357. return OBJDEF:new()
  1358. --
  1359. -- Version history:
  1360. --
  1361. -- 20170416.23 Added the "array_newline" formatting option suggested by yurenchen (http://www.yurenchen.com/)
  1362. --
  1363. -- 20161128.22 Added:
  1364. -- JSON:isString()
  1365. -- JSON:isNumber()
  1366. -- JSON:decodeIntegerObjectificationLength
  1367. -- JSON:decodeDecimalObjectificationLength
  1368. --
  1369. -- 20161109.21 Oops, had a small boo-boo in the previous update.
  1370. --
  1371. -- 20161103.20 Used to silently ignore trailing garbage when decoding. Now fails via JSON:onTrailingGarbage()
  1372. -- http://seriot.ch/parsing_json.php
  1373. --
  1374. -- Built-in error message about "expected comma or ']'" had mistakenly referred to '['
  1375. --
  1376. -- Updated the built-in error reporting to refer to bytes rather than characters.
  1377. --
  1378. -- The decode() method no longer assumes that error handlers abort.
  1379. --
  1380. -- Made the VERSION string a string instead of a number
  1381. --
  1382. -- 20160916.19 Fixed the isNumber.__index assignment (thanks to Jack Taylor)
  1383. --
  1384. -- 20160730.18 Added JSON:forceString() and JSON:forceNumber()
  1385. --
  1386. -- 20160728.17 Added concatenation to the metatable for JSON:asNumber()
  1387. --
  1388. -- 20160709.16 Could crash if not passed an options table (thanks jarno heikkinen <jarnoh@capturemonkey.com>).
  1389. --
  1390. -- Made JSON:asNumber() a bit more resilient to being passed the results of itself.
  1391. --
  1392. -- 20160526.15 Added the ability to easily encode null values in JSON, via the new "null" encoding option.
  1393. -- (Thanks to Adam B for bringing up the issue.)
  1394. --
  1395. -- Added some support for very large numbers and precise floats via
  1396. -- JSON.decodeNumbersAsObjects
  1397. -- JSON.decodeIntegerStringificationLength
  1398. -- JSON.decodeDecimalStringificationLength
  1399. --
  1400. -- Added the "stringsAreUtf8" encoding option. (Hat tip to http://lua-users.org/wiki/JsonModules )
  1401. --
  1402. -- 20141223.14 The encode_pretty() routine produced fine results for small datasets, but isn't really
  1403. -- appropriate for anything large, so with help from Alex Aulbach I've made the encode routines
  1404. -- more flexible, and changed the default encode_pretty() to be more generally useful.
  1405. --
  1406. -- Added a third 'options' argument to the encode() and encode_pretty() routines, to control
  1407. -- how the encoding takes place.
  1408. --
  1409. -- Updated docs to add assert() call to the loadfile() line, just as good practice so that
  1410. -- if there is a problem loading JSON.lua, the appropriate error message will percolate up.
  1411. --
  1412. -- 20140920.13 Put back (in a way that doesn't cause warnings about unused variables) the author string,
  1413. -- so that the source of the package, and its version number, are visible in compiled copies.
  1414. --
  1415. -- 20140911.12 Minor lua cleanup.
  1416. -- Fixed internal reference to 'JSON.noKeyConversion' to reference 'self' instead of 'JSON'.
  1417. -- (Thanks to SmugMug's David Parry for these.)
  1418. --
  1419. -- 20140418.11 JSON nulls embedded within an array were being ignored, such that
  1420. -- ["1",null,null,null,null,null,"seven"],
  1421. -- would return
  1422. -- {1,"seven"}
  1423. -- It's now fixed to properly return
  1424. -- {1, nil, nil, nil, nil, nil, "seven"}
  1425. -- Thanks to "haddock" for catching the error.
  1426. --
  1427. -- 20140116.10 The user's JSON.assert() wasn't always being used. Thanks to "blue" for the heads up.
  1428. --
  1429. -- 20131118.9 Update for Lua 5.3... it seems that tostring(2/1) produces "2.0" instead of "2",
  1430. -- and this caused some problems.
  1431. --
  1432. -- 20131031.8 Unified the code for encode() and encode_pretty(); they had been stupidly separate,
  1433. -- and had of course diverged (encode_pretty didn't get the fixes that encode got, so
  1434. -- sometimes produced incorrect results; thanks to Mattie for the heads up).
  1435. --
  1436. -- Handle encoding tables with non-positive numeric keys (unlikely, but possible).
  1437. --
  1438. -- If a table has both numeric and string keys, or its numeric keys are inappropriate
  1439. -- (such as being non-positive or infinite), the numeric keys are turned into
  1440. -- string keys appropriate for a JSON object. So, as before,
  1441. -- JSON:encode({ "one", "two", "three" })
  1442. -- produces the array
  1443. -- ["one","two","three"]
  1444. -- but now something with mixed key types like
  1445. -- JSON:encode({ "one", "two", "three", SOMESTRING = "some string" }))
  1446. -- instead of throwing an error produces an object:
  1447. -- {"1":"one","2":"two","3":"three","SOMESTRING":"some string"}
  1448. --
  1449. -- To maintain the prior throw-an-error semantics, set
  1450. -- JSON.noKeyConversion = true
  1451. --
  1452. -- 20131004.7 Release under a Creative Commons CC-BY license, which I should have done from day one, sorry.
  1453. --
  1454. -- 20130120.6 Comment update: added a link to the specific page on my blog where this code can
  1455. -- be found, so that folks who come across the code outside of my blog can find updates
  1456. -- more easily.
  1457. --
  1458. -- 20111207.5 Added support for the 'etc' arguments, for better error reporting.
  1459. --
  1460. -- 20110731.4 More feedback from David Kolf on how to make the tests for Nan/Infinity system independent.
  1461. --
  1462. -- 20110730.3 Incorporated feedback from David Kolf at http://lua-users.org/wiki/JsonModules:
  1463. --
  1464. -- * When encoding lua for JSON, Sparse numeric arrays are now handled by
  1465. -- spitting out full arrays, such that
  1466. -- JSON:encode({"one", "two", [10] = "ten"})
  1467. -- returns
  1468. -- ["one","two",null,null,null,null,null,null,null,"ten"]
  1469. --
  1470. -- In 20100810.2 and earlier, only up to the first non-null value would have been retained.
  1471. --
  1472. -- * When encoding lua for JSON, numeric value NaN gets spit out as null, and infinity as "1+e9999".
  1473. -- Version 20100810.2 and earlier created invalid JSON in both cases.
  1474. --
  1475. -- * Unicode surrogate pairs are now detected when decoding JSON.
  1476. --
  1477. -- 20100810.2 added some checking to ensure that an invalid Unicode character couldn't leak in to the UTF-8 encoding
  1478. --
  1479. -- 20100731.1 initial public release
  1480. --