<nowiki>
<h1>David Kolf's JSON module for Lua

<p><em>Version

<p>In the default configuration this module writes no global values, not even
the module table. Import it using</p>

<pre><code>json = require ("dkjson")


<p>In environments where <code>require</code> or a similiar function are not available
and you cannot receive the return value of the module, you can set the
option <code>register_global_module_table</code> to <code>true</code>.  The module table will
then be saved in the global variable with the name given by the option


<p>Exported functions and values:</p>

<h2><code>json.encode (object [,

<p>Create a string representing the object. <code>Object</code> can be a table,
a string, a number, a boolean, <code>nil</code>, <code>json.null</code> or any object with
a function <code>__tojson</code> in its metatable. A table can only use strings
and numbers as keys and its values have to be valid objects as
well. It raises an error for any invalid data types or reference
cycles.</p>

<p><code>state</code> is an optional table with the following fields:</p>

<ul>
<li><code>indent</code> <br>
When <code>indent</code> (a boolean) is set, the created string will contain
newlines and indentations. Otherwise it will be one long line.</li>
<li><code>keyorder</code> <br>
<code>keyorder</code> is an array to specify the ordering of keys in the
encoded output. If an object has keys which are not in this array
they are written after the sorted keys.</li>
<li><code>level</code> <br>
This is the initial level of indentation used when <code>indent</code> is
set. For each level two spaces are added. When absent it is set
to 0.</li>
<li><code>buffer</code> <br>
<code>buffer</code> is an array to store the strings for the result so they
can be concatenated at once. When it isn't given, the encode
function will create it temporary and will return the
concatenated result.</li>
<li><code>bufferlen</code> <br>
When <code>bufferlen</code> is set, it has to be the index of the last
element of
<li><code>tables</code> <br>
<code>tables</code> is a set to detect reference cycles. It is created
temporary when absent. Every table that is currently processed
is used as key, the value is
</ul>

<p>When <code>state.buffer</code> was set, the return value will be <code>true</code> on
success. Without <code>state.buffer</code> the return value will be a string.</p>

<h2><code>json.decode (string [, position [,

<p>Decode <code>string</code> starting at <code>position</code> or at 1 if <code>position</code> was
omitted.</p>

<p><code>null</code> is an optional value to be returned for null values. The
default is <code>nil</code>, but you could set it to <code>json.null</code> or any other
value.</p>

<p>The return values are the object or <code>nil</code>, the position of the next
character that doesn't belong to the object, and in case of errors
an error message.</p>

<p>Two metatables are created. Every array or object that is decoded gets
a metatable with the <code>__jsontype</code> field set to either <code>array</code> or
<code>object</code>. If you want to provide your own metatables use the syntax</p>

<pre><code>json.decode (string, position, null, objectmeta, arraymeta)


<p>To prevent the assigning of metatables pass

<pre><code>json.decode (string, position, null, nil)


<p><code>__jsonorder</code> can overwrite the <code>keyorder</code> for a specific table.</p>


<p><code>__jsontype</code> can be either <code>"array"</code> or <code>"object"</code>. This value is only
checked for empty tables. (The default for empty tables is

<h2><code>&lt;metatable&gt;.__tojson (self,

<p>You can provide your own <code>__tojson</code> function in a metatable. In this
function you can either add directly to the buffer and return true,
or you can return a string. On errors nil and a message should be
returned.</p>


<p>You can use this value for setting explicit <code>null</code> values.</p>


<p>Set to <code>"dkjson

<h2><code>json.quotestring

<p>Quote a UTF-8 string and escape critical characters using JSON
escape sequences. This function is only necessary when you build
your own <code>__tojson</code> functions.</p>

<h2><code>json.addnewline

<p>When <code>state.indent</code> is set, add a newline to <code>state.buffer</code> and spaces
according to

<h2>LPeg support</h2>

<p>When the local configuration variable <code>always_try_using_lpeg</code> is set,
this module tries to load LPeg to replace the <code>decode</code> function. The
speed increase is significant. You can get the LPeg module at
  <a
When LPeg couldn't be loaded, the pure Lua functions stay active.</p>

<p>In case you don't want this module to require LPeg on its own,
disable the option <code>always_try_using_lpeg</code> in the options section at
the top of the module.</p>

<p>In this case you can later load LPeg support using</p>

<h3><code>json.use_lpeg

<p>Require the LPeg module and replace the functions <code>quotestring</code> and
and <code>decode</code> with functions that use LPeg patterns.
This function returns the module table, so you can load the module
using:</p>

<pre><code>json = require "dkjson".use_lpeg()


<p>Alternatively you can use <code>pcall</code> so the JSON module still works when
LPeg isn't found.</p>

<pre><code>json = require "dkjson"
pcall (json.use_lpeg)


<p>This variable is set to <code>true</code> when LPeg was loaded successfully.</p>

<hr>

<h2>Contact</h2>

<p>You can contact the author by sending an e-mail to 'david' at the
domain

<hr>

<p><em>Copyright (C) 2010-2013 David Heiko

<p>Permission is hereby granted, free of charge, to any person obtaining
a copy of this software and associated documentation files (the
"Software"), to deal in the Software without restriction, including
without limitation the rights to use, copy, modify, merge, publish,
distribute, sublicense, and/or sell copies of the Software, and to
permit persons to whom the Software is furnished to do so, subject to
the following conditions:</p>

<p>The above copyright notice and this permission notice shall be
included in all copies or substantial portions of the Software.</p>

<p>THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.</p>
</nowiki>