mirror of
https://github.com/plexusorg/toml4j.git
synced 2024-12-29 11:42:15 +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):
|
||||
|
||||
````xml
|
||||
```xml
|
||||
<dependency>
|
||||
<groupId>com.moandjiezana.toml</groupId>
|
||||
<artifactId>toml4j</artifactId>
|
||||
<version>0.3.1</version>
|
||||
</dependency>
|
||||
````
|
||||
```
|
||||
|
||||
Requires Java 1.6.
|
||||
|
||||
## Quick start
|
||||
|
||||
````java
|
||||
```java
|
||||
Toml toml = new Toml().parse(getTomlFile());
|
||||
String someValue = toml.getString("someKey");
|
||||
Date someDate = toml.getDate("someTable.someDate");
|
||||
MyClass myClass = toml.to(MyClass.class);
|
||||
````
|
||||
```
|
||||
|
||||
## Usage
|
||||
|
||||
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");
|
||||
````
|
||||
```
|
||||
|
||||
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
|
||||
name = "Mwanji Ezana"
|
||||
|
||||
[address]
|
||||
|
@ -54,9 +54,9 @@ name = "Mwanji Ezana"
|
|||
|
||||
[contacts]
|
||||
"email address" = me@example.com
|
||||
````
|
||||
```
|
||||
|
||||
````java
|
||||
```java
|
||||
class Address {
|
||||
String street;
|
||||
String city;
|
||||
|
@ -67,28 +67,31 @@ class User {
|
|||
Address address;
|
||||
Map<String, Object> contacts;
|
||||
}
|
||||
````
|
||||
```
|
||||
|
||||
````java
|
||||
```java
|
||||
User user = new Toml().parse(tomlFile).to(User.class);
|
||||
|
||||
assert user.name.equals("Mwanji Ezana");
|
||||
assert user.address.street.equals("123 A Street");
|
||||
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.
|
||||
|
||||
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`
|
||||
* 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`
|
||||
* Multiline and literal TOML strings can be converted to `String`
|
||||
* A TOML array can be converted to a `List`, `Set` or array. The generic type can be anything that can be converted.
|
||||
* 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.
|
||||
TOML | Java
|
||||
---- | ----
|
||||
Integer | `int`, `long` (or wrapper), `java.math.BigInteger`
|
||||
Float | `float`, `double` (or wrapper), `java.math.BigDecimal`
|
||||
String | `String`, enum, `java.net.URI`, `java.net.URL`
|
||||
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.
|
||||
|
||||
|
@ -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.
|
||||
|
||||
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"
|
||||
"sub title" = "Now with quoted keys"
|
||||
|
||||
|
@ -140,9 +143,9 @@ title = "TOML Example"
|
|||
location = "Geneva"
|
||||
[[networks.operators]]
|
||||
location = "Paris"
|
||||
````
|
||||
```
|
||||
|
||||
````java
|
||||
```java
|
||||
Toml toml = new Toml().parse(getTomlFile());
|
||||
|
||||
String title = toml.getString("title");
|
||||
|
@ -159,13 +162,13 @@ Toml network1 = toml.getTable("networks[0]");
|
|||
String network2Name = toml.getString("networks[1].name"); // "Level 2"
|
||||
List<Toml> network3Operators = toml.getTables("networks[2].operators");
|
||||
String network3Operator2Location = toml.getString("networks[2].operators[1].location"); // "Paris"
|
||||
````
|
||||
```
|
||||
|
||||
### 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.
|
||||
|
||||
````
|
||||
```toml
|
||||
# defaults
|
||||
a = 2
|
||||
b = 3
|
||||
|
@ -173,9 +176,9 @@ b = 3
|
|||
[table]
|
||||
c = 4
|
||||
d = 5
|
||||
````
|
||||
```
|
||||
|
||||
````
|
||||
```toml
|
||||
a = 1
|
||||
|
||||
[table]
|
||||
|
@ -183,9 +186,9 @@ a = 1
|
|||
|
||||
[[array]]
|
||||
d = 3
|
||||
````
|
||||
```
|
||||
|
||||
````java
|
||||
```java
|
||||
Toml defaults = new Toml().parse(getDefaultsFile());
|
||||
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 tableD = toml.getLong("table.d"); // returns null, not 5
|
||||
Long arrayD = toml.getLong("array[0].d"); // returns 3
|
||||
````
|
||||
```
|
||||
|
||||
### Limitations
|
||||
|
||||
|
|
Loading…
Reference in a new issue