Skip to main content

sc.json

The JSON Module for the computer API!


Functions​

isSafe​

sc.json.isSafe( root )

Returns true if a lua table is safe to convert to JSON.

A table is not safe if it (or any table inside of it) has a key that isn't a string or a number, or if it mixes key types (for example having both string and number keys).

Arguments:

  • root [ table ] The lua table to check.

Returns:

  • [ boolean ] Returns true if the data is safe for conversion to json.

toString​

sc.json.toString( root, prettify, indent )

Converts a value to a JSON string.

  • If root is a table that isn't safe, this errors instead of producing a string that could crash the game.
  • Tables with sequential keys (1, 2, 3, ...) become JSON arrays. Every other table (including an empty one) becomes a JSON object.
  • Number keys inside of an object are converted to strings. Any other key type will cause an error.
  • Non-finite numbers (NaN, inf and -inf) are written as null.

Arguments:

  • root [ table|string|number|boolean|nil ] The value to convert to a string.
  • prettify [ boolean? ] If true, The JSON string would be prettier. Use this when you want to display it to the user.
  • indent [ string? ] The indentation character. Defaults to "\t"

Returns:

  • [ string ] The JSON string from a Lua table.

prettifyTable​

sc.json.prettifyTable( rootContents )

Adds syntax highlighting to the JSON tab. Content is automatically prettified.

Arguments:

  • rootContents [ table|string|number|boolean|nil ] The root contents to format.

Returns:

  • [ string ] The prettified JSON-like formatted string.

prettifyString​

sc.json.prettifyString( root )

Prettifies a JSON string by parsing it into a table and passing it into prettifyTable.

Arguments:

  • root [ string ] The raw JSON string.

Returns:

  • [ string ] The prettified result.

toJsonCompatibleTable​

sc.json.toJsonCompatibleTable( tbl )

Cleans a table by removing all keys and values that cannot be represented in JSON. This also goes through all tables inside of it.

  • Valid keys are strings and numbers.
  • Valid values are strings, numbers, booleans and tables.
  • If the table is an array, the invalid items are removed and the remaining items shift down so there are no gaps left.

Arguments:

  • tbl [ table ] The table to clean. (It will be modified.)

Returns:

  • [ table ] The cleaned table.