-
Notifications
You must be signed in to change notification settings - Fork 18
pyio.write_json
Writes a Lua data structure to a JSON file.
pyio.write_json(file, value)| Parameter | Type | Description |
|---|---|---|
file |
path handle |
The file to write. The path handle must grant write access. |
value |
any |
A Lua value (typically a table) that should be serialized to JSON |
| Type | Description |
|---|---|
boolean |
true on success, false if the file could not be written (see remarks) |
Writing the following Lua table
local data = {
Type = "TEST",
["My Numbers"] = {1, 2, 3.5}
}
local folder = pyui.select_folder("full")
if folder then
pyio.write_json(folder:append_path("sample.json"), data)
endwill create a JSON file with the following content:
{
"Type": "TEST",
"My Numbers": [1, 2, 3.5]
}Lua tables are automatically mapped to JSON structures.
If a table contains only consecutive integer keys starting at 1, it is written as a JSON array. Otherwise, it is written as a JSON object with string keys.
Lua values are converted as follows:
| Lua Type | JSON equivalent |
|---|---|
| table (array-like) | JSON array [...]
|
| table (key/value) | JSON object {...}
|
| string | JSON string |
| number | JSON number |
| boolean | JSON true / false
|
| nil | JSON null
|
Only string keys are written for JSON objects. Non-string keys are ignored.
Unsupported Lua types (e.g. functions, userdata) are written as null.
The output encoding is UTF-8.
A writable path handle can be obtained from pyui.select_folder with "full" access. Passing a read-only handle raises an error. false is returned when the file cannot be opened or written.
Minimum PYTHA Version: V26