### parsercontext.run Source: https://github.com/grafi-tt/lunajson/blob/master/README.md Starts the SAX-style JSON parsing process from the current position within the parser context. It advances the position to after the parsed JSON if successful. ```APIDOC #### parsercontext.run() ### Description Start parsing from current position. If valid JSON is parsed, the position moves to just after the end of this JSON. Otherwise it errors. ``` -------------------------------- ### Run SAX Parser and Get Position Source: https://github.com/grafi-tt/lunajson/blob/master/README.md Executes the SAX parser from its current position and returns the position after parsing. Use `tellpos` to retrieve the current parsing position. ```lua local pos = parser:run() local current_pos = parser:tellpos() ``` -------------------------------- ### lunajson.decode Source: https://github.com/grafi-tt/lunajson/blob/master/README.md Decodes a JSON string into a Lua table. It supports optional parameters for starting position, a null value sentinel, and array length tracking. ```APIDOC ## lunajson.decode(jsonstr, [pos, [nullv, [arraylen]]]) ### Description Decode `jsonstr`. If `pos` is specified, it starts decoding from `pos` until the JSON definition ends, otherwise the entire input is parsed as JSON. `null` inside `jsonstr` will be decoded as the optional sentinel value `nullv` if specified, and discarded otherwise. If `arraylen` is true, the length of an array `ary` will be stored in `ary[0]`. This behavior is useful when empty arrays should not be confused with empty objects. This function returns the decoded value if `jsonstr` contains valid JSON, otherwise an error will be raised. If `pos` is specified it also returns the position immediately after the end of decoded JSON. ``` -------------------------------- ### Create SAX-style Parser with Input Function Source: https://github.com/grafi-tt/lunajson/blob/master/README.md Demonstrates creating a SAX-style parser using a custom input function that reads data in chunks. The input function should return a string chunk or nil when done. This is useful for parsing large files or network streams. ```lua local fp = io.open("myfavorite.json") local function input() local s if fp then s = fp:read(8192) if not s then fp:close() fp = nil end end return s end local saxtbl = {} local parser = lunajson.newparser(input, saxtbl) parser:run() ``` -------------------------------- ### lunajson.newparser and lunajson.newfileparser Source: https://github.com/grafi-tt/lunajson/blob/master/README.md Creates a SAX-style parser context for parsing JSON from a string, a function returning chunks, or a file. It uses a callback table for handling parsing events. ```APIDOC ## lunajson.newparser(input, saxtbl) ## lunajson.newfileparser(filename, saxtbl) ### Description Create and return a sax-style parser context, which parses `input` or a file specified by `filename`. `input` can be a string to be parsed, or a function that returns the next chunk of a data as a string to be parsed (or `nil` when all data is yielded). An example function for `input` follows (this sample is essentially same as the implementation of `newfileparser`). Note that `input` will never be called once it has returned `nil`. local fp = io.open("myfavorite.json") local function input() local s if fp then s = fp:read(8192) if not s then fp:close() fp = nil end end return s end `saxtbl` is a table of callbacks. It can have the following functions. Those functions will be called on corresponding events, if it is in the table. - startobject() - key(s) - endobject() - startarray() - endarray() - string(s) - number(n) - boolean(b) - null() A parser context maintains the current parse position, initially 1. ``` -------------------------------- ### parsercontext.tryc Source: https://github.com/grafi-tt/lunajson/blob/master/README.md Returns the byte value of the character at the current parsing position without advancing the position. Returns nil if the input has ended. ```APIDOC #### parsercontext.tryc() ### Description Return the byte of current position as a number. If input is ended, it returns `nil`. It does not change current position. ``` -------------------------------- ### parsercontext.tellpos Source: https://github.com/grafi-tt/lunajson/blob/master/README.md Returns the current parsing position within the input data. ```APIDOC #### parsercontext.tellpos() ### Description Return current position. ``` -------------------------------- ### Read Data from Parser Source: https://github.com/grafi-tt/lunajson/blob/master/README.md Reads a specified number of bytes from the current parser position and advances the position. Truncates if the input ends. ```lua local data = parser:read(10) ``` -------------------------------- ### parsercontext.read Source: https://github.com/grafi-tt/lunajson/blob/master/README.md Reads a specified number of bytes from the current parsing position and advances the position accordingly. Truncates if the end of input is reached. ```APIDOC #### parsercontext.read(n) ### Description Return the `n`-length string starting from current position, and increase the index by `n`. If the input ends, the returned string and the updated position will be truncated. ``` -------------------------------- ### Peek Current Byte Source: https://github.com/grafi-tt/lunajson/blob/master/README.md Returns the current byte at the parser's position without advancing the position. Returns nil if the input has ended. ```lua local byte = parser:tryc() ``` -------------------------------- ### Decode and Encode JSON with Lunajson Source: https://github.com/grafi-tt/lunajson/blob/master/README.md Basic usage for decoding a JSON string into a Lua table and encoding a Lua table back into a JSON string. Ensure the input is valid JSON and the table structure is compatible with JSON encoding. ```lua local lunajson = require 'lunajson' local jsonstr = '{"Hello":["lunajson",1.5]}' local t = lunajson.decode(jsonstr) print(t.Hello[2]) -- prints 1.5 print(lunajson.encode(t)) -- prints {"Hello":["lunajson",1.5]} ``` -------------------------------- ### lunajson.encode Source: https://github.com/grafi-tt/lunajson/blob/master/README.md Encodes a Lua value into a JSON string. It handles Lua tables as arrays or objects and supports a sentinel value for encoding Lua's `nil` as JSON `null`. ```APIDOC ## lunajson.encode(value, [nullv]) ### Description Encode `value` into a JSON string and return it. If `nullv` is specified, values equal to `nullv` will be encoded as `null`. This function encodes a table `t` as a JSON array if a value `t[1]` is present or a number `t[0]` is present. If `t[0]` is present, its value is considered as the length of the array. Then the array may contain `nil` and those will be encoded as `null`. Otherwise, this function scans non-`nil` values starting from index 1, up to the first `nil` it finds. When the table `t` is not an array, it is an object and all of its keys must be strings. If this constraint is not met or unsupported types (e.g. function) are contained in `value`, an error will be raised. ``` === COMPLETE CONTENT === This response contains all available snippets from this library. No additional content exists. Do not make further requests.