Pyjo.TOML - TOML loader and dumper

from Pyjo.TOML import dump, load

config = load('name = "Pyjoyment"\n\n[server]\nworkers = 4\n')

string = dump({'name': 'Pyjoyment', 'server': {'workers': 4}})

Pyjo.TOML loads TOML 1.0 with tomllib of the standard library, which has no writer of TOML, and dumps it, the format of Pyjoyment.Plugin.TOMLConfig and of the stash value toml of the renderer of Pyjoyment.

dump() writes a dict as a table, whose keys come first, and then its tables, [server], a dict of a dict, and its arrays of tables, [[products]], a list of dict, whose tables follow each of them. A table with keys only in its tables has no line of its own, as [a.b] defines a. Other lists and tuples are arrays, whose dicts are inline tables, [{ name = "x" }, 2]. A string is a basic string, with the escapes of TOML, and a string of several lines is a multi-line basic string, """...""". datetime.datetime, datetime.date and datetime.time are the date-times, the dates and the times of TOML, an aware one with its offset. A key is bare where TOML allows it, and quoted otherwise.

# 'name = "app"\n\n[server]\nworkers = 4\n'
dump({'name': 'app', 'server': {'workers': 4}})

TOML has no null and no bytes, so None and bytes raise an error, as do a key that is not a string, a value at the top that is not a table, and a collection inside itself. Another object is what Pyjo.JSON encodes, the value of its method __json__ or its string.

See also

Pyjoyment.Plugin.TOMLConfig, Pyjo.YAML, https://mojolicious.org.

Pyjo.TOML.dump(value: Any, *, sort_keys: bool = False) → str[source]
string = dump(value)
string = dump(value, sort_keys=True)

Dump a table to a TOML string, with the keys of a table in the order of the dict, or sorted with sort_keys.

# 'name = "Pyjoyment"\n'
dump({'name': 'Pyjoyment'})
Pyjo.TOML.load(string: str | bytes | bytearray) → dict[str, Any][source]
value = load(string)

Load a TOML string with tomllib.loads(), whose error is a Pyjo.Exception.

# {'name': 'Pyjoyment'}
load('name = "Pyjoyment"')

A byte order mark at the start of the string, which an editor on Windows writes, is not a part of its first line. Bytes are UTF-8, as TOML is, or UTF-16 or UTF-32 with a byte order mark, as Windows PowerShell writes UTF-16LE.

# {'name': 'Pyjoyment'}
load('name = "Pyjoyment"'.encode('UTF-16'))