https://github.com/supertassu/config
A java configuration utility
https://github.com/supertassu/config
api config configuration hocon java
Last synced: 6 months ago
JSON representation
A java configuration utility
- Host: GitHub
- URL: https://github.com/supertassu/config
- Owner: supertassu
- License: mit
- Created: 2018-10-15T07:39:19.000Z (almost 7 years ago)
- Default Branch: master
- Last Pushed: 2018-10-15T07:43:39.000Z (almost 7 years ago)
- Last Synced: 2025-02-09T06:14:48.805Z (8 months ago)
- Topics: api, config, configuration, hocon, java
- Language: Java
- Size: 63.5 KB
- Stars: 0
- Watchers: 2
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# Config `1.0.01`
A java configuration utility
## Getting started
### Add the dependency```groovy
repositories {
mavenCentral()
maven {
name = 'sponge-repo'
url = 'https://repo.spongepowered.org/maven'
}
maven {
name = 'tassu-repo'
url = 'https://maven.tassu.me/'
}
}dependencies {
// choose one:
compile 'me.tassu.cfg:hocon:(version)'
compile 'me.tassu.cfg:yaml:(version)'
}
```No example for Maven because Maven is bad.
### Create the ConfigFactory
```java
import me.tassu.cfg.ConfigFactory;
import me.tassu.cfg.impl.HoconConfigFactory;void loadConfig() {
final Path path = /* ... */;
final ConfigFactory factory = new HoconConfigFactory(path);
}
```### Create a config class
```java
package me.tassu.publicplugin;import com.google.common.collect.ImmutableMap;
import com.google.common.collect.Lists;
import me.tassu.cfg.AbstractConfig;
import me.tassu.cfg.ConfigFactory;
import ninja.leaping.configurate.objectmapping.ObjectMapper;
import ninja.leaping.configurate.objectmapping.ObjectMappingException;
import ninja.leaping.configurate.objectmapping.Setting;import java.util.List;
import java.util.Map;@SuppressWarnings("FieldCanBeLocal")
public class ExampleConfig extends AbstractConfig {public ExampleConfig(ConfigFactory factory) {
// get configuration loader for "example.conf"
loader = factory.getLoader("example");try {
// logic handled by AbstractConfig
this.configMapper = ObjectMapper.forObject(this);
} catch (ObjectMappingException e) {
throw new RuntimeException(e);
}
}@Setting
private int exampleInt = 0;@Setting
private String exampleString = "Hello!";@Setting(comment = "Example comment")
private Map exampleMap = ImmutableMap.builder()
.put("first key", "first value")
.put("second key", "second value")
.build();@Setting
private List exampleList = Lists.newArrayList(1.5f);public int getExampleInt() {
return exampleInt;
}public String getExampleString() {
return exampleString;
}public Map getExampleMap() {
return exampleMap;
}public List getExampleList() {
return exampleList;
}
}
```### Load and use the configuration
```java
import me.tassu.cfg.ConfigFactory;
import me.tassu.cfg.impl.HoconConfigFactory;void loadConfig() {
final Path path = /* ... */;
final ConfigFactory factory = new HoconConfigFactory(path);
final ExampleConfig config = new ExampleConfig(factory);try {
config.load();
config.save();
} catch (IOException | ObjectMappingException e) {
throw new RuntimeException(e);
}
System.out.println("Example int: " + config.getExampleInt());
System.out.println("Example string: " + config.getExampleString());
System.out.println("Example map: " + config.getExampleMap());
System.out.println("Example list: " + config.getExampleList());
}
```### Result
```hocon
exampleInt=0
exampleList=[
1.5
]
# Example comment
exampleMap {
"first key"="first value"
"second key"="second value"
}
exampleString="Hello!"
```# License
- MIT