dkjson
dkjson
Not logged in

Chisel is closing down. This repository can be found under the new URL dkolf.de/src/dkjson-lua.fsl/.

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)

Changes since version 2.2:

Version 1.2 (2013-04-14)

Changes since version 1.1:

Version 2.2 (2012-04-28)

Changes since version 2.1:

Version 2.1 (2011-07-08)

Changes since version 2.0:

Version 1.1 (2011-07-08)

Changes since version 1.0:

Version 2.0 (2011-05-30)

Changes since version 1.0:

Version 1.0

Initial version, released 2010-08-28.