mirror of
https://github.com/plexusorg/toml4j.git
synced 2025-01-01 04:52:24 +00:00
Matched README to wip branch
This commit is contained in:
parent
8fd7e6691c
commit
62b2718a17
1 changed files with 33 additions and 30 deletions
63
README.md
63
README.md
|
@ -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,7 +45,7 @@ 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]
|
||||||
|
@ -54,9 +54,9 @@ name = "Mwanji Ezana"
|
||||||
|
|
||||||
[contacts]
|
[contacts]
|
||||||
"email address" = me@example.com
|
"email address" = me@example.com
|
||||||
````
|
```
|
||||||
|
|
||||||
````java
|
```java
|
||||||
class Address {
|
class Address {
|
||||||
String street;
|
String street;
|
||||||
String city;
|
String city;
|
||||||
|
@ -67,28 +67,31 @@ class User {
|
||||||
Address address;
|
Address address;
|
||||||
Map<String, Object> contacts;
|
Map<String, Object> contacts;
|
||||||
}
|
}
|
||||||
````
|
```
|
||||||
|
|
||||||
````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");
|
||||||
assert user.contacts.get("\"email address\"").equals("me@example.com");
|
assert user.contacts.get("\"email address\"").equals("me@example.com");
|
||||||
````
|
```
|
||||||
|
|
||||||
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.
|
||||||
|
|
||||||
Quoted keys cannot be mapped directly to a Java object, but they can be used as keys within a `Map`.
|
Quoted keys cannot be mapped directly to a Java object, but they can be used as keys within a `Map`.
|
||||||
|
|
||||||
All TOML primitives can be mapped, as well as a number of Java-specific types:
|
TOML primitives can be mapped to a number of Java types:
|
||||||
|
|
||||||
* A TOML Number can be converted to any primitive type (or the wrapper equivalent), `BigInteger` or `BigDecimal`
|
TOML | Java
|
||||||
* A TOML string can be converted to a `String`, enum, `java.net.URI` or `java.net.URL`
|
---- | ----
|
||||||
* A single-letter TOML string can be converted to a `char` or `Character`
|
Integer | `int`, `long` (or wrapper), `java.math.BigInteger`
|
||||||
* Multiline and literal TOML strings can be converted to `String`
|
Float | `float`, `double` (or wrapper), `java.math.BigDecimal`
|
||||||
* A TOML array can be converted to a `List`, `Set` or array. The generic type can be anything that can be converted.
|
String | `String`, enum, `java.net.URI`, `java.net.URL`
|
||||||
* A TOML table can be converted to a custom class or to a `Map<String, Object>`. The generic type of the value can be anything that can be converted.
|
One-letter String | `char`, `Character`
|
||||||
|
Multiline and Literal Strings | `String`
|
||||||
|
Array | `List`, `Set`, array. The generic type can be anything that can be converted.
|
||||||
|
Table | Custom class, `Map<String, Object>`
|
||||||
|
|
||||||
Custom classes, Maps and collections thereof can be nested to any level. See [TomlToClassTest#should_convert_fruit_table_array()](src/test/java/com/moandjiezana/toml/TomlToClassTest.java) for an example.
|
Custom classes, Maps and collections thereof can be nested to any level. See [TomlToClassTest#should_convert_fruit_table_array()](src/test/java/com/moandjiezana/toml/TomlToClassTest.java) for an example.
|
||||||
|
|
||||||
|
@ -109,9 +112,9 @@ 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.
|
||||||
|
|
||||||
When retrieving quoted keys, the quotes must be used and the key must be spelled exactly the same way, including whitespace.
|
When retrieving quoted keys, the quotes must be used and the key must be spelled exactly the same way, including quotes and whitespace.
|
||||||
|
|
||||||
````
|
```toml
|
||||||
title = "TOML Example"
|
title = "TOML Example"
|
||||||
"sub title" = "Now with quoted keys"
|
"sub title" = "Now with quoted keys"
|
||||||
|
|
||||||
|
@ -140,9 +143,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");
|
||||||
|
@ -159,13 +162,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
|
||||||
|
@ -173,9 +176,9 @@ b = 3
|
||||||
[table]
|
[table]
|
||||||
c = 4
|
c = 4
|
||||||
d = 5
|
d = 5
|
||||||
````
|
```
|
||||||
|
|
||||||
````
|
```toml
|
||||||
a = 1
|
a = 1
|
||||||
|
|
||||||
[table]
|
[table]
|
||||||
|
@ -183,9 +186,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());
|
||||||
|
|
||||||
|
@ -195,7 +198,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
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue