Many hyperlinks are disabled.
Use anonymous login to enable hyperlinks.
David Kolf's JSON module for Lua 5.1/5.2
This module writes no global values, not even the module table. Import it using
json = require ("dkjson")
Exported functions and values:
json.encode (object [, state])
Create a string representing the object.
Object can be a table,
a string, a number, a boolean,
json.null or any object with
__tojson 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
state is an optional table with the following fields:
indent(a boolean) is set, the created string will contain newlines and indentations. Otherwise it will be one long line.
keyorderis 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.
This is the initial level of indentation used when
indentis set. For each level two spaces are added. When absent it is set to 0.
bufferis 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.
bufferlenis set, it has to be the index of the last element of
tablesis 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
state.buffer was set, the return value will be
state.buffer the return value will be a string.
json.decode (string [, position [, null]])
string starting at
position or at 1 if
null is an optional value to be returned for null values. The
nil, but you could set it to
json.null or any other
The return values are the object or
nil, the position of the next
character that doesn't belong to the object, and in case of errors
an error message.
Two metatables are created. Every array or object that is decoded gets
a metatable with the
__jsontype field set to either
object. If you want to provide your own metatables use the syntax
json.decode (string, position, null, objectmeta, arraymeta)
To prevent the assigning of metatables pass
json.decode (string, position, null, nil)
__jsonorder can overwrite the
keyorder for a specific table.
__jsontype can be either
"object". This value is only
checked for empty tables. (The default for empty tables is
<metatable>.__tojson (self, state)
You can provide your own
__tojson 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
You can use this value for setting explicit
Quote a UTF-8 string and escape critical characters using JSON
escape sequences. This function is only necessary when you build
state.indent is set, add a newline to
state.buffer and spaces
When the local configuration variable
always_try_using_lpeg is set,
this module tries to load LPeg to replace the
decode function. The
speed increase is significant. You can get the LPeg module at
When LPeg couldn't be loaded, the pure Lua functions stay active.
In case you don't want this module to require LPeg on its own,
disable the option
always_try_using_lpeg in the options section at
the top of the module.
In this case you can later load LPeg support using
Require the LPeg module and replace the functions
decode with functions that use LPeg patterns.
This function returns the module table, so you can load the module
json = require "dkjson".use_lpeg()
Alternatively you can use
pcall so the JSON module still works when
LPeg isn't found.
json = require "dkjson" pcall (json.use_lpeg)
This variable is set to
true when LPeg was loaded successfully.
You can contact the author by sending an e-mail to 'kolf' at the e-mail provider 'gmx.de'.
Copyright (C) 2010, 2011, 2012 David Heiko Kolf
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:
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
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.