The module used for parsing and serializing data in JSON format
local JSON = require("json") local JSONString = "{'json_parser': true}" local data = JSON.parse(JSONString) print(table.prettyprint(data)) local testTable = {this_is_a_boolean = true, {1, 3, 4, 5, 6}}; print(JSON.stringify(testTable, true))
[boolean] json_parser true {[1,3,4,5,6],"this_is_a_boolean":true}
JSON | |
---|---|
Dependencies: | lextra |
Writen in: | lua |
Dependants: | [none] |
The JSON module has two methods: parse and stringify. They do the opposite thing of each other
parse | ||
---|---|---|
Arguments | Returns | Description |
[string] str [number]* start |
[table] data [number] position |
Converts any valid JSON string into a Lua table. It is important to note that this funciton returns a table AND a number. This number is mostly used for parsing, and represents the last character the parser read. |
* optional parameter, default = 0
stringify | ||
---|---|---|
Arguments | Returns | Description |
[table] t [boolean]* arrayCheck |
[string] JSONString |
Serializes any Lua table into JSON string. The second parameter (optional) will check if the Lua table can be represented as a JSON array. This is false by default, and can impact performance. |
* optional parameter, default = false