A small, simple TOML parser and serializer for Swift. Powered by toml++.
TOMLKit is a Swift wrapper around toml++, allowing you to parse and serialize TOML files in Swift.
Created by gh-md-toc
Documentation is available here.
Add this to the dependencies
array in Package.swift
:
.package(url: "https://github.com/LebJe/TOMLKit.git", from: "0.4.0")
Also add this to the targets
array in the aforementioned file:
.product(name: "TOMLKit", package: "TOMLKit")
import TOMLKit
import struct Foundation.Date
let toml = """
string = "Hello, World!"
int = 523053
double = 3250.34
"""
do {
let table = try TOMLTable(string: toml)
table.remove(at: "double")
table["dateTime"] = TOMLDateTime(date: Foundation.Date())
table.insert(
TOMLArray([1, 2, "3", TOMLTable(["time": TOMLTime(hour: 10, minute: 26, second: 49, nanoSecond: 203)])]),
at: "array"
)
print("TOML:\n\(table.convert(to: .toml))\n")
print("JSON:\n\(table.convert(to: .json))\n")
print("YAML:\n\(table.convert(to: .yaml))")
} catch let error as TOMLParseError {
// TOMLParseError occurs when the TOML document is invalid.
// `error.source.begin` contains the line and column where the error started,
// and `error.source.end` contains the line where the error ended.
print(error.source.begin)
print(error.source.end)
}
// TOML:
// array = [ 1, 2, '3', { time = 10:26:49.000000203 } ]
// dateTime = 2022-01-18T10:47:49.640691995Z
// int = 523053
// string = 'Hello, World!'
//
// JSON:
// {
// "array" : [
// 1,
// 2,
// "3",
// {
// "time" : "10:26:49.000000203"
// }
// ],
// "dateTime" : "2022-01-18T10:47:49.640691995Z",
// "int" : 523053,
// "string" : "Hello, World!"
// }
//
// YAML:
// array:
// - 1
// - 2
// - 3
// - time: '10:26:49.000000203'
// dateTime: '2022-01-18T10:47:49.640691995Z'
// int: 523053
// string: 'Hello, World!'
Full documentation is available here.
The toml++ license is available in the tomlplusplus
directory in the LICENSE
file.
Before committing, please install pre-commit, swift-format, clang-format, and Prettier, then install the pre-commit hook:
$ brew bundle # install the packages specified in Brewfile
$ pre-commit install
# Commit your changes.
To install pre-commit on other platforms, refer to the documentation.