Skip to content

A simple java json configuration system built with gson.

Notifications You must be signed in to change notification settings

deekappyok/simple-configuration

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

33 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Simple Configuration

A simple json configuration system built with gson.

Setup 💻

Using Maven

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>

Usage 📚

Initialize

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
    }

}

Config Class

@ConfigurationFile(
    directory = "./configs/test",
    file = "config.json"
)
class Config {
    private String name;
    @ConfigurationExclusion
    private int age;
}