Save Json

Signature

bool saveJson(string path, table data)

Description

Serializes a table and saves it as a JSON file.

Returns whether the operation was successful (true) or not (false).

Arguments

Path

The path of the file to be saved.

Absolute paths are supported and relative paths start from {game}\x64\.

Data

The contents to serialize and save.

Example

Save Config

local data = {
  name = "Vaan",
  model = 6488064
}

config.saveJson(config.path .. "/mymod-config.json", data)

Output

mymod-config.json
{
    "model": 6488064,
    "name": "Vaan"
}

Notes

Table keys are sorted alphabetically before saving data.

If a table starts at 1, increases sequentially without gaps, and contains only integer keys, it will be serialized as an array. However, if explicit key notations ([n] = value) are used, it will be serialized as a table with string keys, since Lua internally uses a hash table implementation for all keys and the C++ interface provided by Sol2 cannot traverse these tables in the proper order.

Last updated