Object Instance luci.jsonc.parser
LuCI JSON parser instance. A JSON parser instance is useful to parse JSON data chunk by chunk, without the need to assemble all data in advance.
Functions
parser:parse (json) | Parses one chunk of JSON data. |
parser:get () | Convert parsed JSON data into Lua table. |
parser:set (data) | Put Lua data into the parser. |
parser:sink () | Generate an ltn12-compatible sink. |
parser:stringify (pretty) | Serialize current parser state as JSON. |
Functions
- parser:parse (json)
-
Parses one chunk of JSON data.
Parameters
- json: String containing the JSON fragment to parse
Usage:
parser = luci.jsonc.new() while true do chunk = ... -- fetch a cunk of data, e.g. from a socket finish, errmsg = parser.parse(chunk) if finish == nil then error("Cannot parse JSON: " .. errmsg) end if finish == true then break end end
Return value:
true
if a complete JSON object has been parsed and no further input is expected.false
if further input is requirednil
if an error was encountered while parsing the current chunk. In this case a string describing the parse error is returned as second value.
See also:
- parser:get ()
-
Convert parsed JSON data into Lua table.
Usage:
parser = luci.jsonc.new() parser:parse('{ "example": "test" }') data = parser:get() print(data.example) -- "test"
Return value:
Parsed JSON object converted into a Lua table ornil
if the parser didn't finish or encountered an error.See also:
- parser:set (data)
-
Put Lua data into the parser.
Parameters
-
data: Lua data to put into the parser object. The data is converted to an
internal JSON representation that can be dumped with
stringify()
. The conversion follows the rules described inluci.jsonc.stringify
.
Usage:
parser = luci.jsonc.new() parser:set({ "some", "data" })
Return value:
Nothing is returned.See also:
-
data: Lua data to put into the parser object. The data is converted to an
internal JSON representation that can be dumped with
- parser:sink ()
-
Generate an ltn12-compatible sink.
Usage:
parser = luci.jsonc.new() ltn12.pump.all(ltn12.source.file(io.input()), parser:sink()) print(parser:get())
Return value:
Returns a function that can be used as an ltn12 sink. - parser:stringify (pretty)
-
Serialize current parser state as JSON.
Parameters
- pretty: A boolean value indicating whether the resulting JSON should be pretty printed.
Usage:
parser = luci.jsonc.new() parser:parse('{ "example": "test" }') print(parser:serialize()) -- '{"example":"test"}'
Return value:
Returns the serialized JSON data of this parser instance.