A simple json configuration system built with gson.
REPOSITORY
<repositories>
<repository>
<id>eu.dkcode</id>
<url>https://repo.dkcode.eu</url>
</repository>
</repositories>
DEPENDENCY
<dependency>
<groupId>eu.dkcode</groupId>
<artifactId>json-configuration</artifactId>
<version>1.3-SNAPSHOT</version> <!-- or replace with newer version -->
</dependency>
class Bootstrap {
private final ConfigLoader configLoader;
public Bootstrap() {
configLoader = new ConfigLoader(); // you can add you own gson instance
/*
loading
*/
Config config = configLoader.load(Config.class, new Config()); // pass the class and default options
config.setName("John");
/*
save
*/
configLoader.save(Config.class, config); // pass the class and the config object
/*
add custom adapters
*/
configLoader.addAdapter(NewObject.class, new NewAdapter());
/*
add any other custom properties to GsonBuilder
*/
GsonBuilder gsonBuilder = configLoader.getGsonBuilder(); // get the gson builder
gsonBuilder.registerTypeAdapter(ExampleObject.class, new ExampleAdapter());
gsonBuilder.excludeFieldsWithoutExposeAnnotation();
configLoader.setGsonBuilder(gsonBuilder); // re set the gson builder
}
}
@ConfigurationFile(
directory = "./configs/test",
file = "config.json"
)
class Config {
private String name;
@ConfigurationExclusion
private int age;
}