rtoml: Fast TOML Library for Python (Rust Implemented)

0.13.0 · active · verified Sun Apr 12

rtoml is a high-performance Python library for parsing and serializing TOML, implemented in Rust. It emphasizes correctness, speed, and flexible handling of `None` values, passing all standard TOML tests. It currently requires Python 3.10 or newer and is actively maintained with frequent updates.

Warnings

Install

Imports

Quickstart

This quickstart demonstrates how to serialize a Python dictionary to a TOML string using `rtoml.dumps` and then parse it back using `rtoml.loads`. It also highlights the default behavior of `None` values.

import rtoml

# Define a Python object
obj = {
    'title': 'TOML Example',
    'owner': {
        'name': 'Tom Preston-Werner',
    },
    'database': {
        'server': '192.168.1.1',
        'ports': [8001, 8001, 8002],
        'connection_max': 5000,
        'enabled': True,
        'option_none': None # Example with None
    },
}

# Serialize Python object to TOML string
toml_string = rtoml.dumps(obj, pretty=True)
print("Generated TOML:\n", toml_string)

# Load TOML string back into a Python object
loaded_obj = rtoml.loads(toml_string)
print("\nLoaded object:\n", loaded_obj)

# Verify round-trip (Note: None handling defaults differ)
# For exact match with None, dumps(none_value=None) or loads(none_value='null') might be needed
assert loaded_obj['database']['option_none'] is None
assert loaded_obj['database']['enabled'] == obj['database']['enabled']

view raw JSON →