public class Toml extends Object
Provides access to the keys and tables in a TOML data source.
All getters can fall back to default values if they have been provided as a constructor argument.
Getters for simple values (String, Date, etc.) will return null if no matching key exists.
getList(String), getTable(String) and getTables(String) return empty values if there is no matching key.
All read methods throw an IllegalStateException if the TOML is incorrect.
Example usage:
Toml toml = new Toml().read(getTomlFile());
String name = toml.getString("name");
Long port = toml.getLong("server.ip"); // compound key. Is equivalent to:
Long port2 = toml.getTable("server").getLong("ip");
MyConfig config = toml.to(MyConfig.class);
| Constructor and Description |
|---|
Toml()
Creates Toml instance with no defaults.
|
Toml(Toml defaults) |
| Modifier and Type | Method and Description |
|---|---|
boolean |
contains(String key) |
boolean |
containsPrimitive(String key) |
boolean |
containsTable(String key) |
boolean |
containsTableArray(String key) |
Set<Map.Entry<String,Object>> |
entrySet() |
Boolean |
getBoolean(String key) |
Boolean |
getBoolean(String key,
Boolean defaultValue) |
Date |
getDate(String key) |
Date |
getDate(String key,
Date defaultValue) |
Double |
getDouble(String key) |
Double |
getDouble(String key,
Double defaultValue) |
<T> List<T> |
getList(String key) |
<T> List<T> |
getList(String key,
List<T> defaultValue) |
Long |
getLong(String key) |
Long |
getLong(String key,
Long defaultValue) |
String |
getString(String key) |
String |
getString(String key,
String defaultValue) |
Toml |
getTable(String key) |
List<Toml> |
getTables(String key) |
boolean |
isEmpty() |
Toml |
read(File file)
Populates the current Toml instance with values from file.
|
Toml |
read(InputStream inputStream)
Populates the current Toml instance with values from inputStream.
|
Toml |
read(Reader reader)
Populates the current Toml instance with values from reader.
|
Toml |
read(String tomlString)
Populates the current Toml instance with values from tomlString.
|
Toml |
read(Toml otherToml)
Populates the current Toml instance with values from otherToml.
|
<T> T |
to(Class<T> targetClass)
Populates an instance of targetClass with the values of this Toml instance.
|
Map<String,Object> |
toMap() |
public Toml()
public Toml(Toml defaults)
defaults - fallback values used when the requested key or table is not present in the TOML source that has been read.public Toml read(File file)
file - The File to be read. Expected to be encoded as UTF-8.IllegalStateException - If file contains invalid TOMLpublic Toml read(InputStream inputStream)
inputStream - Closed after it has been read.IllegalStateException - If file contains invalid TOMLpublic Toml read(Reader reader)
reader - Closed after it has been read.IllegalStateException - If file contains invalid TOMLpublic Toml read(Toml otherToml)
otherToml - public Toml read(String tomlString) throws IllegalStateException
tomlString - String to be read.IllegalStateException - If tomlString is not valid TOMLpublic <T> List<T> getList(String key)
T - type of list itemskey - a TOML keynull if the key is not foundpublic <T> List<T> getList(String key, List<T> defaultValue)
T - type of list itemskey - a TOML keydefaultValue - a list of default valuesnull is the key is not foundpublic Toml getTable(String key)
key - A table name, not including square brackets.null if no value is found for key.public List<Toml> getTables(String key)
key - Name of array of tables, not including square brackets.List of Toml instances or null if no value is found for key.public boolean contains(String key)
key - a key name, can be compound (eg. a.b.c)public boolean containsPrimitive(String key)
key - a key name, can be compound (eg. a.b.c)public boolean containsTable(String key)
key - a key name, can be compound (eg. a.b.c)public boolean containsTableArray(String key)
key - a key name, can be compound (eg. a.b.c)public boolean isEmpty()
public <T> T to(Class<T> targetClass)
Populates an instance of targetClass with the values of this Toml instance. The target's field names must match keys or tables. Keys not present in targetClass will be ignored.
Tables are recursively converted to custom classes or to Map<String, Object>.
In addition to straight-forward conversion of TOML primitives, the following are also available:
BigIntegerBigDecimalCharacterString, enum, URI, URLStringList, Set, array. The generic type can be anything that can be converted.Map<String, Object>T - type of targetClass.targetClass - Class to deserialize TOML to.Copyright © 2013–2017. All rights reserved.