Skip to content

Config formats

Guillaume R edited this page Jul 14, 2018 · 9 revisions

You may have noticed a configFormat() method in UnmodifiableConfig, that "returns the config's format". So NightConfig associates each configuration with a ConfigFormat<C> object.

Why?

In the previous versions of NightConfig, each configuration had a function to tell if it supported a given type of value or not. I realized that these values depended on the configuration format, and that creating a representation for the formats had several benefits, like easily changing the parser and writer used with a given configuration.

ConfigFormat

ConfigFormat, formally defined as ConfigFormat<C extends Config>, represents a configuration format like JSON, TOML, etc.

The generic parameter C defines the type of configuration that this format can create. Any configuration can be populated and written by any format, with only one condition: the configuration's values must be compatible with this format (see below).

Format characteristics

Each format can support different value types, for instance TOML supports DateTime and JSON doesn't. The method ConfigFormat.supportsType(Class<?>) checks if a given type of value is supported by the format.

Also, each format can, or not, support commented configurations. You can check this with the method supportsComments().

ConfigFormat<?> json = JsonFormat.fancyInstance();
ConfigFormat<?> toml = TomlFormat.instance();
boolean jsonHasComments = json.supportsComments(); // false
boolean tomlHasComments = toml.supportsComments(); // true

Creating a configuration of a given format

Once you've got an instance of ConfigFormat, use its createConfig() or createConcurrentConfig() to create a new configuration with that format. You can also use the static method Config.of(ConfigFormat).

ConfigFormat<?> inMemoryFormat = InMemoryFormat.defaultInstance();
Config inMemoryConfig = inMemoryFormat.createConfig();

Note: InMemoryFormat.defaultInstance().createConfig() is equivalent to Config.inMemory().

Enforcing value types

By default, NightConfig let you put any value in your configurations. Sometimes it can be useful to enforce that a Config contains only supported values. This can be done by wrapping the configuration with checked():

Config unchecked = Config.inMemory(); // supports primitive types, strings, collections and configs
Config checked = unchecked.checked();

unchecked.set("a", BigInteger.ONE); // ok, type isn't checked
checked.set("b", BigInteger.ONE); // error: unsupported type

The NightConfig formats

In Memory

These formats cannot create parsers nor writers.

Supported types Supports comments
InMemoryFormat.defaultInstance() standard:
  • Primitive types
  • Strings
  • java.util.Collection
  • com.electronwill.core.Config
No
InMemoryFormat.withUniversalSupport() all No
InMemoryFormat.withSupport(Predicate) defined by the predicate No
InMemoryCommentedFormat.defaultInstance() standard Yes
InMemoryCommentedFormat.withUniversalSupport() all Yes
InMemoryCommentedFormat.withSupport(Predicate) defined by the predicate Yes

Files

These formats can create parsers and writers.

Supported types Supports comments Notes
JsonFormat.minimalInstance() standard No Creates minimal json writers that write no spaces nor newlines
JsonFormat.fancyInstance() standard No Creates fancy, configurable json writers
JsonFormat.emptyTolerantInstance() standard No Fancy json writer that supports empty input data
JsonFormat.minimalEmptyTolerantInstance() standard No Minimal json writer that supports empty input data
TomlFormat.instance() standard + java.time.Temporal (and subclasses such as LocalDateTime) Yes
HoconFormat.instance() standard Yes
YamlFormat.defaultInstance()
  • Primitive types
  • Strings
  • Set
  • List
  • Config
  • java.util.Date
  • java.sql.Date
  • java.sql.Timestamp
Yes
YamlFormat.configuredInstance(Yaml) same as defaultInstance Yes A YamlFormat with a special instance of org.yaml.snakeyaml.Yaml for additional settings