Viewing version 0.7.0. Open the latest package page.

toml

0.7.0

Parser and encoder for TOML v0.4.0 (Tom's Obvious, Minimal Language) configuration files.

Download archive

Readme

toml.dart

Dart CI Pub Package GitHub License

This package provides an implementation of a TOML parser and encoder for Dart.

It currently supports version 0.4.0 of the TOML specification.

Table of Contents

  1. Installation
  2. Usage
    1. Loading TOML
    2. Parsing TOML
    3. Decoding TOML
    4. Encoding TOML
  3. Data Structure
  4. Testing
  5. License

Installation

To get started add toml as a dependency to your pubspec.yaml and run the dart pub get or flutter pub get command.

dependencies:
  toml: "^0.7.0"

Usage

The toml.dart package can be used for loading, decoding and encoding TOML documents. To get started, just add the following import.

import 'package:toml/toml.dart';

The subsequent sections describe how to use the library in more detail. Additional examples can be found in the ./example directory.

Loading TOML

In order to load a TOML document, invoke the static TomlDocument.load method and pass the name of the configuration file to load. The method returns a Future of the loaded TomlDocument.

void main() async {
  var document = await TomlDocument.load('config.toml');
  // ...
}

When the code is running in the browser, HTTP is used to fetch the configuration file. When the code is running on the Dart VM or natively, the file is loaded from the local file system.

If the loaded TOML file contains a syntax error, a TomlParserException is thrown.

Full examples for loading a configuration file via HTTP and from a file can be found in ./example/http_config_loader and ./example/filesystem_config_loader, respectively.

Parsing TOML

Sometimes the two default mechanisms for loading TOML documents with TomlDocument.load are not sufficient. In those cases, you can simply load the contents of the configuration file yourself and parse them as a TOML document manually using the static TomlDocument.parse function.

void main() {
  var contents = '...';
  var document = TomlDocument.parse(contents);
  // ...
}

If the loaded TOML file contains a syntax error, a TomlParserException is thrown.

An example for writing a custom configuration file loader and parsing the loaded file manually can be found in ./example/toml_parser.

Decoding TOML

In the last two subsections we've learned how to load or parse a TomlDocument. Such a TomlDocument is an abstract representation of the syntax of a TOML document. In order to access the configuration options that are stored in the TOML document, we first have to convert it to a hash map with the TomlDocument.toMap method.

void main() {
  var config = TomlDocument.parse('...').toMap();
  // ...
}

If the TOML document is semantically invalid, TomlException is thrown.

In the next section the type and structure of the generated hash map will be elaborated in more detail.

Encoding TOML

This package also includes a TOML encoder that can convert a hash map to a TOML document. Simply use the TomlDocument.fromMap factory constructor to convert the hash map into the internal representation. The resulting document can be converted to a TOML encoded string using the toString method.

var document = TomlDocument.fromMap({
  // ...
}).toString();

The type and structure of the hash map should match the format described in the next section. Additionally, the map may contain arbitrary keys and values that implement the TomlEncodableKey and TomlEncodableValue interfaces, respectively. Classes which implement those interfaces must define a toTomlKey or toTomlValue method, respectively, whose return value is either implements the interface itself or is natively encodable by TOML. If an object cannot be encoded as a TOML key or value, a TomlUnknownKeyTypeException or TomlUnknownValueTypeException is thrown by TomlDocument.fromMap.

An example for using the encoder and the TomlEncodableValue interface to encode a Map can be found in ./example/toml_encoder.

Data Structure

TOML documents and tables as well as inline tables are represented through nested Map objects whose keys are Strings and values dynamic representations of the corresponding TOML value or sub-table. The contents of a table declared by

[a.b.c]
key = 'value'

may be accessed using [] as shown in the listing below.

var table = document['a']['b']['c']; // ok

The following, on the other hand, is invalid.

var table = document['a.b.c']; // error

All kinds of arrays including arrays of tables are stored as List objects. The encoder accepts any Iterable, though. The items of the list represent either a value or a table. Consider the document that contains an array of tables.

[[items]]
name = 'A'

[[items]]
name = 'B'

[[items]]
name = 'C'

For example, it is possible to iterate over the tables in the array as follows.

document['items'].forEach((Map<String, dynamic> item) {
  print(item['name']);
});

All string variants produce regular dart Strings. All of the following are therefore equivalent.

str1 = "Hello World!"
str2 = 'Hello World!'
str3 = """\
  Hello \
  World!\
"""
str4 = '''
Hello World!'''

Integers are of type int and floating point numbers are represented as doubles. When compiled to JavaScript these two types are not distinct. Thus a float without decimal places might accidentally be encoded as an integer. This behavior would lead to the generation of invalid numeric arrays. The TOML encoder addresses this issue by analyzing the contents of numeric arrays first. If any of its items cannot be represented as an integer, all items will be encoded as floats instead. Encoding the following map, for example, would throw an TomlMixedArrayTypesException in the VM.

var document = {
  'array': [1, 2.0, 3.141]
};

However, in JavaScript, the encoder yields the following TOML document.

array = [1.0, 2.0, 3.141]

Boolean values are obviously of type bool.

Datetime values are UTC DateTime objects.

Testing

To see whether everything is working correctly change into the root directory of this package and run the included tests as follows:

dart test

You may pass the --platform command line argument to test the package on other platforms than the VM. Run dart test --help for a list of all available platforms.

Alternatively, you can run toml-test (again from the package root):

$GOPATH/bin/toml-test bin/decoder.dart
$GOPATH/bin/toml-test -encoder bin/encoder.dart

To speedup the tests, it is recommended to compile the encoder and decoder scripts before running toml-test.

mkdir -p build/bin
dart compile exe -o build/bin/decoder bin/decoder.dart
dart compile exe -o build/bin/encoder bin/encoder.dart

$GOPATH/bin/toml-test build/bin/decoder
$GOPATH/bin/toml-test -encoder build/bin/encoder

Unfortunately, some encoder tests from toml-test are failing at the moment because date times are always encoded as UTC and the encoder never generates inline tables.

License

toml.dart is licensed under the MIT license agreement. See the LICENSE file for details.

Changelog

Changelog

0.7.0 / 2021-01-17

This is a major update that does not only bring along many changes to the internal architecture of the library but also to its public interface.

  • Removed the TomlParser class. Use TomlDocument.parse and TomlDocument.toMap instead.
  • In preparation to support TOML v0.5.0 in an upcoming version of the library, the parser is now based on TOML's official ABNF.
  • Removed the TomlEncoder class. Use TomlDocument.fromMap and TomlDocument.toString instead.
  • Removed the toml.decoder and toml.encoder libraries. You should always use the toml library instead.
  • Removed the toml.loader library as well as the toml.loader.* libraries. Use TomlDocument.load instead. In order to implement a custom loading mechanism, use TomlDocument.parse instead.
  • Removed the yaml dependency. JSON and YAML documents cannot be loaded with this package anymore.
  • Renamed TomlEncodable to TomlEncodableValue and toToml to toTomlValue.
  • Added TomlEncodableKey interface. Objects that implement this interface are allowed to be used as keys in hash maps instead of strings. Their toTomlKey method must return a TomlEncodeableKey itself or a string that can be used as a TOML key. By default toTomlKey is implemented via toTomlValue.

0.6.1 / 2020-11-19

  • Upgraded dependencies.

0.6.0 / 2020-05-22

  • Upgraded to Dart 2.

0.5.1 / 2017-06-19

  • Upgraded dependencies.

0.5.0 / 2016-07-24

  • Removed deprecated use*ConfigLoader functions.
  • Improved testing and added support for BurntSushi's toml-test suite.

0.4.0 / 2015-06-05

  • Removed deprecated toml.browser and toml.server libraries.
  • Dropped support for dart_config. There is now a custom ConfigLoader interface with two default implementations.
  • The use*ConfigLoader functions are still available for backward compatibility, but are deprecated and will be removed in the next release. Each of the ConfigLoader implementations has a static use method which you should use instead.

0.3.0 / 2015-04-08

  • Introduced new toml.loader library.
  • The toml.browser and toml.server libraries are now deprecated and will be removed in the next release. Use the new toml.loader library instead.

0.2.0 / 2015-02-12

  • Updated to v0.4.0 of the TOML spec.
  • Added bare/quoted keys.
  • Added inline table syntax.
  • Allowed underscores in numbers.
  • Removed forward slash as an escapable character.

0.1.1 / 2015-01-26

  • Fixed links and markdown.

0.1.0 / 2015-01-23

  • Initial version, implements v0.3.1 of the TOML spec.

Installing

export PUB_HOSTED_URL=https://pub.mirror.p-hatan.ir
flutter pub add 'toml:0.7.0'
pubspec.yaml
name: toml
version: 0.7.0
description: Parser and encoder for TOML v0.4.0 (Tom's Obvious, Minimal Language) configuration files.
repository: https://github.com/just95/toml.dart
environment:
  sdk: '>=2.7.0 <3.0.0'
dependencies:
  petitparser: ^3.1.0
  quiver: ^2.1.5
dev_dependencies:
  pedantic: ^1.9.2
  test: ^1.15.5

Version history

VersionPublishedArchiveSHA-256
0.1.0Cached · Upstream SHA-256 matchedDownloadfe943e7b…745f75
0.1.1Cached · Upstream SHA-256 matchedDownload0fbeebbe…b0f201
0.2.0Cached · Upstream SHA-256 matchedDownload794b8740…318e06
0.3.0Cached · Upstream SHA-256 matchedDownload5d51223f…9bc5e6
0.4.0Cached · Upstream SHA-256 matchedDownload2679b939…1f56f1
0.5.0Cached · Upstream SHA-256 matchedDownloadb503ca42…be77e9
0.5.1Cached · Upstream SHA-256 matchedDownload003443c1…06a362
0.6.0Cached · Upstream SHA-256 matchedDownload1a883934…bcfb52
0.6.1Cached · Upstream SHA-256 matchedDownload8a7fe415…f61a4f
0.7.0Cached · Upstream SHA-256 matchedDownloadd866f9e8…8c1a3d
0.8.0Cached · Upstream SHA-256 matchedDownloadf1f1d0d0…637181
0.9.0Cached · Upstream SHA-256 matchedDownloadc4b52a2f…7fd499
0.9.1Cached · Upstream SHA-256 matchedDownload2a96184e…7a62f5
0.10.0Cached · Upstream SHA-256 matchedDownload02575f98…b911f9
0.11.0Cached · Upstream SHA-256 matchedDownload585a8c04…539e29
0.12.0Cached · Upstream SHA-256 matchedDownload00172a9c…c88526
0.13.0Cached · Upstream SHA-256 matchedDownload06ce5b8d…0e20f1
0.13.1Cached · Upstream SHA-256 matchedDownload69756bc1…9ac485
0.14.0Cached · Upstream SHA-256 matchedDownload157c5dca…ccee44
0.15.0Cached · Upstream SHA-256 matchedDownload9968de24…c45a2d
0.16.0Cached · Upstream SHA-256 matchedDownloadd968d149…400e71
0.17.0Cached · Upstream SHA-256 matchedDownload35cd2a13…ede786
0.18.0Cached · Upstream SHA-256 matchedDownload35a35f78…79c05e

Dependencies

petitparser "^3.1.0"

quiver "^2.1.5"