Module jls.util.json
Provide JavaScript Object Notation (JSON) codec.
Functions
json.encode (value) | Returns the JSON encoded string representing the specified value. |
json.decode (value) | Returns the value representing the specified string. |
json.parse (value) | Returns the value representing the specified string. |
json.stringify (value[, space[, lenient]]) | Returns the JSON encoded string representing the specified value. |
json.require (name[, try]) | Loads the JSON resource for the specified name. |
Fields
json.null | The opaque value representing null. |
Functions
- json.encode (value)
-
Returns the JSON encoded string representing the specified value.
Parameters:
- value The value to encode.
Returns:
-
string
the encoded string.
Usage:
local json = require('jls.util.json') json.encode({aString = 'Hello world !'}) -- Returns '{"aString":"Hello world !"}'
- json.decode (value)
-
Returns the value representing the specified string.
Parameters:
- value string The JSON string to decode.
Returns:
-
the decoded value.
Usage:
local json = require('jls.util.json') json.decode('{"aString":"Hello world !"}') -- Returns {aString = 'Hello world !'}
- json.parse (value)
-
Returns the value representing the specified string.
Raises an error if the value cannot be parsed.
Parameters:
- value string The JSON string to parse.
Returns:
-
the parsed value.
- json.stringify (value[, space[, lenient]])
-
Returns the JSON encoded string representing the specified value.
When specifying space, the encoded string will includes new lines.
Invalid value will raise an error if lenient mode is not enabled.
Parameters:
- value The value to to convert to a JSON encoded string.
- space number The number of space characters to use as white space. (optional)
- lenient boolean true to convert invalid JSON keys or values using tostring. (optional)
Returns:
-
string
the encoded string.
- json.require (name[, try])
-
Loads the JSON resource for the specified name.
See loader loadResource function.
Parameters:
- name string the JSON name.
- try boolean true to return nil in place of raising an error (optional)
Returns:
-
the JSON value.