IMIsle Modkit
Pages in this area

json

Shared

A small JSON library written in Lua, built into both hosts.

modkit.json.encode

modkit.json.encode(value)

Parameter Type Description
value nil, boolean, number, string, table the value to encode

Returns: JSON text.

Rules.

  • A table whose keys are exactly 1..n becomes an array. An empty table also becomes an array, [].
  • Every other table becomes an object. Keys are turned into strings.
  • NaN and infinity become null.
  • Functions, userdata and coroutines raise an error.
  • A table with holes ({ 1, nil, 3 }) is not an array and becomes an object with the keys "1" and "3".
local text = modkit.json.encode({ name = "Rex", tags = { "big", "loud" }, growth = 0.75 })

modkit.json.decode

modkit.json.decode(text)

Parameter Type Description
text string JSON text

Returns: the decoded value. JSON null becomes nil, so a null inside an array leaves a hole.

Invalid JSON raises a Lua error. Wrap the call in pcall when the text comes from a player or a file that may be damaged.

local ok, data = pcall(modkit.json.decode, text)
if not ok then data = {} end