Files

Data

Storage, json and HttpService.

json

Encode and decode JSON. This works on text only: there is no network access.

json.encode(value, pretty)

Returns a JSON string.

  • pretty = true adds line breaks and two-space indentation.
  • Tables whose keys are 1..n become arrays. Other tables become objects with sorted keys.
  • An empty table becomes [].
  • nan and infinity become null.
  • A table that contains itself raises json: circular reference.

json.decode(text)

Returns the value. Raises an error such as json: key expected at 5 for invalid text, nested more than 64 levels, or with data after the value. JSON null becomes nil.

local text = json.encode({ name = "test", list = { 1, 2, 3 } }, true)
local data = json.decode(text)
print(data.list[2]) -- 2

HttpService

Compatibility object. It has no network functions.

HttpService:JSONEncode(value, pretty)

Same as json.encode. Call it with a colon.

HttpService:JSONDecode(text)

Same as json.decode.

HttpService:GenerateGUID()

Returns a random ID like 3f2a9c1e-5b7d-4e8a-9c3d-2a1b4c5d6e7f. Also available as the global uuid().

Storage

Saves Lua values as JSON files in the workspace folder storage/. Names are made safe: anything other than letters, digits, _ and - becomes _.

Storage.save(name, value)

Writes storage/name.json. Returns true on success, or false and an error message.

Storage.load(name, default)

Returns the saved value, or default if the file is missing or is not valid JSON.

Storage.exists(name)

Returns true if the file exists.

Storage.delete(name)

Deletes the file. Returns true on success, or false and an error message.

Storage.list()

Returns the names of all saved values.

local settings = Storage.load("settings", { range = 4 })
settings.range = 5
Storage.save("settings", settings)

Because values go through JSON, only strings, numbers, booleans, tables and arrays survive. Functions and vectors do not.