dkjson — dkjson

JSON Module for Lua

Introduction

This is a JSON module written in Lua. It supports UTF-8.

JSON (JavaScript Object Notation) is a format for serializing data based on the syntax for JavaScript data structures. It is an ideal format for transmitting data between different applications and commonly used for dynamic web pages. It can also be used to save Lua data structures, but you should be aware that not every Lua table can be represented by the JSON standard. For example tables that contain both string keys and an array part cannot be exactly represented by JSON. You can solve this by putting your array data in an explicit subtable.

dkjson is written in Lua without any dependencies, but when LPeg is available dkjson uses it to speed up decoding.

Download

Usage

The full documentation including the license is available here in the wiki or as Markdown text in the readme.txt file.

dkjson is free software released under the same conditions as the Lua interpreter. Please remember to mention external code you are using in your software.

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.5 (2014-04-28)

Changes since version 2.4:

Version 2.4 (2013-09-28)

Changes since version 2.3:

Version 1.3 (2013-09-28)

Changes since version 1.2:

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 (2010-08-28)

Initial version.