Quick Overview
TOML (Tom's Obvious, Minimal Language) is a configuration file format designed to be easy to read and write. It aims to be a minimal configuration file format that's human-friendly and straightforward to parse into data structures in a wide variety of programming languages.
Pros
- Simple and intuitive syntax, easy for humans to read and write
- Supports a variety of data types, including strings, integers, floats, booleans, dates, and arrays
- Unambiguous specification, reducing parsing errors and inconsistencies
- Wide language support with implementations in many programming languages
Cons
- Less flexible than some other formats (e.g., JSON) for complex nested structures
- Not as widely adopted as JSON or YAML in some ecosystems
- Limited support for comments in some implementations
- Can become verbose for deeply nested structures
Code Examples
Since TOML is a configuration file format and not a code library, we'll provide examples of TOML syntax instead of code examples.
Example 1: Basic key-value pairs
# This is a TOML document
title = "TOML Example"
author = "John Doe"
date = 2023-04-14
Example 2: Arrays and tables
[fruits]
apple = { color = "red", taste = "sweet" }
banana = { color = "yellow", taste = "sweet" }
[[people]]
name = "Alice"
age = 30
[[people]]
name = "Bob"
age = 28
Example 3: Nested tables and arrays
[database]
server = "192.168.1.1"
ports = [ 8001, 8001, 8002 ]
[database.credentials]
username = "admin"
password = "secret123"
[[fruits]]
name = "apple"
[fruits.physical]
color = "red"
shape = "round"
Getting Started
To use TOML in your project, you typically need to:
- Create a file with a
.toml
extension (e.g.,config.toml
) - Write your configuration using TOML syntax
- Use a TOML parser library in your programming language of choice to read the file
For example, in Python, you can use the toml
library:
import toml
# Read TOML file
with open('config.toml', 'r') as f:
config = toml.load(f)
# Access configuration values
print(config['database']['server'])
Remember to install the appropriate TOML parser for your programming language before using it in your project.
Competitor Comparisons
Python lib for TOML
Pros of TOML
- More actively maintained with frequent updates
- Includes additional features and extensions beyond the TOML specification
- Provides a comprehensive test suite for TOML parsing and encoding
Cons of TOML
- Larger codebase, potentially more complex to understand and contribute to
- May include features not strictly compliant with the official TOML specification
- Slightly higher learning curve for new users due to additional functionality
Code Comparison
TOML (uiri/toml):
import toml
# Parse TOML string
parsed_toml = toml.loads('key = "value"')
# Write TOML to file
with open('config.toml', 'w') as f:
toml.dump(parsed_toml, f)
TOML (toml-lang/toml):
# No direct parsing implementation
# This repository is primarily for specification
# and documentation purposes
The toml-lang/toml repository is focused on the TOML specification itself, while uiri/toml provides a practical implementation for parsing and writing TOML in Python. The uiri/toml project offers more functionality for developers working with TOML files, whereas toml-lang/toml serves as the authoritative source for the TOML format definition.
Go library for the TOML file format
Pros of go-toml
- Provides a full-featured TOML library for Go, including encoding, decoding, and manipulation
- Offers better performance and more efficient memory usage
- Includes additional tools like a TOML-to-JSON converter and a linter
Cons of go-toml
- Specific to Go language, limiting its use in other programming environments
- May have a steeper learning curve for developers new to Go
Code Comparison
TOML:
[example]
key = "value"
number = 42
go-toml:
type Config struct {
Example struct {
Key string
Number int
}
}
var config Config
_, err := toml.Decode(tomlData, &config)
TOML is a specification for a minimal configuration file format, while go-toml is a Go library implementation of that specification. TOML provides the standard, while go-toml offers tools to work with TOML in Go applications.
TOML focuses on defining the format, syntax, and rules for TOML files. go-toml, on the other hand, provides practical implementations for parsing, encoding, and manipulating TOML data within Go programs.
While TOML ensures consistency across different implementations, go-toml offers Go-specific optimizations and features that make working with TOML in Go more efficient and convenient.
Convert designs to code with AI
Introducing Visual Copilot: A new AI model to turn Figma designs to high quality code using your components.
Try Visual CopilotREADME
TOML
Tom's Obvious, Minimal Language.
By Tom Preston-Werner, Pradyun Gedam, et al.
This repository contains the in-development version of the TOML specification. You can find the released versions at https://toml.io.
Objectives
TOML aims to be a minimal configuration file format that's easy to read due to obvious semantics. TOML is designed to map unambiguously to a hash table. TOML should be easy to parse into data structures in a wide variety of languages.
Example
# This is a TOML document.
title = "TOML Example"
[owner]
name = "Tom Preston-Werner"
dob = 1979-05-27T07:32:00-08:00 # First class dates
[database]
server = "192.168.1.1"
ports = [ 8000, 8001, 8002 ]
connection_max = 5000
enabled = true
[servers]
# Indentation (tabs and/or spaces) is allowed but not required
[servers.alpha]
ip = "10.0.0.1"
dc = "eqdc10"
[servers.beta]
ip = "10.0.0.2"
dc = "eqdc10"
[clients]
data = [ ["gamma", "delta"], [1, 2] ]
# Line breaks are OK when inside arrays
hosts = [
"alpha",
"omega"
]
Comparison with Other Formats
TOML shares traits with other file formats used for application configuration and data serialization, such as YAML and JSON. TOML and JSON both are simple and use ubiquitous data types, making them easy to code for or parse with machines. TOML and YAML both emphasize human readability features, like comments that make it easier to understand the purpose of a given line. TOML differs in combining these, allowing comments (unlike JSON) but preserving simplicity (unlike YAML).
Because TOML is explicitly intended as a configuration file format, parsing it is easy, but it is not intended for serializing arbitrary data structures. TOML always has a hash table at the top level of the file, which can easily have data nested inside its keys, but it doesn't permit top-level arrays or floats, so it cannot directly serialize some data. There is also no standard identifying the start or end of a TOML file, which can complicate sending it through a stream. These details must be negotiated on the application layer.
INI files are frequently compared to TOML for their similarities in syntax and use as configuration files. However, there is no standardized format for INI and they do not gracefully handle more than one or two levels of nesting.
Further reading:
- YAML spec: https://yaml.org/spec/1.2/spec.html
- JSON spec: https://tools.ietf.org/html/rfc8259
- Wikipedia on INI files: https://en.wikipedia.org/wiki/INI_file
Get Involved
Documentation, bug reports, pull requests, and all other contributions are welcome!
Wiki
We have an Official TOML Wiki that catalogs the following:
- Projects using TOML
- Implementations
- Validators
- Language-agnostic test suite for TOML decoders and encoders
- Editor support
- Encoders
- Converters
Please take a look if you'd like to view or add to that list. Thanks for being a part of the TOML community!
Convert designs to code with AI
Introducing Visual Copilot: A new AI model to turn Figma designs to high quality code using your components.
Try Visual Copilot