A Java library for representing, reading, validating and fixing externally stored files/configs.
RuntimeGoodies is a Java 8+ library that aims to solve a fundamental problem: reading & maintaining program configurations. It attemps to load external config files into declared POJOs with its custom annotations and validators. It even fixes the config when necessary (e.g an empty field is replaced by the default value and saved)
It comes with a few modules to support following formats:
- JSON (module:
com.github.iGoodie.RuntimeGoodies:json:<version-here>
) - TOML 0.4.0 (module:
com.github.iGoodie.RuntimeGoodies:toml:<version-here>
)
Out of the box, it comes with a built-in way to universally represent entities during runtime called Goodies. Goodies are serializable in any format desired. They have a very similar structure to JSON elements. Goodies do not exist outside the runtime, they are in-memory only.
This library uses JitPack to serve artifacts that can be depended on. If your project is a Gradle project, use following steps:
- Add JitPack repository to your
build.gradle
file:
allprojects {
repositories {
...
maven { url 'https://jitpack.io' }
}
}
- Depend on desired release. (Replace
<version-here>
with the version of choice)
dependencies {
implementation 'com.github.iGoodie.RuntimeGoodies:core:<version-here>'
implementation 'com.github.iGoodie.RuntimeGoodies:json:<version-here>' // Format of choice, see supported formats above
}
List of all available the versions can be found under GitHub
Releases
section. Alternatively, master-SNAPSHOT
can be used to depend on the nightly version (at your own risk).
- Create a POJO (ConfiGoodie) to represent the external config file. (In this example, external file is in JSON format)
public class ServerConfig extends JsonConfiGoodie {
@Goodie // <-- Annotate the fields you want to be exposed with @Goodie
String username, password;
@Goodie
Date launchDate; // Some values are converted from string!
@Goodie
int port = 3000; // You can declare default values!
@Goodie
DBConnection mongodbConnection; // Other ConfiGoodies/POJOs can be nested too!
@Goodie
DBConnection mysqlConnection = defaultValue(() -> {
DBConnection connection = new DBConnection();
connection.ports = Arrays.asList(3000, 3001, 3002);
return connection;
}); // You can declare default values via your own supplier!
public static class DBConnection {
@Goodie
String uri = "localhost:3000";
@Goodie
List<Integer> ports; // You can have Lists as well!
@Goodie
Map<String, String> aliases; // Guess what, Maps too!
}
}
- Instance that POJO and let RuntimeGoodie read (and fix if necessary) for you!
public class Test {
public static void main(String[] args) {
File configFile = new File("path/to/config.json");
// Tadaaa! Your config object is now read, validated and fixed accordingly.
ServerConfig config = new ServerConfig().readConfig(configFile);
// Note that, the call above will override the file if changes are made during fixing phase.
// Check out the Wiki, if you want to customize that behavior.
}
}
In order to learn more about what else you can do, check the Wiki out!
- Join our Discord Server - https://discordapp.com/invite/KNxxdvN
- Create an issue on Github: https://github.com/iGoodie/RuntimeGoodies/issues
- Contact iGoodie via Discord: iGoodie#1945
© 2021 Taha Anılcan Metinyurt (iGoodie)
For any part of this work for which the license is applicable, this work is licensed under the Attribution-NonCommercial-NoDerivatives 4.0 International license. (See LICENSE).