Class luci.jsonc

LuCI JSON parsing and serialization library. The luci.jsonc class is a high level Lua binding to the JSON-C library to allow reading and writing JSON data with minimal overhead.

Functions

new () Construct a new luci.jsonc.parser instance.
parse (json) Parse a complete JSON string and convert it into a Lua data structure.
stringify (data, pretty) Convert given Lua data into a JSON string.


Functions

new ()
Construct a new luci.jsonc.parser instance.

Usage:

parser = luci.jsonc.new()

Return value:

A luci.jsonc.parser object representing a JSON-C tokener.
parse (json)
Parse a complete JSON string and convert it into a Lua data structure.

Parameters

  • json: A string containing the JSON data to parse, must be either a JSON array or a JSON object.

Usage:

data = luci.jsonc.parse('{ "name": "John", "age": 34 }') 
print(data.name)  -- "John"

Return value:

On success, a table containing the parsed JSON data is returned, on failure the function returns nil and a string containing the reason of the parse error.

See also:

stringify (data, pretty)
Convert given Lua data into a JSON string. This function recursively converts the given Lua data into a JSON string, ignoring any unsupported data. Lua tables are converted into JSON arrays if they only contain integer keys, mixed tables are turned into JSON objects with any existing numeric keys converted into strings. Lua functions, coroutines and userdata objects are ignored and Lua numbers are converted to integers if they do not contain fractional values.

Parameters

  • data: The Lua data to convert, can be a table, string, boolean or number.
  • pretty: A boolean value indicating whether the resulting JSON should be pretty printed.

Usage:

json = luci.jsonc.stringify({ item = true, values = { 1, 2, 3 } }) 
print(json)  -- '{"item":true,"values":[1,2,3]}'

Return value:

Returns a string containing the JSON representation of the given Lua data.

See also:

Valid XHTML 1.0!