manual.html 47 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333
  1. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN"
  2. "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
  3. <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en">
  4. <head>
  5. <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
  6. <meta name="generator" content="AsciiDoc 8.4.5" />
  7. <title>Lua CJSON 2.1.0 Manual</title>
  8. <style type="text/css">
  9. /* Debug borders */
  10. p, li, dt, dd, div, pre, h1, h2, h3, h4, h5, h6 {
  11. /*
  12. border: 1px solid red;
  13. */
  14. }
  15. body {
  16. margin: 1em 5% 1em 5%;
  17. }
  18. a {
  19. color: blue;
  20. text-decoration: underline;
  21. }
  22. a:visited {
  23. color: fuchsia;
  24. }
  25. em {
  26. font-style: italic;
  27. color: navy;
  28. }
  29. strong {
  30. font-weight: bold;
  31. color: #083194;
  32. }
  33. tt {
  34. color: navy;
  35. }
  36. h1, h2, h3, h4, h5, h6 {
  37. color: #527bbd;
  38. font-family: sans-serif;
  39. margin-top: 1.2em;
  40. margin-bottom: 0.5em;
  41. line-height: 1.3;
  42. }
  43. h1, h2, h3 {
  44. border-bottom: 2px solid silver;
  45. }
  46. h2 {
  47. padding-top: 0.5em;
  48. }
  49. h3 {
  50. float: left;
  51. }
  52. h3 + * {
  53. clear: left;
  54. }
  55. div.sectionbody {
  56. font-family: serif;
  57. margin-left: 0;
  58. }
  59. hr {
  60. border: 1px solid silver;
  61. }
  62. p {
  63. margin-top: 0.5em;
  64. margin-bottom: 0.5em;
  65. }
  66. ul, ol, li > p {
  67. margin-top: 0;
  68. }
  69. pre {
  70. padding: 0;
  71. margin: 0;
  72. }
  73. span#author {
  74. color: #527bbd;
  75. font-family: sans-serif;
  76. font-weight: bold;
  77. font-size: 1.1em;
  78. }
  79. span#email {
  80. }
  81. span#revnumber, span#revdate, span#revremark {
  82. font-family: sans-serif;
  83. }
  84. div#footer {
  85. font-family: sans-serif;
  86. font-size: small;
  87. border-top: 2px solid silver;
  88. padding-top: 0.5em;
  89. margin-top: 4.0em;
  90. }
  91. div#footer-text {
  92. float: left;
  93. padding-bottom: 0.5em;
  94. }
  95. div#footer-badges {
  96. float: right;
  97. padding-bottom: 0.5em;
  98. }
  99. div#preamble {
  100. margin-top: 1.5em;
  101. margin-bottom: 1.5em;
  102. }
  103. div.tableblock, div.imageblock, div.exampleblock, div.verseblock,
  104. div.quoteblock, div.literalblock, div.listingblock, div.sidebarblock,
  105. div.admonitionblock {
  106. margin-top: 1.5em;
  107. margin-bottom: 1.5em;
  108. }
  109. div.admonitionblock {
  110. margin-top: 2.5em;
  111. margin-bottom: 2.5em;
  112. }
  113. div.content { /* Block element content. */
  114. padding: 0;
  115. }
  116. /* Block element titles. */
  117. div.title, caption.title {
  118. color: #527bbd;
  119. font-family: sans-serif;
  120. font-weight: bold;
  121. text-align: left;
  122. margin-top: 1.0em;
  123. margin-bottom: 0.5em;
  124. }
  125. div.title + * {
  126. margin-top: 0;
  127. }
  128. td div.title:first-child {
  129. margin-top: 0.0em;
  130. }
  131. div.content div.title:first-child {
  132. margin-top: 0.0em;
  133. }
  134. div.content + div.title {
  135. margin-top: 0.0em;
  136. }
  137. div.sidebarblock > div.content {
  138. background: #ffffee;
  139. border: 1px solid silver;
  140. padding: 0.5em;
  141. }
  142. div.listingblock > div.content {
  143. border: 1px solid silver;
  144. background: #f4f4f4;
  145. padding: 0.5em;
  146. }
  147. div.quoteblock {
  148. padding-left: 2.0em;
  149. margin-right: 10%;
  150. }
  151. div.quoteblock > div.attribution {
  152. padding-top: 0.5em;
  153. text-align: right;
  154. }
  155. div.verseblock {
  156. padding-left: 2.0em;
  157. margin-right: 10%;
  158. }
  159. div.verseblock > div.content {
  160. white-space: pre;
  161. }
  162. div.verseblock > div.attribution {
  163. padding-top: 0.75em;
  164. text-align: left;
  165. }
  166. /* DEPRECATED: Pre version 8.2.7 verse style literal block. */
  167. div.verseblock + div.attribution {
  168. text-align: left;
  169. }
  170. div.admonitionblock .icon {
  171. vertical-align: top;
  172. font-size: 1.1em;
  173. font-weight: bold;
  174. text-decoration: underline;
  175. color: #527bbd;
  176. padding-right: 0.5em;
  177. }
  178. div.admonitionblock td.content {
  179. padding-left: 0.5em;
  180. border-left: 2px solid silver;
  181. }
  182. div.exampleblock > div.content {
  183. border-left: 2px solid silver;
  184. padding: 0.5em;
  185. }
  186. div.imageblock div.content { padding-left: 0; }
  187. span.image img { border-style: none; }
  188. a.image:visited { color: white; }
  189. dl {
  190. margin-top: 0.8em;
  191. margin-bottom: 0.8em;
  192. }
  193. dt {
  194. margin-top: 0.5em;
  195. margin-bottom: 0;
  196. font-style: normal;
  197. color: navy;
  198. }
  199. dd > *:first-child {
  200. margin-top: 0.1em;
  201. }
  202. ul, ol {
  203. list-style-position: outside;
  204. }
  205. ol.arabic {
  206. list-style-type: decimal;
  207. }
  208. ol.loweralpha {
  209. list-style-type: lower-alpha;
  210. }
  211. ol.upperalpha {
  212. list-style-type: upper-alpha;
  213. }
  214. ol.lowerroman {
  215. list-style-type: lower-roman;
  216. }
  217. ol.upperroman {
  218. list-style-type: upper-roman;
  219. }
  220. div.compact ul, div.compact ol,
  221. div.compact p, div.compact p,
  222. div.compact div, div.compact div {
  223. margin-top: 0.1em;
  224. margin-bottom: 0.1em;
  225. }
  226. div.tableblock > table {
  227. border: 3px solid #527bbd;
  228. }
  229. thead {
  230. font-family: sans-serif;
  231. font-weight: bold;
  232. }
  233. tfoot {
  234. font-weight: bold;
  235. }
  236. td > div.verse {
  237. white-space: pre;
  238. }
  239. p.table {
  240. margin-top: 0;
  241. }
  242. /* Because the table frame attribute is overriden by CSS in most browsers. */
  243. div.tableblock > table[frame="void"] {
  244. border-style: none;
  245. }
  246. div.tableblock > table[frame="hsides"] {
  247. border-left-style: none;
  248. border-right-style: none;
  249. }
  250. div.tableblock > table[frame="vsides"] {
  251. border-top-style: none;
  252. border-bottom-style: none;
  253. }
  254. div.hdlist {
  255. margin-top: 0.8em;
  256. margin-bottom: 0.8em;
  257. }
  258. div.hdlist tr {
  259. padding-bottom: 15px;
  260. }
  261. dt.hdlist1.strong, td.hdlist1.strong {
  262. font-weight: bold;
  263. }
  264. td.hdlist1 {
  265. vertical-align: top;
  266. font-style: normal;
  267. padding-right: 0.8em;
  268. color: navy;
  269. }
  270. td.hdlist2 {
  271. vertical-align: top;
  272. }
  273. div.hdlist.compact tr {
  274. margin: 0;
  275. padding-bottom: 0;
  276. }
  277. .comment {
  278. background: yellow;
  279. }
  280. @media print {
  281. div#footer-badges { display: none; }
  282. }
  283. div#toctitle {
  284. color: #527bbd;
  285. font-family: sans-serif;
  286. font-size: 1.1em;
  287. font-weight: bold;
  288. margin-top: 1.0em;
  289. margin-bottom: 0.1em;
  290. }
  291. div.toclevel1, div.toclevel2, div.toclevel3, div.toclevel4 {
  292. margin-top: 0;
  293. margin-bottom: 0;
  294. }
  295. div.toclevel2 {
  296. margin-left: 2em;
  297. font-size: 0.9em;
  298. }
  299. div.toclevel3 {
  300. margin-left: 4em;
  301. font-size: 0.9em;
  302. }
  303. div.toclevel4 {
  304. margin-left: 6em;
  305. font-size: 0.9em;
  306. }
  307. /* Workarounds for IE6's broken and incomplete CSS2. */
  308. div.sidebar-content {
  309. background: #ffffee;
  310. border: 1px solid silver;
  311. padding: 0.5em;
  312. }
  313. div.sidebar-title, div.image-title {
  314. color: #527bbd;
  315. font-family: sans-serif;
  316. font-weight: bold;
  317. margin-top: 0.0em;
  318. margin-bottom: 0.5em;
  319. }
  320. div.listingblock div.content {
  321. border: 1px solid silver;
  322. background: #f4f4f4;
  323. padding: 0.5em;
  324. }
  325. div.quoteblock-attribution {
  326. padding-top: 0.5em;
  327. text-align: right;
  328. }
  329. div.verseblock-content {
  330. white-space: pre;
  331. }
  332. div.verseblock-attribution {
  333. padding-top: 0.75em;
  334. text-align: left;
  335. }
  336. div.exampleblock-content {
  337. border-left: 2px solid silver;
  338. padding-left: 0.5em;
  339. }
  340. /* IE6 sets dynamically generated links as visited. */
  341. div#toc a:visited { color: blue; }
  342. </style>
  343. <script type="text/javascript">
  344. /*<![CDATA[*/
  345. window.onload = function(){generateToc(2)}
  346. /* Author: Mihai Bazon, September 2002
  347. * http://students.infoiasi.ro/~mishoo
  348. *
  349. * Table Of Content generator
  350. * Version: 0.4
  351. *
  352. * Feel free to use this script under the terms of the GNU General Public
  353. * License, as long as you do not remove or alter this notice.
  354. */
  355. /* modified by Troy D. Hanson, September 2006. License: GPL */
  356. /* modified by Stuart Rackham, October 2006. License: GPL */
  357. function getText(el) {
  358. var text = "";
  359. for (var i = el.firstChild; i != null; i = i.nextSibling) {
  360. if (i.nodeType == 3 /* Node.TEXT_NODE */) // IE doesn't speak constants.
  361. text += i.data;
  362. else if (i.firstChild != null)
  363. text += getText(i);
  364. }
  365. return text;
  366. }
  367. function TocEntry(el, text, toclevel) {
  368. this.element = el;
  369. this.text = text;
  370. this.toclevel = toclevel;
  371. }
  372. function tocEntries(el, toclevels) {
  373. var result = new Array;
  374. var re = new RegExp('[hH]([2-'+(toclevels+1)+'])');
  375. // Function that scans the DOM tree for header elements (the DOM2
  376. // nodeIterator API would be a better technique but not supported by all
  377. // browsers).
  378. var iterate = function (el) {
  379. for (var i = el.firstChild; i != null; i = i.nextSibling) {
  380. if (i.nodeType == 1 /* Node.ELEMENT_NODE */) {
  381. var mo = re.exec(i.tagName)
  382. if (mo)
  383. result[result.length] = new TocEntry(i, getText(i), mo[1]-1);
  384. iterate(i);
  385. }
  386. }
  387. }
  388. iterate(el);
  389. return result;
  390. }
  391. // This function does the work. toclevels = 1..4.
  392. function generateToc(toclevels) {
  393. var toc = document.getElementById("toc");
  394. var entries = tocEntries(document.getElementsByTagName("body")[0], toclevels);
  395. for (var i = 0; i < entries.length; ++i) {
  396. var entry = entries[i];
  397. if (entry.element.id == "")
  398. entry.element.id = "toc" + i;
  399. var a = document.createElement("a");
  400. a.href = "#" + entry.element.id;
  401. a.appendChild(document.createTextNode(entry.text));
  402. var div = document.createElement("div");
  403. div.appendChild(a);
  404. div.className = "toclevel" + entry.toclevel;
  405. toc.appendChild(div);
  406. }
  407. if (entries.length == 0)
  408. document.getElementById("header").removeChild(toc);
  409. }
  410. /*]]>*/
  411. </script>
  412. </head>
  413. <body>
  414. <div id="header">
  415. <h1>Lua CJSON 2.1.0 Manual</h1>
  416. <span id="author">Mark Pulford</span><br />
  417. <span id="email"><tt>&lt;<a href="mailto:mark@kyne.com.au">mark@kyne.com.au</a>&gt;</tt></span><br />
  418. <span id="revdate">1st March 2012</span>
  419. <div id="toc">
  420. <div id="toctitle">Table of Contents</div>
  421. <noscript><p><b>JavaScript must be enabled in your browser to display the table of contents.</b></p></noscript>
  422. </div>
  423. </div>
  424. <h2 id="_overview">1. Overview</h2>
  425. <div class="sectionbody">
  426. <div class="paragraph"><p>The Lua CJSON module provides JSON support for Lua.</p></div>
  427. <div class="dlist"><dl>
  428. <dt class="hdlist1">
  429. <strong>Features</strong>
  430. </dt>
  431. <dd>
  432. <div class="ulist"><ul>
  433. <li>
  434. <p>
  435. Fast, standards compliant encoding/parsing routines
  436. </p>
  437. </li>
  438. <li>
  439. <p>
  440. Full support for JSON with UTF-8, including decoding surrogate pairs
  441. </p>
  442. </li>
  443. <li>
  444. <p>
  445. Optional run-time support for common exceptions to the JSON
  446. specification (infinity, NaN,..)
  447. </p>
  448. </li>
  449. <li>
  450. <p>
  451. No dependencies on other libraries
  452. </p>
  453. </li>
  454. </ul></div>
  455. </dd>
  456. <dt class="hdlist1">
  457. <strong>Caveats</strong>
  458. </dt>
  459. <dd>
  460. <div class="ulist"><ul>
  461. <li>
  462. <p>
  463. UTF-16 and UTF-32 are not supported
  464. </p>
  465. </li>
  466. </ul></div>
  467. </dd>
  468. </dl></div>
  469. <div class="paragraph"><p>Lua CJSON is covered by the MIT license. Review the file <tt>LICENSE</tt> for
  470. details.</p></div>
  471. <div class="paragraph"><p>The latest version of this software is available from the
  472. <a href="http://www.kyne.com.au/%7Emark/software/lua-cjson.php">Lua CJSON website</a>.</p></div>
  473. <div class="paragraph"><p>Feel free to email me if you have any patches, suggestions, or comments.</p></div>
  474. </div>
  475. <h2 id="_installation">2. Installation</h2>
  476. <div class="sectionbody">
  477. <div class="paragraph"><p>Lua CJSON requires either <a href="http://www.lua.org">Lua</a> 5.1, Lua 5.2, or
  478. <a href="http://www.luajit.org">LuaJIT</a> to build.</p></div>
  479. <div class="paragraph"><p>The build method can be selected from 4 options:</p></div>
  480. <div class="dlist"><dl>
  481. <dt class="hdlist1">
  482. Make
  483. </dt>
  484. <dd>
  485. <p>
  486. Unix (including Linux, BSD, Mac OSX &amp; Solaris), Windows
  487. </p>
  488. </dd>
  489. <dt class="hdlist1">
  490. CMake
  491. </dt>
  492. <dd>
  493. <p>
  494. Unix, Windows
  495. </p>
  496. </dd>
  497. <dt class="hdlist1">
  498. RPM
  499. </dt>
  500. <dd>
  501. <p>
  502. Linux
  503. </p>
  504. </dd>
  505. <dt class="hdlist1">
  506. LuaRocks
  507. </dt>
  508. <dd>
  509. <p>
  510. Unix, Windows
  511. </p>
  512. </dd>
  513. </dl></div>
  514. <h3 id="_make">2.1. Make</h3><div style="clear:left"></div>
  515. <div class="paragraph"><p>The included <tt>Makefile</tt> has generic settings.</p></div>
  516. <div class="paragraph"><p>First, review and update the included makefile to suit your platform (if
  517. required).</p></div>
  518. <div class="paragraph"><p>Next, build and install the module:</p></div>
  519. <div class="exampleblock">
  520. <div class="exampleblock-content"><!-- Generator: GNU source-highlight 3.1.4
  521. by Lorenzo Bettini
  522. http://www.lorenzobettini.it
  523. http://www.gnu.org/software/src-highlite -->
  524. <pre><tt>make install</tt></pre></div></div>
  525. <div class="paragraph"><p>Or install manually into your Lua module directory:</p></div>
  526. <div class="exampleblock">
  527. <div class="exampleblock-content"><!-- Generator: GNU source-highlight 3.1.4
  528. by Lorenzo Bettini
  529. http://www.lorenzobettini.it
  530. http://www.gnu.org/software/src-highlite -->
  531. <pre><tt>make
  532. cp cjson<span style="color: #990000">.</span>so <span style="color: #009900">$LUA_MODULE_DIRECTORY</span></tt></pre></div></div>
  533. <h3 id="_cmake">2.2. CMake</h3><div style="clear:left"></div>
  534. <div class="paragraph"><p><a href="http://www.cmake.org">CMake</a> can generate build configuration for many
  535. different platforms (including Unix and Windows).</p></div>
  536. <div class="paragraph"><p>First, generate the makefile for your platform using CMake. If CMake is
  537. unable to find Lua, manually set the <tt>LUA_DIR</tt> environment variable to
  538. the base prefix of your Lua 5.1 installation.</p></div>
  539. <div class="paragraph"><p>While <tt>cmake</tt> is used in the example below, <tt>ccmake</tt> or <tt>cmake-gui</tt> may
  540. be used to present an interface for changing the default build options.</p></div>
  541. <div class="exampleblock">
  542. <div class="exampleblock-content"><!-- Generator: GNU source-highlight 3.1.4
  543. by Lorenzo Bettini
  544. http://www.lorenzobettini.it
  545. http://www.gnu.org/software/src-highlite -->
  546. <pre><tt>mkdir build
  547. cd build
  548. <span style="font-style: italic"><span style="color: #9A1900"># Optional: export LUA_DIR=$LUA51_PREFIX</span></span>
  549. cmake <span style="color: #990000">..</span></tt></pre></div></div>
  550. <div class="paragraph"><p>Next, build and install the module:</p></div>
  551. <div class="exampleblock">
  552. <div class="exampleblock-content"><!-- Generator: GNU source-highlight 3.1.4
  553. by Lorenzo Bettini
  554. http://www.lorenzobettini.it
  555. http://www.gnu.org/software/src-highlite -->
  556. <pre><tt>make install
  557. <span style="font-style: italic"><span style="color: #9A1900"># Or:</span></span>
  558. make
  559. cp cjson<span style="color: #990000">.</span>so <span style="color: #009900">$LUA_MODULE_DIRECTORY</span></tt></pre></div></div>
  560. <div class="paragraph"><p>Review the
  561. <a href="http://www.cmake.org/cmake/help/documentation.html">CMake documentation</a>
  562. for further details.</p></div>
  563. <h3 id="_rpm">2.3. RPM</h3><div style="clear:left"></div>
  564. <div class="paragraph"><p>Linux distributions using <a href="http://rpm.org">RPM</a> can create a package via
  565. the included RPM spec file. Ensure the <tt>rpm-build</tt> package (or similar)
  566. has been installed.</p></div>
  567. <div class="paragraph"><p>Build and install the module via RPM:</p></div>
  568. <div class="exampleblock">
  569. <div class="exampleblock-content"><!-- Generator: GNU source-highlight 3.1.4
  570. by Lorenzo Bettini
  571. http://www.lorenzobettini.it
  572. http://www.gnu.org/software/src-highlite -->
  573. <pre><tt>rpmbuild -tb lua-cjson-<span style="color: #993399">2.1</span><span style="color: #990000">.</span><span style="color: #993399">0</span><span style="color: #990000">.</span>tar<span style="color: #990000">.</span>gz
  574. rpm -Uvh <span style="color: #009900">$LUA_CJSON_RPM</span></tt></pre></div></div>
  575. <h3 id="_luarocks">2.4. LuaRocks</h3><div style="clear:left"></div>
  576. <div class="paragraph"><p><a href="http://luarocks.org">LuaRocks</a> can be used to install and manage Lua
  577. modules on a wide range of platforms (including Windows).</p></div>
  578. <div class="paragraph"><p>First, extract the Lua CJSON source package.</p></div>
  579. <div class="paragraph"><p>Next, install the module:</p></div>
  580. <div class="exampleblock">
  581. <div class="exampleblock-content"><!-- Generator: GNU source-highlight 3.1.4
  582. by Lorenzo Bettini
  583. http://www.lorenzobettini.it
  584. http://www.gnu.org/software/src-highlite -->
  585. <pre><tt>cd lua-cjson-<span style="color: #993399">2.1</span><span style="color: #990000">.</span><span style="color: #993399">0</span>
  586. luarocks make</tt></pre></div></div>
  587. <div class="admonitionblock">
  588. <table><tr>
  589. <td class="icon">
  590. <div class="title">Note</div>
  591. </td>
  592. <td class="content">LuaRocks does not support platform specific configuration for Solaris.
  593. On Solaris, you may need to manually uncomment <tt>USE_INTERNAL_ISINF</tt> in
  594. the rockspec before building this module.</td>
  595. </tr></table>
  596. </div>
  597. <div class="paragraph"><p>Review the <a href="http://luarocks.org/en/Documentation">LuaRocks documentation</a>
  598. for further details.</p></div>
  599. <h3 id="build_options">2.5. Build Options (#define)</h3><div style="clear:left"></div>
  600. <div class="paragraph"><p>Lua CJSON offers several <tt>#define</tt> build options to address portability
  601. issues, and enable non-default features. Some build methods may
  602. automatically set platform specific options if required. Other features
  603. should be enabled manually.</p></div>
  604. <div class="dlist"><dl>
  605. <dt class="hdlist1">
  606. USE_INTERNAL_ISINF
  607. </dt>
  608. <dd>
  609. <p>
  610. Workaround for Solaris platforms missing <tt>isinf</tt>.
  611. </p>
  612. </dd>
  613. <dt class="hdlist1">
  614. DISABLE_INVALID_NUMBERS
  615. </dt>
  616. <dd>
  617. <p>
  618. Recommended on platforms where <tt>strtod</tt> /
  619. <tt>sprintf</tt> are not POSIX compliant (eg, Windows MinGW). Prevents
  620. <tt>cjson.encode_invalid_numbers</tt> and <tt>cjson.decode_invalid_numbers</tt> from
  621. being enabled. However, <tt>cjson.encode_invalid_numbers</tt> may still be
  622. set to <tt>"null"</tt>. When using the Lua CJSON built-in floating point
  623. conversion this option is unnecessary and is ignored.
  624. </p>
  625. </dd>
  626. </dl></div>
  627. <h4 id="_built_in_floating_point_conversion">2.5.1. Built-in floating point conversion</h4>
  628. <div class="paragraph"><p>Lua CJSON may be built with David Gay&#8217;s
  629. <a href="http://www.netlib.org/fp/">floating point conversion routines</a>. This can
  630. increase overall performance by up to 50% on some platforms when
  631. converting a large amount of numeric data. However, this option reduces
  632. portability and is disabled by default.</p></div>
  633. <div class="dlist"><dl>
  634. <dt class="hdlist1">
  635. USE_INTERNAL_FPCONV
  636. </dt>
  637. <dd>
  638. <p>
  639. Enable internal number conversion routines.
  640. </p>
  641. </dd>
  642. <dt class="hdlist1">
  643. IEEE_BIG_ENDIAN
  644. </dt>
  645. <dd>
  646. <p>
  647. Must be set on big endian architectures.
  648. </p>
  649. </dd>
  650. <dt class="hdlist1">
  651. MULTIPLE_THREADS
  652. </dt>
  653. <dd>
  654. <p>
  655. Must be set if Lua CJSON may be used in a
  656. multi-threaded application. Requires the <em>pthreads</em> library.
  657. </p>
  658. </dd>
  659. </dl></div>
  660. </div>
  661. <h2 id="_api_functions">3. API (Functions)</h2>
  662. <div class="sectionbody">
  663. <h3 id="_synopsis">3.1. Synopsis</h3><div style="clear:left"></div>
  664. <div class="listingblock">
  665. <div class="content"><!-- Generator: GNU source-highlight 3.1.4
  666. by Lorenzo Bettini
  667. http://www.lorenzobettini.it
  668. http://www.gnu.org/software/src-highlite -->
  669. <pre><tt><span style="font-style: italic"><span style="color: #9A1900">-- Module instantiation</span></span>
  670. <span style="font-weight: bold"><span style="color: #0000FF">local</span></span> cjson <span style="color: #990000">=</span> require <span style="color: #FF0000">"cjson"</span>
  671. <span style="font-weight: bold"><span style="color: #0000FF">local</span></span> cjson2 <span style="color: #990000">=</span> cjson<span style="color: #990000">.</span><span style="font-weight: bold"><span style="color: #000000">new</span></span><span style="color: #990000">()</span>
  672. <span style="font-weight: bold"><span style="color: #0000FF">local</span></span> cjson_safe <span style="color: #990000">=</span> require <span style="color: #FF0000">"cjson.safe"</span>
  673. <span style="font-style: italic"><span style="color: #9A1900">-- Translate Lua value to/from JSON</span></span>
  674. text <span style="color: #990000">=</span> cjson<span style="color: #990000">.</span><span style="font-weight: bold"><span style="color: #000000">encode</span></span><span style="color: #990000">(</span>value<span style="color: #990000">)</span>
  675. value <span style="color: #990000">=</span> cjson<span style="color: #990000">.</span><span style="font-weight: bold"><span style="color: #000000">decode</span></span><span style="color: #990000">(</span>text<span style="color: #990000">)</span>
  676. <span style="font-style: italic"><span style="color: #9A1900">-- Get and/or set Lua CJSON configuration</span></span>
  677. setting <span style="color: #990000">=</span> cjson<span style="color: #990000">.</span><span style="font-weight: bold"><span style="color: #000000">decode_invalid_numbers</span></span><span style="color: #990000">([</span>setting<span style="color: #990000">])</span>
  678. setting <span style="color: #990000">=</span> cjson<span style="color: #990000">.</span><span style="font-weight: bold"><span style="color: #000000">encode_invalid_numbers</span></span><span style="color: #990000">([</span>setting<span style="color: #990000">])</span>
  679. keep <span style="color: #990000">=</span> cjson<span style="color: #990000">.</span><span style="font-weight: bold"><span style="color: #000000">encode_keep_buffer</span></span><span style="color: #990000">([</span>keep<span style="color: #990000">])</span>
  680. depth <span style="color: #990000">=</span> cjson<span style="color: #990000">.</span><span style="font-weight: bold"><span style="color: #000000">encode_max_depth</span></span><span style="color: #990000">([</span>depth<span style="color: #990000">])</span>
  681. depth <span style="color: #990000">=</span> cjson<span style="color: #990000">.</span><span style="font-weight: bold"><span style="color: #000000">decode_max_depth</span></span><span style="color: #990000">([</span>depth<span style="color: #990000">])</span>
  682. convert<span style="color: #990000">,</span> ratio<span style="color: #990000">,</span> safe <span style="color: #990000">=</span> cjson<span style="color: #990000">.</span><span style="font-weight: bold"><span style="color: #000000">encode_sparse_array</span></span><span style="color: #990000">([</span>convert<span style="color: #990000">[,</span> ratio<span style="color: #990000">[,</span> safe<span style="color: #990000">]]])</span></tt></pre></div></div>
  683. <h3 id="_module_instantiation">3.2. Module Instantiation</h3><div style="clear:left"></div>
  684. <div class="listingblock">
  685. <div class="content"><!-- Generator: GNU source-highlight 3.1.4
  686. by Lorenzo Bettini
  687. http://www.lorenzobettini.it
  688. http://www.gnu.org/software/src-highlite -->
  689. <pre><tt><span style="font-weight: bold"><span style="color: #0000FF">local</span></span> cjson <span style="color: #990000">=</span> require <span style="color: #FF0000">"cjson"</span>
  690. <span style="font-weight: bold"><span style="color: #0000FF">local</span></span> cjson2 <span style="color: #990000">=</span> cjson<span style="color: #990000">.</span><span style="font-weight: bold"><span style="color: #000000">new</span></span><span style="color: #990000">()</span>
  691. <span style="font-weight: bold"><span style="color: #0000FF">local</span></span> cjson_safe <span style="color: #990000">=</span> require <span style="color: #FF0000">"cjson.safe"</span></tt></pre></div></div>
  692. <div class="paragraph"><p>Import Lua CJSON via the Lua <tt>require</tt> function. Lua CJSON does not
  693. register a global module table.</p></div>
  694. <div class="paragraph"><p>The <tt>cjson</tt> module will throw an error during JSON conversion if any
  695. invalid data is encountered. Refer to <a href="#cjson_encode"><tt>cjson.encode</tt></a>
  696. and <a href="#cjson_decode"><tt>cjson.decode</tt></a> for details.</p></div>
  697. <div class="paragraph"><p>The <tt>cjson.safe</tt> module behaves identically to the <tt>cjson</tt> module,
  698. except when errors are encountered during JSON conversion. On error, the
  699. <tt>cjson_safe.encode</tt> and <tt>cjson_safe.decode</tt> functions will return
  700. <tt>nil</tt> followed by the error message.</p></div>
  701. <div class="paragraph"><p><tt>cjson.new</tt> can be used to instantiate an independent copy of the Lua
  702. CJSON module. The new module has a separate persistent encoding buffer,
  703. and default settings.</p></div>
  704. <div class="paragraph"><p>Lua CJSON can support Lua implementations using multiple preemptive
  705. threads within a single Lua state provided the persistent encoding
  706. buffer is not shared. This can be achieved by one of the following
  707. methods:</p></div>
  708. <div class="ulist"><ul>
  709. <li>
  710. <p>
  711. Disabling the persistent encoding buffer with
  712. <a href="#encode_keep_buffer"><tt>cjson.encode_keep_buffer</tt></a>
  713. </p>
  714. </li>
  715. <li>
  716. <p>
  717. Ensuring each thread calls <a href="#encode"><tt>cjson.encode</tt></a> separately (ie,
  718. treat <tt>cjson.encode</tt> as non-reentrant).
  719. </p>
  720. </li>
  721. <li>
  722. <p>
  723. Using a separate <tt>cjson</tt> module table per preemptive thread
  724. (<tt>cjson.new</tt>)
  725. </p>
  726. </li>
  727. </ul></div>
  728. <div class="admonitionblock">
  729. <table><tr>
  730. <td class="icon">
  731. <div class="title">Note</div>
  732. </td>
  733. <td class="content">Lua CJSON uses <tt>strtod</tt> and <tt>snprintf</tt> to perform numeric conversion as
  734. they are usually well supported, fast and bug free. However, these
  735. functions require a workaround for JSON encoding/parsing under locales
  736. using a comma decimal separator. Lua CJSON detects the current locale
  737. during instantiation to determine and automatically implement the
  738. workaround if required. Lua CJSON should be reinitialised via
  739. <tt>cjson.new</tt> if the locale of the current process changes. Using a
  740. different locale per thread is not supported.</td>
  741. </tr></table>
  742. </div>
  743. <h3 id="_decode">3.3. decode</h3><div style="clear:left"></div>
  744. <div class="listingblock">
  745. <div class="content"><!-- Generator: GNU source-highlight 3.1.4
  746. by Lorenzo Bettini
  747. http://www.lorenzobettini.it
  748. http://www.gnu.org/software/src-highlite -->
  749. <pre><tt>value <span style="color: #990000">=</span> cjson<span style="color: #990000">.</span><span style="font-weight: bold"><span style="color: #000000">decode</span></span><span style="color: #990000">(</span>json_text<span style="color: #990000">)</span></tt></pre></div></div>
  750. <div class="paragraph"><p><tt>cjson.decode</tt> will deserialise any UTF-8 JSON string into a Lua value
  751. or table.</p></div>
  752. <div class="paragraph"><p>UTF-16 and UTF-32 JSON strings are not supported.</p></div>
  753. <div class="paragraph"><p><tt>cjson.decode</tt> requires that any NULL (ASCII 0) and double quote (ASCII
  754. 34) characters are escaped within strings. All escape codes will be
  755. decoded and other bytes will be passed transparently. UTF-8 characters
  756. are not validated during decoding and should be checked elsewhere if
  757. required.</p></div>
  758. <div class="paragraph"><p>JSON <tt>null</tt> will be converted to a NULL <tt>lightuserdata</tt> value. This can
  759. be compared with <tt>cjson.null</tt> for convenience.</p></div>
  760. <div class="paragraph"><p>By default, numbers incompatible with the JSON specification (infinity,
  761. NaN, hexadecimal) can be decoded. This default can be changed with
  762. <a href="#decode_invalid_numbers"><tt>cjson.decode_invalid_numbers</tt></a>.</p></div>
  763. <div class="exampleblock">
  764. <div class="title">Example: Decoding</div>
  765. <div class="exampleblock-content"><!-- Generator: GNU source-highlight 3.1.4
  766. by Lorenzo Bettini
  767. http://www.lorenzobettini.it
  768. http://www.gnu.org/software/src-highlite -->
  769. <pre><tt>json_text <span style="color: #990000">=</span> <span style="color: #FF0000">'[ true, { "foo": "bar" } ]'</span>
  770. value <span style="color: #990000">=</span> cjson<span style="color: #990000">.</span><span style="font-weight: bold"><span style="color: #000000">decode</span></span><span style="color: #990000">(</span>json_text<span style="color: #990000">)</span>
  771. <span style="font-style: italic"><span style="color: #9A1900">-- Returns: { true, { foo = "bar" } }</span></span></tt></pre></div></div>
  772. <div class="admonitionblock">
  773. <table><tr>
  774. <td class="icon">
  775. <div class="title">Caution</div>
  776. </td>
  777. <td class="content">Care must be taken after decoding JSON objects with numeric keys. Each
  778. numeric key will be stored as a Lua <tt>string</tt>. Any subsequent code
  779. assuming type <tt>number</tt> may break.</td>
  780. </tr></table>
  781. </div>
  782. <h3 id="decode_invalid_numbers">3.4. decode_invalid_numbers</h3><div style="clear:left"></div>
  783. <div class="listingblock">
  784. <div class="content"><!-- Generator: GNU source-highlight 3.1.4
  785. by Lorenzo Bettini
  786. http://www.lorenzobettini.it
  787. http://www.gnu.org/software/src-highlite -->
  788. <pre><tt>setting <span style="color: #990000">=</span> cjson<span style="color: #990000">.</span><span style="font-weight: bold"><span style="color: #000000">decode_invalid_numbers</span></span><span style="color: #990000">([</span>setting<span style="color: #990000">])</span>
  789. <span style="font-style: italic"><span style="color: #9A1900">-- "setting" must be a boolean. Default: true.</span></span></tt></pre></div></div>
  790. <div class="paragraph"><p>Lua CJSON may generate an error when trying to decode numbers not
  791. supported by the JSON specification. <em>Invalid numbers</em> are defined as:</p></div>
  792. <div class="ulist"><ul>
  793. <li>
  794. <p>
  795. infinity
  796. </p>
  797. </li>
  798. <li>
  799. <p>
  800. not-a-number (NaN)
  801. </p>
  802. </li>
  803. <li>
  804. <p>
  805. hexadecimal
  806. </p>
  807. </li>
  808. </ul></div>
  809. <div class="paragraph"><p>Available settings:</p></div>
  810. <div class="dlist"><dl>
  811. <dt class="hdlist1">
  812. <tt>true</tt>
  813. </dt>
  814. <dd>
  815. <p>
  816. Accept and decode <em>invalid numbers</em>. This is the default
  817. setting.
  818. </p>
  819. </dd>
  820. <dt class="hdlist1">
  821. <tt>false</tt>
  822. </dt>
  823. <dd>
  824. <p>
  825. Throw an error when <em>invalid numbers</em> are encountered.
  826. </p>
  827. </dd>
  828. </dl></div>
  829. <div class="paragraph"><p>The current setting is always returned, and is only updated when an
  830. argument is provided.</p></div>
  831. <h3 id="decode_max_depth">3.5. decode_max_depth</h3><div style="clear:left"></div>
  832. <div class="listingblock">
  833. <div class="content"><!-- Generator: GNU source-highlight 3.1.4
  834. by Lorenzo Bettini
  835. http://www.lorenzobettini.it
  836. http://www.gnu.org/software/src-highlite -->
  837. <pre><tt>depth <span style="color: #990000">=</span> cjson<span style="color: #990000">.</span><span style="font-weight: bold"><span style="color: #000000">decode_max_depth</span></span><span style="color: #990000">([</span>depth<span style="color: #990000">])</span>
  838. <span style="font-style: italic"><span style="color: #9A1900">-- "depth" must be a positive integer. Default: 1000.</span></span></tt></pre></div></div>
  839. <div class="paragraph"><p>Lua CJSON will generate an error when parsing deeply nested JSON once
  840. the maximum array/object depth has been exceeded. This check prevents
  841. unnecessarily complicated JSON from slowing down the application, or
  842. crashing the application due to lack of process stack space.</p></div>
  843. <div class="paragraph"><p>An error may be generated before the depth limit is hit if Lua is unable
  844. to allocate more objects on the Lua stack.</p></div>
  845. <div class="paragraph"><p>By default, Lua CJSON will reject JSON with arrays and/or objects nested
  846. more than 1000 levels deep.</p></div>
  847. <div class="paragraph"><p>The current setting is always returned, and is only updated when an
  848. argument is provided.</p></div>
  849. <h3 id="encode">3.6. encode</h3><div style="clear:left"></div>
  850. <div class="listingblock">
  851. <div class="content"><!-- Generator: GNU source-highlight 3.1.4
  852. by Lorenzo Bettini
  853. http://www.lorenzobettini.it
  854. http://www.gnu.org/software/src-highlite -->
  855. <pre><tt>json_text <span style="color: #990000">=</span> cjson<span style="color: #990000">.</span><span style="font-weight: bold"><span style="color: #000000">encode</span></span><span style="color: #990000">(</span>value<span style="color: #990000">)</span></tt></pre></div></div>
  856. <div class="paragraph"><p><tt>cjson.encode</tt> will serialise a Lua value into a string containing the
  857. JSON representation.</p></div>
  858. <div class="paragraph"><p><tt>cjson.encode</tt> supports the following types:</p></div>
  859. <div class="ulist"><ul>
  860. <li>
  861. <p>
  862. <tt>boolean</tt>
  863. </p>
  864. </li>
  865. <li>
  866. <p>
  867. <tt>lightuserdata</tt> (NULL value only)
  868. </p>
  869. </li>
  870. <li>
  871. <p>
  872. <tt>nil</tt>
  873. </p>
  874. </li>
  875. <li>
  876. <p>
  877. <tt>number</tt>
  878. </p>
  879. </li>
  880. <li>
  881. <p>
  882. <tt>string</tt>
  883. </p>
  884. </li>
  885. <li>
  886. <p>
  887. <tt>table</tt>
  888. </p>
  889. </li>
  890. </ul></div>
  891. <div class="paragraph"><p>The remaining Lua types will generate an error:</p></div>
  892. <div class="ulist"><ul>
  893. <li>
  894. <p>
  895. <tt>function</tt>
  896. </p>
  897. </li>
  898. <li>
  899. <p>
  900. <tt>lightuserdata</tt> (non-NULL values)
  901. </p>
  902. </li>
  903. <li>
  904. <p>
  905. <tt>thread</tt>
  906. </p>
  907. </li>
  908. <li>
  909. <p>
  910. <tt>userdata</tt>
  911. </p>
  912. </li>
  913. </ul></div>
  914. <div class="paragraph"><p>By default, numbers are encoded with 14 significant digits. Refer to
  915. <a href="#encode_number_precision"><tt>cjson.encode_number_precision</tt></a> for details.</p></div>
  916. <div class="paragraph"><p>Lua CJSON will escape the following characters within each UTF-8 string:</p></div>
  917. <div class="ulist"><ul>
  918. <li>
  919. <p>
  920. Control characters (ASCII 0 - 31)
  921. </p>
  922. </li>
  923. <li>
  924. <p>
  925. Double quote (ASCII 34)
  926. </p>
  927. </li>
  928. <li>
  929. <p>
  930. Forward slash (ASCII 47)
  931. </p>
  932. </li>
  933. <li>
  934. <p>
  935. Blackslash (ASCII 92)
  936. </p>
  937. </li>
  938. <li>
  939. <p>
  940. Delete (ASCII 127)
  941. </p>
  942. </li>
  943. </ul></div>
  944. <div class="paragraph"><p>All other bytes are passed transparently.</p></div>
  945. <div class="admonitionblock">
  946. <table><tr>
  947. <td class="icon">
  948. <div class="title">Caution</div>
  949. </td>
  950. <td class="content">
  951. <div class="paragraph"><p>Lua CJSON will successfully encode/decode binary strings, but this is
  952. technically not supported by JSON and may not be compatible with other
  953. JSON libraries. To ensure the output is valid JSON, applications should
  954. ensure all Lua strings passed to <tt>cjson.encode</tt> are UTF-8.</p></div>
  955. <div class="paragraph"><p>Base64 is commonly used to encode binary data as the most efficient
  956. encoding under UTF-8 can only reduce the encoded size by a further
  957. &#126;8%. Lua Base64 routines can be found in the
  958. <a href="http://w3.impa.br/%7Ediego/software/luasocket/">LuaSocket</a> and
  959. <a href="http://www.tecgraf.puc-rio.br/%7Elhf/ftp/lua/#lbase64">lbase64</a> packages.</p></div>
  960. </td>
  961. </tr></table>
  962. </div>
  963. <div class="paragraph"><p>Lua CJSON uses a heuristic to determine whether to encode a Lua table as
  964. a JSON array or an object. A Lua table with only positive integer keys
  965. of type <tt>number</tt> will be encoded as a JSON array. All other tables will
  966. be encoded as a JSON object.</p></div>
  967. <div class="paragraph"><p>Lua CJSON does not use metamethods when serialising tables.</p></div>
  968. <div class="ulist"><ul>
  969. <li>
  970. <p>
  971. <tt>rawget</tt> is used to iterate over Lua arrays
  972. </p>
  973. </li>
  974. <li>
  975. <p>
  976. <tt>next</tt> is used to iterate over Lua objects
  977. </p>
  978. </li>
  979. </ul></div>
  980. <div class="paragraph"><p>Lua arrays with missing entries (<em>sparse arrays</em>) may optionally be
  981. encoded in several different ways. Refer to
  982. <a href="#encode_sparse_array"><tt>cjson.encode_sparse_array</tt></a> for details.</p></div>
  983. <div class="paragraph"><p>JSON object keys are always strings. Hence <tt>cjson.encode</tt> only supports
  984. table keys which are type <tt>number</tt> or <tt>string</tt>. All other types will
  985. generate an error.</p></div>
  986. <div class="admonitionblock">
  987. <table><tr>
  988. <td class="icon">
  989. <div class="title">Note</div>
  990. </td>
  991. <td class="content">Standards compliant JSON must be encapsulated in either an object (<tt>{}</tt>)
  992. or an array (<tt>[]</tt>). If strictly standards compliant JSON is desired, a
  993. table must be passed to <tt>cjson.encode</tt>.</td>
  994. </tr></table>
  995. </div>
  996. <div class="paragraph"><p>By default, encoding the following Lua values will generate errors:</p></div>
  997. <div class="ulist"><ul>
  998. <li>
  999. <p>
  1000. Numbers incompatible with the JSON specification (infinity, NaN)
  1001. </p>
  1002. </li>
  1003. <li>
  1004. <p>
  1005. Tables nested more than 1000 levels deep
  1006. </p>
  1007. </li>
  1008. <li>
  1009. <p>
  1010. Excessively sparse Lua arrays
  1011. </p>
  1012. </li>
  1013. </ul></div>
  1014. <div class="paragraph"><p>These defaults can be changed with:</p></div>
  1015. <div class="ulist"><ul>
  1016. <li>
  1017. <p>
  1018. <a href="#encode_invalid_numbers"><tt>cjson.encode_invalid_numbers</tt></a>
  1019. </p>
  1020. </li>
  1021. <li>
  1022. <p>
  1023. <a href="#encode_max_depth"><tt>cjson.encode_max_depth</tt></a>
  1024. </p>
  1025. </li>
  1026. <li>
  1027. <p>
  1028. <a href="#encode_sparse_array"><tt>cjson.encode_sparse_array</tt></a>
  1029. </p>
  1030. </li>
  1031. </ul></div>
  1032. <div class="exampleblock">
  1033. <div class="title">Example: Encoding</div>
  1034. <div class="exampleblock-content"><!-- Generator: GNU source-highlight 3.1.4
  1035. by Lorenzo Bettini
  1036. http://www.lorenzobettini.it
  1037. http://www.gnu.org/software/src-highlite -->
  1038. <pre><tt>value <span style="color: #990000">=</span> <span style="color: #FF0000">{</span> <span style="font-weight: bold"><span style="color: #0000FF">true</span></span><span style="color: #990000">,</span> <span style="color: #FF0000">{</span> foo <span style="color: #990000">=</span> <span style="color: #FF0000">"bar"</span> <span style="color: #FF0000">}</span> <span style="color: #FF0000">}</span>
  1039. json_text <span style="color: #990000">=</span> cjson<span style="color: #990000">.</span><span style="font-weight: bold"><span style="color: #000000">encode</span></span><span style="color: #990000">(</span>value<span style="color: #990000">)</span>
  1040. <span style="font-style: italic"><span style="color: #9A1900">-- Returns: '[true,{"foo":"bar"}]'</span></span></tt></pre></div></div>
  1041. <h3 id="encode_invalid_numbers">3.7. encode_invalid_numbers</h3><div style="clear:left"></div>
  1042. <div class="listingblock">
  1043. <div class="content"><!-- Generator: GNU source-highlight 3.1.4
  1044. by Lorenzo Bettini
  1045. http://www.lorenzobettini.it
  1046. http://www.gnu.org/software/src-highlite -->
  1047. <pre><tt>setting <span style="color: #990000">=</span> cjson<span style="color: #990000">.</span><span style="font-weight: bold"><span style="color: #000000">encode_invalid_numbers</span></span><span style="color: #990000">([</span>setting<span style="color: #990000">])</span>
  1048. <span style="font-style: italic"><span style="color: #9A1900">-- "setting" must a boolean or "null". Default: false.</span></span></tt></pre></div></div>
  1049. <div class="paragraph"><p>Lua CJSON may generate an error when encoding floating point numbers not
  1050. supported by the JSON specification (<em>invalid numbers</em>):</p></div>
  1051. <div class="ulist"><ul>
  1052. <li>
  1053. <p>
  1054. infinity
  1055. </p>
  1056. </li>
  1057. <li>
  1058. <p>
  1059. not-a-number (NaN)
  1060. </p>
  1061. </li>
  1062. </ul></div>
  1063. <div class="paragraph"><p>Available settings:</p></div>
  1064. <div class="dlist"><dl>
  1065. <dt class="hdlist1">
  1066. <tt>true</tt>
  1067. </dt>
  1068. <dd>
  1069. <p>
  1070. Allow <em>invalid numbers</em> to be encoded. This will generate
  1071. non-standard JSON, but this output is supported by some libraries.
  1072. </p>
  1073. </dd>
  1074. <dt class="hdlist1">
  1075. <tt>"null"</tt>
  1076. </dt>
  1077. <dd>
  1078. <p>
  1079. Encode <em>invalid numbers</em> as a JSON <tt>null</tt> value. This allows
  1080. infinity and NaN to be encoded into valid JSON.
  1081. </p>
  1082. </dd>
  1083. <dt class="hdlist1">
  1084. <tt>false</tt>
  1085. </dt>
  1086. <dd>
  1087. <p>
  1088. Throw an error when attempting to encode <em>invalid numbers</em>.
  1089. This is the default setting.
  1090. </p>
  1091. </dd>
  1092. </dl></div>
  1093. <div class="paragraph"><p>The current setting is always returned, and is only updated when an
  1094. argument is provided.</p></div>
  1095. <h3 id="encode_keep_buffer">3.8. encode_keep_buffer</h3><div style="clear:left"></div>
  1096. <div class="listingblock">
  1097. <div class="content"><!-- Generator: GNU source-highlight 3.1.4
  1098. by Lorenzo Bettini
  1099. http://www.lorenzobettini.it
  1100. http://www.gnu.org/software/src-highlite -->
  1101. <pre><tt>keep <span style="color: #990000">=</span> cjson<span style="color: #990000">.</span><span style="font-weight: bold"><span style="color: #000000">encode_keep_buffer</span></span><span style="color: #990000">([</span>keep<span style="color: #990000">])</span>
  1102. <span style="font-style: italic"><span style="color: #9A1900">-- "keep" must be a boolean. Default: true.</span></span></tt></pre></div></div>
  1103. <div class="paragraph"><p>Lua CJSON can reuse the JSON encoding buffer to improve performance.</p></div>
  1104. <div class="paragraph"><p>Available settings:</p></div>
  1105. <div class="dlist"><dl>
  1106. <dt class="hdlist1">
  1107. <tt>true</tt>
  1108. </dt>
  1109. <dd>
  1110. <p>
  1111. The buffer will grow to the largest size required and is not
  1112. freed until the Lua CJSON module is garbage collected. This is the
  1113. default setting.
  1114. </p>
  1115. </dd>
  1116. <dt class="hdlist1">
  1117. <tt>false</tt>
  1118. </dt>
  1119. <dd>
  1120. <p>
  1121. Free the encode buffer after each call to <tt>cjson.encode</tt>.
  1122. </p>
  1123. </dd>
  1124. </dl></div>
  1125. <div class="paragraph"><p>The current setting is always returned, and is only updated when an
  1126. argument is provided.</p></div>
  1127. <h3 id="encode_max_depth">3.9. encode_max_depth</h3><div style="clear:left"></div>
  1128. <div class="listingblock">
  1129. <div class="content"><!-- Generator: GNU source-highlight 3.1.4
  1130. by Lorenzo Bettini
  1131. http://www.lorenzobettini.it
  1132. http://www.gnu.org/software/src-highlite -->
  1133. <pre><tt>depth <span style="color: #990000">=</span> cjson<span style="color: #990000">.</span><span style="font-weight: bold"><span style="color: #000000">encode_max_depth</span></span><span style="color: #990000">([</span>depth<span style="color: #990000">])</span>
  1134. <span style="font-style: italic"><span style="color: #9A1900">-- "depth" must be a positive integer. Default: 1000.</span></span></tt></pre></div></div>
  1135. <div class="paragraph"><p>Once the maximum table depth has been exceeded Lua CJSON will generate
  1136. an error. This prevents a deeply nested or recursive data structure from
  1137. crashing the application.</p></div>
  1138. <div class="paragraph"><p>By default, Lua CJSON will generate an error when trying to encode data
  1139. structures with more than 1000 nested tables.</p></div>
  1140. <div class="paragraph"><p>The current setting is always returned, and is only updated when an
  1141. argument is provided.</p></div>
  1142. <div class="exampleblock">
  1143. <div class="title">Example: Recursive Lua table</div>
  1144. <div class="exampleblock-content"><!-- Generator: GNU source-highlight 3.1.4
  1145. by Lorenzo Bettini
  1146. http://www.lorenzobettini.it
  1147. http://www.gnu.org/software/src-highlite -->
  1148. <pre><tt>a <span style="color: #990000">=</span> <span style="color: #FF0000">{}</span><span style="color: #990000">;</span> a<span style="color: #990000">[</span><span style="color: #993399">1</span><span style="color: #990000">]</span> <span style="color: #990000">=</span> a</tt></pre></div></div>
  1149. <h3 id="encode_number_precision">3.10. encode_number_precision</h3><div style="clear:left"></div>
  1150. <div class="listingblock">
  1151. <div class="content"><!-- Generator: GNU source-highlight 3.1.4
  1152. by Lorenzo Bettini
  1153. http://www.lorenzobettini.it
  1154. http://www.gnu.org/software/src-highlite -->
  1155. <pre><tt>precision <span style="color: #990000">=</span> cjson<span style="color: #990000">.</span><span style="font-weight: bold"><span style="color: #000000">encode_number_precision</span></span><span style="color: #990000">([</span>precision<span style="color: #990000">])</span>
  1156. <span style="font-style: italic"><span style="color: #9A1900">-- "precision" must be an integer between 1 and 14. Default: 14.</span></span></tt></pre></div></div>
  1157. <div class="paragraph"><p>The amount of significant digits returned by Lua CJSON when encoding
  1158. numbers can be changed to balance accuracy versus performance. For data
  1159. structures containing many numbers, setting
  1160. <tt>cjson.encode_number_precision</tt> to a smaller integer, for example <tt>3</tt>,
  1161. can improve encoding performance by up to 50%.</p></div>
  1162. <div class="paragraph"><p>By default, Lua CJSON will output 14 significant digits when converting
  1163. a number to text.</p></div>
  1164. <div class="paragraph"><p>The current setting is always returned, and is only updated when an
  1165. argument is provided.</p></div>
  1166. <h3 id="encode_sparse_array">3.11. encode_sparse_array</h3><div style="clear:left"></div>
  1167. <div class="listingblock">
  1168. <div class="content"><!-- Generator: GNU source-highlight 3.1.4
  1169. by Lorenzo Bettini
  1170. http://www.lorenzobettini.it
  1171. http://www.gnu.org/software/src-highlite -->
  1172. <pre><tt>convert<span style="color: #990000">,</span> ratio<span style="color: #990000">,</span> safe <span style="color: #990000">=</span> cjson<span style="color: #990000">.</span><span style="font-weight: bold"><span style="color: #000000">encode_sparse_array</span></span><span style="color: #990000">([</span>convert<span style="color: #990000">[,</span> ratio<span style="color: #990000">[,</span> safe<span style="color: #990000">]]])</span>
  1173. <span style="font-style: italic"><span style="color: #9A1900">-- "convert" must be a boolean. Default: false.</span></span>
  1174. <span style="font-style: italic"><span style="color: #9A1900">-- "ratio" must be a positive integer. Default: 2.</span></span>
  1175. <span style="font-style: italic"><span style="color: #9A1900">-- "safe" must be a positive integer. Default: 10.</span></span></tt></pre></div></div>
  1176. <div class="paragraph"><p>Lua CJSON classifies a Lua table into one of three kinds when encoding a
  1177. JSON array. This is determined by the number of values missing from the
  1178. Lua array as follows:</p></div>
  1179. <div class="dlist"><dl>
  1180. <dt class="hdlist1">
  1181. Normal
  1182. </dt>
  1183. <dd>
  1184. <p>
  1185. All values are available.
  1186. </p>
  1187. </dd>
  1188. <dt class="hdlist1">
  1189. Sparse
  1190. </dt>
  1191. <dd>
  1192. <p>
  1193. At least 1 value is missing.
  1194. </p>
  1195. </dd>
  1196. <dt class="hdlist1">
  1197. Excessively sparse
  1198. </dt>
  1199. <dd>
  1200. <p>
  1201. The number of values missing exceeds the configured
  1202. ratio.
  1203. </p>
  1204. </dd>
  1205. </dl></div>
  1206. <div class="paragraph"><p>Lua CJSON encodes sparse Lua arrays as JSON arrays using JSON <tt>null</tt> for
  1207. the missing entries.</p></div>
  1208. <div class="paragraph"><p>An array is excessively sparse when all the following conditions are
  1209. met:</p></div>
  1210. <div class="ulist"><ul>
  1211. <li>
  1212. <p>
  1213. <tt>ratio</tt> &gt; <tt>0</tt>
  1214. </p>
  1215. </li>
  1216. <li>
  1217. <p>
  1218. <em>maximum_index</em> &gt; <tt>safe</tt>
  1219. </p>
  1220. </li>
  1221. <li>
  1222. <p>
  1223. <em>maximum_index</em> &gt; <em>item_count</em> * <tt>ratio</tt>
  1224. </p>
  1225. </li>
  1226. </ul></div>
  1227. <div class="paragraph"><p>Lua CJSON will never consider an array to be <em>excessively sparse</em> when
  1228. <tt>ratio</tt> = <tt>0</tt>. The <tt>safe</tt> limit ensures that small Lua arrays are always
  1229. encoded as sparse arrays.</p></div>
  1230. <div class="paragraph"><p>By default, attempting to encode an <em>excessively sparse</em> array will
  1231. generate an error. If <tt>convert</tt> is set to <tt>true</tt>, <em>excessively sparse</em>
  1232. arrays will be converted to a JSON object.</p></div>
  1233. <div class="paragraph"><p>The current settings are always returned. A particular setting is only
  1234. changed when the argument is provided (non-<tt>nil</tt>).</p></div>
  1235. <div class="exampleblock">
  1236. <div class="title">Example: Encoding a sparse array</div>
  1237. <div class="exampleblock-content"><!-- Generator: GNU source-highlight 3.1.4
  1238. by Lorenzo Bettini
  1239. http://www.lorenzobettini.it
  1240. http://www.gnu.org/software/src-highlite -->
  1241. <pre><tt>cjson<span style="color: #990000">.</span><span style="font-weight: bold"><span style="color: #000000">encode</span></span><span style="color: #990000">(</span><span style="color: #FF0000">{</span> <span style="color: #990000">[</span><span style="color: #993399">3</span><span style="color: #990000">]</span> <span style="color: #990000">=</span> <span style="color: #FF0000">"data"</span> <span style="color: #FF0000">}</span><span style="color: #990000">)</span>
  1242. <span style="font-style: italic"><span style="color: #9A1900">-- Returns: '[null,null,"data"]'</span></span></tt></pre></div></div>
  1243. <div class="exampleblock">
  1244. <div class="title">Example: Enabling conversion to a JSON object</div>
  1245. <div class="exampleblock-content"><!-- Generator: GNU source-highlight 3.1.4
  1246. by Lorenzo Bettini
  1247. http://www.lorenzobettini.it
  1248. http://www.gnu.org/software/src-highlite -->
  1249. <pre><tt>cjson<span style="color: #990000">.</span><span style="font-weight: bold"><span style="color: #000000">encode_sparse_array</span></span><span style="color: #990000">(</span><span style="font-weight: bold"><span style="color: #0000FF">true</span></span><span style="color: #990000">)</span>
  1250. cjson<span style="color: #990000">.</span><span style="font-weight: bold"><span style="color: #000000">encode</span></span><span style="color: #990000">(</span><span style="color: #FF0000">{</span> <span style="color: #990000">[</span><span style="color: #993399">1000</span><span style="color: #990000">]</span> <span style="color: #990000">=</span> <span style="color: #FF0000">"excessively sparse"</span> <span style="color: #FF0000">}</span><span style="color: #990000">)</span>
  1251. <span style="font-style: italic"><span style="color: #9A1900">-- Returns: '{"1000":"excessively sparse"}'</span></span></tt></pre></div></div>
  1252. </div>
  1253. <h2 id="_api_variables">4. API (Variables)</h2>
  1254. <div class="sectionbody">
  1255. <h3 id="_name">4.1. _NAME</h3><div style="clear:left"></div>
  1256. <div class="paragraph"><p>The name of the Lua CJSON module (<tt>"cjson"</tt>).</p></div>
  1257. <h3 id="_version">4.2. _VERSION</h3><div style="clear:left"></div>
  1258. <div class="paragraph"><p>The version number of the Lua CJSON module (<tt>"2.1.0"</tt>).</p></div>
  1259. <h3 id="_null">4.3. null</h3><div style="clear:left"></div>
  1260. <div class="paragraph"><p>Lua CJSON decodes JSON <tt>null</tt> as a Lua <tt>lightuserdata</tt> NULL pointer.
  1261. <tt>cjson.null</tt> is provided for comparison.</p></div>
  1262. </div>
  1263. <h2 id="_references">5. References</h2>
  1264. <div class="sectionbody">
  1265. <div class="ulist"><ul>
  1266. <li>
  1267. <p>
  1268. <a href="http://tools.ietf.org/html/rfc4627">RFC 4627</a>
  1269. </p>
  1270. </li>
  1271. <li>
  1272. <p>
  1273. <a href="http://www.json.org/">JSON website</a>
  1274. </p>
  1275. </li>
  1276. </ul></div>
  1277. </div>
  1278. <div id="footer">
  1279. <div id="footer-text">
  1280. Last updated 2012-03-01 22:52:58 CST
  1281. </div>
  1282. </div>
  1283. </body>
  1284. </html>