JSON Utilities

JSON Utilities

Encode and decode JSON data:

Functions

sandman.json.try_decode

Type: Function
Description: Decodes a JSON string into a Lua variable

⚠️ Note: This function can throw errors. A sandman.json.try_decode alternative is available that returns result_or_nil, error instead of throwing.

Usage

value = sandman.json.decode('{"this": "is Sandman"}')

Parameters

  • json_string: string

Returns

  • value: any

Examples

-- Basic usage
value = sandman.json.decode('{"this": "is Sandman"}')

-- Safe alternative (returns value_or_nil, error):
value_or_nil, error = sandman.json.try_decode('{"this": "is Sandman"}')
if value_or_nil then
  -- Use value_or_nil
else
  -- Handle the error
end

sandman.json.encode

Type: Function
Description: Encodes a Lua variable into a JSON string

Usage

json_string = sandman.json.encode({this = "is Sandman"})

Parameters

  • value: any

Returns

  • json_string: string

Examples

-- Basic usage
json_string = sandman.json.encode({this = "is Sandman"})