Module toml4j

Class Toml

java.lang.Object
com.moandjiezana.toml.Toml

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 Details

    • Toml

      public Toml()
      Creates Toml instance with no defaults.
    • Toml

      public Toml(Toml defaults)
      Parameters:
      defaults - fallback values used when the requested key or table is not present in the TOML source that has been read.
  • Method Details

    • read

      public Toml read(File file)
      Populates the current Toml instance with values from file.
      Parameters:
      file - The File to be read. Expected to be encoded as UTF-8.
      Returns:
      this instance
      Throws:
      IllegalStateException - If file contains invalid TOML
    • read

      public Toml read(InputStream inputStream)
      Populates the current Toml instance with values from inputStream.
      Parameters:
      inputStream - Closed after it has been read.
      Returns:
      this instance
      Throws:
      IllegalStateException - If file contains invalid TOML
    • read

      public Toml read(Reader reader)
      Populates the current Toml instance with values from reader.
      Parameters:
      reader - Closed after it has been read.
      Returns:
      this instance
      Throws:
      IllegalStateException - If file contains invalid TOML
    • read

      public Toml read(Toml otherToml)
      Populates the current Toml instance with values from otherToml.
      Parameters:
      otherToml -
      Returns:
      this instance
    • read

      public Toml read(String tomlString) throws IllegalStateException
      Populates the current Toml instance with values from tomlString.
      Parameters:
      tomlString - String to be read.
      Returns:
      this instance
      Throws:
      IllegalStateException - If tomlString is not valid TOML
    • getString

      public String getString(String key)
    • getString

      public String getString(String key, String defaultValue)
    • getLong

      public Long getLong(String key)
    • getLong

      public Long getLong(String key, Long defaultValue)
    • getList

      public <T> List<T> getList(String key)
      Type Parameters:
      T - type of list items
      Parameters:
      key - a TOML key
      Returns:
      null if the key is not found
    • getList

      public <T> List<T> getList(String key, List<T> defaultValue)
      Type Parameters:
      T - type of list items
      Parameters:
      key - a TOML key
      defaultValue - a list of default values
      Returns:
      null is the key is not found
    • getBoolean

      public Boolean getBoolean(String key)
    • getBoolean

      public Boolean getBoolean(String key, Boolean defaultValue)
    • getDate

      public Date getDate(String key)
    • getDate

      public Date getDate(String key, Date defaultValue)
    • getDouble

      public Double getDouble(String key)
    • getDouble

      public Double getDouble(String key, Double defaultValue)
    • getTable

      public Toml getTable(String key)
      Parameters:
      key - A table name, not including square brackets.
      Returns:
      A new Toml instance or null if no value is found for key.
    • getTables

      public List<Toml> getTables(String key)
      Parameters:
      key - Name of array of tables, not including square brackets.
      Returns:
      A List of Toml instances or null if no value is found for key.
    • contains

      public boolean contains(String key)
      Parameters:
      key - a key name, can be compound (eg. a.b.c)
      Returns:
      true if key is present
    • containsPrimitive

      public boolean containsPrimitive(String key)
      Parameters:
      key - a key name, can be compound (eg. a.b.c)
      Returns:
      true if key is present and is a primitive
    • containsTable

      public boolean containsTable(String key)
      Parameters:
      key - a key name, can be compound (eg. a.b.c)
      Returns:
      true if key is present and is a table
    • containsTableArray

      public boolean containsTableArray(String key)
      Parameters:
      key - a key name, can be compound (eg. a.b.c)
      Returns:
      true if key is present and is a table array
    • isEmpty

      public boolean isEmpty()
    • to

      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:

      Type Parameters:
      T - type of targetClass.
      Parameters:
      targetClass - Class to deserialize TOML to.
      Returns:
      A new instance of targetClass.
    • toMap

      public Map<String,Object> toMap()
    • entrySet

      public Set<Map.Entry<String,Object>> entrySet()
      Returns:
      a Set of Map.Entry instances. Modifications to the Set are not reflected in this Toml instance. Entries are immutable, so Map.Entry.setValue(Object) throws an UnsupportedOperationException.