JSON Module for Lua
Introduction
This is a JSON module written in Lua without any dependencies to other external libraries. It supports UTF-8.
Download
Usage
The full documentation is available here in the wiki or as Markdown text in the source file.
Examples
Encoding
local json = require ("dkjson")
local tbl = {
animals = { "dog", "cat", "aardvark" },
instruments = { "violin", "trombone", "theremin" },
bugs = json.null,
trees = nil
}
local str = json.encode (tbl, { indent = true })
print (str)
Output
{
"bugs":null,
"instruments":["violin","trombone","theremin"],
"animals":["dog","cat","aardvark"]
}
Decoding
local json = require ("dkjson")
local str = [[
{
"numbers": [ 2, 3, -20.23e+2, -4 ],
"currency": "\u20AC"
}
]]
local obj, pos, err = json.decode (str, 1, nil)
if err then
print ("Error:", err)
else
print ("currency", obj.currency)
for i = 1,#obj.numbers do
print (i, obj.numbers[i])
end
end
Output
currency € 1 2 2 3 3 -2023 4 -4
Versions
Version 2.3 (2013-04-14)
- Download dkjson.lua version 2.3
Changes since version 2.2:
- Corrected the range of escaped characters. Among other characters U+2029 was missing, which would cause trouble when parsed by a JavaScript interpreter.
- Added options to register the module table in a global variable. This is useful in environments where functions similar to require are not available.
Version 1.2 (2013-04-14)
- Download dkjson.lua version 1.2
Changes since version 1.1:
- Corrected the range of escaped characters. Among other characters U+2029 was missing, which would cause trouble when parsed by a JavaScript interpreter.
- Locations for error messages were off by one in the first line.
Version 2.2 (2012-04-28)
- Download dkjson.lua version 2.2
Changes since version 2.1:
- __jsontype is only used for empty tables.
- It is possible to decode tables without assigning metatables.
- Locations for error messages were off by one in the first line.
- There is no LPeg version of json.quotestring anymore.
Version 2.1 (2011-07-08)
- Download dkjson.lua version 2.1
Changes since version 2.0:
- Changed the documentation to Markdown format.
- LPeg is now parsing only a single value at a time to avoid running out of Lua stack for big arrays and objects.
- Read __tojson, __jsontype and __jsonorder even from blocked metatables through the debug module.
- Fixed decoding single numbers (only affected the non-LPeg mode).
- Corrected the range of escaped Unicode control characters.
Version 1.1 (2011-07-08)
- Download dkjson.lua version 1.1
Changes since version 1.0:
- The values NaN/+Inf/-Inf are recognised and encoded as "null" like in the original JavaScript implementation.
- Read __tojson even from blocked metatables through the debug module.
- Fixed decoding single numbers.
- Corrected the range of escaped Unicode control characters.
Version 2.0 (2011-05-30)
- Download dkjson.lua version 2.0
Changes since version 1.0:
- Optional LPeg support.
- Invalid input data for encoding raises errors instead of returning nil and the error message. (Invalid data for encoding is usually a programming error. Raising an error removes the work of explicitly checking the result).
- The metatable field __jsontype can control whether a Lua table is encoded as a JSON array or object. (Mainly useful for empty tables).
- When decoding, two metatables are created. One is used to mark the arrays while the other one is used for the objects. (The metatables are created once for each decoding operation to make sandboxing possible. However, you can specify your own metatables as arguments).
- There are no spaces added any longer when encoding.
- It is possible to explicitly sort keys for encoding by providing an array with key names to the option "keyorder" or the metatable field __jsonorder.
- The values NaN/+Inf/-Inf are recognised and encoded as "null" like in the original JavaScript implementation.
Version 1.0
Initial version, released 2010-08-28.
- Download dkjson.lua version 1.0