Improved display of TOML code blocks

This commit is contained in:
moandji.ezana 2015-01-15 01:32:51 +02:00
parent cd94968907
commit 6906dea7d0

View file

@ -10,32 +10,32 @@ For the bleeding-edge version integrating the latest specs, see the [work-in-pro
Add the following dependency to your POM (or equivalent for other dependency managers): Add the following dependency to your POM (or equivalent for other dependency managers):
````xml ```xml
<dependency> <dependency>
<groupId>com.moandjiezana.toml</groupId> <groupId>com.moandjiezana.toml</groupId>
<artifactId>toml4j</artifactId> <artifactId>toml4j</artifactId>
<version>0.3.1</version> <version>0.3.1</version>
</dependency> </dependency>
```` ```
Requires Java 1.6. Requires Java 1.6.
## Quick start ## Quick start
````java ```java
Toml toml = new Toml().parse(getTomlFile()); Toml toml = new Toml().parse(getTomlFile());
String someValue = toml.getString("someKey"); String someValue = toml.getString("someKey");
Date someDate = toml.getDate("someTable.someDate"); Date someDate = toml.getDate("someTable.someDate");
MyClass myClass = toml.to(MyClass.class); MyClass myClass = toml.to(MyClass.class);
```` ```
## Usage ## Usage
A `com.moandjiezana.toml.Toml` instance is populated by calling one of `parse(File)`, `parse(InputStream)`, `parse(Reader)` or `parse(String)`. A `com.moandjiezana.toml.Toml` instance is populated by calling one of `parse(File)`, `parse(InputStream)`, `parse(Reader)` or `parse(String)`.
````java ```java
Toml toml = new Toml().parse("a=1"); Toml toml = new Toml().parse("a=1");
```` ```
An exception is thrown if the source is not valid TOML. An exception is thrown if the source is not valid TOML.
@ -45,15 +45,15 @@ The data can then be accessed either by converting the Toml instance to your own
`Toml#to(Class<T>)` maps a Toml instance to the given class. `Toml#to(Class<T>)` maps a Toml instance to the given class.
```` ```toml
name = "Mwanji Ezana" name = "Mwanji Ezana"
[address] [address]
street = "123 A Street" street = "123 A Street"
city = "AnyVille" city = "AnyVille"
```` ```
````java ```java
class Address { class Address {
String street; String street;
String city; String city;
@ -63,14 +63,14 @@ class User {
String name; String name;
Address address; Address address;
} }
```` ```
````java ```java
User user = new Toml().parse(tomlFile).to(User.class); User user = new Toml().parse(tomlFile).to(User.class);
assert user.name.equals("Mwanji Ezana"); assert user.name.equals("Mwanji Ezana");
assert user.address.street.equals("123 A Street"); assert user.address.street.equals("123 A Street");
```` ```
Any keys not found in both the TOML and the class are ignored. Fields may be private. Any keys not found in both the TOML and the class are ignored. Fields may be private.
@ -102,7 +102,7 @@ You can also navigate values within a table with a compound key of the form `tab
Non-existent keys return null. Non-existent keys return null.
```` ```toml
title = "TOML Example" title = "TOML Example"
[database] [database]
@ -130,9 +130,9 @@ title = "TOML Example"
location = "Geneva" location = "Geneva"
[[networks.operators]] [[networks.operators]]
location = "Paris" location = "Paris"
```` ```
````java ```java
Toml toml = new Toml().parse(getTomlFile()); Toml toml = new Toml().parse(getTomlFile());
String title = toml.getString("title"); String title = toml.getString("title");
@ -148,13 +148,13 @@ Toml network1 = toml.getTable("networks[0]");
String network2Name = toml.getString("networks[1].name"); // "Level 2" String network2Name = toml.getString("networks[1].name"); // "Level 2"
List<Toml> network3Operators = toml.getTables("networks[2].operators"); List<Toml> network3Operators = toml.getTables("networks[2].operators");
String network3Operator2Location = toml.getString("networks[2].operators[1].location"); // "Paris" String network3Operator2Location = toml.getString("networks[2].operators[1].location"); // "Paris"
```` ```
### Defaults ### Defaults
The constructor can be given a set of default values that will be used as fallbacks. For tables and table arrays, a shallow merge is performed. The constructor can be given a set of default values that will be used as fallbacks. For tables and table arrays, a shallow merge is performed.
```` ```toml
# defaults # defaults
a = 2 a = 2
b = 3 b = 3
@ -162,9 +162,9 @@ b = 3
[table] [table]
c = 4 c = 4
d = 5 d = 5
```` ```
```` ```toml
a = 1 a = 1
[table] [table]
@ -172,9 +172,9 @@ a = 1
[[array]] [[array]]
d = 3 d = 3
```` ```
````java ```java
Toml defaults = new Toml().parse(getDefaultsFile()); Toml defaults = new Toml().parse(getDefaultsFile());
Toml toml = new Toml(defaults).parse(getTomlFile()); Toml toml = new Toml(defaults).parse(getTomlFile());
@ -184,7 +184,7 @@ Long c = toml.getLong("c"); // returns null
Long tableC = toml.getLong("table.c"); // returns 2, not 4 Long tableC = toml.getLong("table.c"); // returns 2, not 4
Long tableD = toml.getLong("table.d"); // returns null, not 5 Long tableD = toml.getLong("table.d"); // returns null, not 5
Long arrayD = toml.getLong("array[0].d"); // returns 3 Long arrayD = toml.getLong("array[0].d"); // returns 3
```` ```
### Limitations ### Limitations