pub repository
mirror.p-hatan.comtoml
0.6.1Parser and encoder for TOML v0.4.0 (Tom's Obvious, Minimal Language) configuration files.
Readme
toml.dart
This package provides an implementation of a TOML (Tom's Obvious, Minimal Language) parser and encoder for Dart.
It currently supports version 0.4.0 of the TOML specification.
Table of Contents
Installation
To get started add toml as a dependency to your pubspec.yaml and run the
pub get command.
dependencies:
toml: "^0.6.1"
Usage
The toml.dart package includes three libraries for loading, decoding and encoding TOML documents.
The usage of the individual libraries is described in the subsequent sections in more detail.
Additional examples for the usage of the toml.dart package can be found in the ./example directory.
All three libraries toml.decoder, toml.encoder and toml.loader are reexported by the toml library.
Thus, a single import usually suffices.
import 'package:toml/toml.dart';
Load Configuration Files
Before any configuration file can be parsed, the library needs to know how to load it. There are two default methods available, but you can easily implement your own loading mechanism as described in the next section.
If your code is running in the browser, you probably want to use XHR to
fetch the file from the server.
To do so import the toml.loader.http library and call the static HttpConfigLoader.use method, e.g., from your main function.
import 'package:toml/loader/http.dart';
void main() {
HttpConfigLoader.use();
// ...
}
An example for loading a configuration file via HTTP can be found in ./example/http_config_loader.
If your code is running in the Dart VM, you can load configuration files from the local file system.
Simply import the toml.loader.fs library and call the static FilesystemConfigLoader.use method, e.g., from your main function.
import 'package:toml/loader/fs.dart';
void main() {
FilesystemConfigLoader.use();
// ...
}
An example for loading a configuration file from a file can be found in ./example/filesystem_config_loader.
For convenience both libraries export the loadConfig function from the toml.loader library.
It optionally takes the path to the configuration file as its only argument (defaults to 'config.toml') and returns a Future of the parsed configuration options.
Future main() async {
// ...
var cfg = await loadConfig();
// ...
}
Implement a Custom Loader
To create a custom loader which fits exactly your needs import the toml.loader library, create a new class and implement the ConfigLoader interface.
You can use the following template as a starting point.
library my.config.loader;
import 'package:toml/loader.dart';
export 'package:toml/loader.dart' show loadConfig;
class MyConfigLoader implements ConfigLoader {
static void use() {
ConfigLoader.use(new MyConfigLoader());
}
@override
Future<String> loadConfig(String filename) {
// ...
}
}
In your main function invoke the MyConfigLoader.use method and call the loadConfig function as usual.
An example for writing a custom configuration file loader can be found in ./example/custom_config_loader.
Decode TOML
When you are using the toml.loader library, you don't have to decode TOML documents manually.
The decoding is performed by loadConfig already.
However, if you have a String and want to decode its contents as a TOML document, you can use the toml.decoder library.
import 'package:toml/decoder.dart';
This library contains the actual TomlParser class whose parse method takes a String and returns a Result object.
The result's value property holds an unmodifiable Map of the parsed document.
var toml = '''
# ...
''';
var parser = new TomlParser();
var document = parser.parse(toml).value;
An example for using TomlParser to decode a string can be found in ./example/toml_parser.
Encode TOML
This package includes a TOML encoder. To use it add the following import.
import 'package:toml/encoder.dart';
The library provides a TomlEncoder class whose encode method takes a Map and returns a TOML encoded String.
All values of the map must be natively representable by TOML or implement the TomlEncodable interface.
var document = {
// ...
};
var encoder = new TomlEncoder();
var toml = encoder.encode(document);
Classes which implement the TomlEncodable interface define a toToml method whose return value can be represented by TOML in turn.
An example for using the TomlEncoder and the TomlEncodable 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 UnmodifiableMapView objects whose keys are Strings and values dynamic read-only 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
var value = table['key'];
The following, however, is invalid.
var table = document['a.b.c']; // error
table['key'] = value; // error
All kinds of arrays including arrays of tables are stored as UnmodifiableListView objects.
Though the encoder accepts any Iterable.
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'
It is possible to iterate over the tables in the array.
document['items'].forEach((Map item) { // ok
print(item.name);
});
However, it is not allowed to add, remove or modify its entries.
document['items'].add({ // error
'name': 'D'
});
document['items'][0] = { // error
'name': 'E'
};
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 TomlEncoder 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 MixedArrayTypesException 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:
pub run test
You may pass the --platform command line argument to test the package on other platforms than the VM.
Run pub run 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
Unfortunately, some tests from toml-test are failing at the moment.
Thus, toml.dart is not fully TOML 0.4.0 compliant at the moment.
We will fix the failing tests in an upcoming release of this library soon.
License
toml.dart is licensed under the MIT license agreement.
See the LICENSE file for details.
Changelog
Changelog
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*ConfigLoaderfunctions. - Improved testing and added support for BurntSushi's
toml-testsuite.
0.4.0 / 2015-06-05
- Removed deprecated
toml.browserandtoml.serverlibraries. - Dropped support for
dart_config. There is now a customConfigLoaderinterface with two default implementations. - The
use*ConfigLoaderfunctions are still available for backward compatibility, but are deprecated and will be removed in the next release. Each of theConfigLoaderimplementations has a staticusemethod which you should use instead.
0.3.0 / 2015-04-08
- Introduced new
toml.loaderlibrary. - The
toml.browserandtoml.serverlibraries are now deprecated and will be removed in the next release. Use the newtoml.loaderlibrary 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.com
flutter pub add tomlpubspec.yaml
name: toml
version: 0.6.1
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:
path: ^1.7.0
petitparser: ^3.1.0
quiver: ^2.1.5
yaml: ^2.2.1
dev_dependencies:
pedantic: ^1.9.2
test: any
Version history
| Version | Published | Archive | SHA-256 |
|---|---|---|---|
| 0.1.0 | 2015-01-23T14:48:22.639310Z | Download | fe943e7b640fdc8ade39d14110a99b51188ab41f89ce574aefe79da178745f75 |
| 0.1.1 | 2015-01-26T13:47:23.835010Z | Download | 0fbeebbe90be744bf1ae35871c8f6b640fb82b87cad92dd5064ade53c4b0f201 |
| 0.2.0 | 2015-02-12T21:52:07.949870Z | Download | 794b87401f2ca24e6564279bb636e833cdb9cdfe4a1fbabc0fff7ba1c9318e06 |
| 0.3.0 | 2015-04-08T12:59:06.990840Z | Download | 5d51223fa88af735bd5ce33bed30f005d57371a2c8800580f74d5ddea19bc5e6 |
| 0.4.0 | 2015-06-05T14:57:17.183Z | Download | 2679b939dc96886bd04e2a1a4be602bec634ceaa7e7d93cf2f2d2cb2f21f56f1 |
| 0.5.0 | 2016-09-24T20:51:54.767Z | Download | b503ca42ecb7cf198f9387546b2b7eab75a2ffc617631c7ccc9e4b85c1be77e9 |
| 0.5.1 | 2017-06-19T09:16:08.248223Z | Download | 003443c1ddca744af80bd1434393110d31e2e5cb29da321a2a1f5025fa06a362 |
| 0.6.0 | 2020-05-22T14:19:44.977152Z | Download | 1a883934f58fd75335774b5e1dd9b50ad519e1945b1141555dfd7f68d2bcfb52 |
| 0.6.1 | 2020-11-19T17:51:57.723072Z | Download | 8a7fe4155bf9df605ec14db22e8d5f9ba6b92f661f27270f6ddf06e664f61a4f |
| 0.7.0 | 2021-01-17T01:27:50.546631Z | Download | d866f9e860393ebf08b37aa3a7d4203cc5f88ee0e09d43dcb174a7c48b8c1a3d |
| 0.8.0 | 2021-01-29T20:37:29.598384Z | Download | f1f1d0d049e6ca20ee08164504bca7b5da7f8c5e2ea7060f2968106799637181 |
| 0.9.0 | 2021-03-07T09:40:55.824203Z | Download | c4b52a2fdbc5e909647d96212faa4e667f11744ab1e7b9a2ff4c0993277fd499 |
| 0.9.1 | 2021-03-07T14:35:46.955647Z | Download | 2a96184e183ca3fe26b0a605955d0b37c6ad1ca732f765e9570c0a16607a62f5 |
| 0.10.0 | 2021-03-07T16:20:49.018649Z | Download | 02575f98805450710672aac1c7afd3af561d8e762815ae5e5821cc899db911f9 |
| 0.11.0 | 2021-04-18T22:21:43.724006Z | Download | 585a8c04b3e85d6a2512217e9d76b4e9e8c0dd0f3054a4f6ccbed69260539e29 |
| 0.12.0 | 2021-09-07T11:11:50.949884Z | Download | 00172a9c2d8211953a90ef71e888d22aeabe4a6e3f69c6a5aeabf77851c88526 |
| 0.13.0 | 2022-05-30T00:23:44.817391Z | Download | 06ce5b8d97c37b5c09bbee32f0619b135cbe495e44ea76131f61237f830e20f1 |
| 0.13.1 | 2022-05-31T15:33:42.145627Z | Download | 69756bc12eccf279b72217a87310d217efc4b3752f722e890f672801f19ac485 |
| 0.14.0 | 2023-03-05T01:43:21.795252Z | Download | 157c5dca5160fced243f3ce984117f729c788bb5e475504f3dbcda881accee44 |
| 0.15.0 | 2023-12-07T00:15:11.761933Z | Download | 9968de24e45b632bf1a654fe1ac7b6fe5261c349243df83fd262397799c45a2d |
| 0.16.0 | 2024-07-07T18:43:21.444176Z | Download | d968d149c8bd06dc14e09ea3a140f90a3f2ba71949e7a91df4a46f3107400e71 |
| 0.17.0 | 2025-10-21T22:43:46.595624Z | Download | 35cd2a1351c14bd213f130f8efcbd3e0c18181bff0c8ca7a08f6822a2bede786 |
| 0.18.0 | 2025-12-30T17:02:45.113288Z | Download | 35a35f782228656a2af31e8c73d1353cc4ef3d683fd68af1111b44631879c05e |
Dependencies
path "^1.7.0"
petitparser "^3.1.0"
quiver "^2.1.5"
yaml "^2.2.1"