This is a library that allows to easily work with configuration files in JSON format
repositories {
mavenCentral()
maven {
url "https://nexus.finwave.app/repository/maven-public"
}
}
dependencies {
implementation 'app.finwave.scw:finwave-scw:0.4.0'
}
File file = new File("main.conf");
RootConfig rootConfig = new RootConfig(file);
rootConfig.setValue("someValue", Math.random()); // set value
ConfigNode misc = rootConfig.subNode("misc");
misc.setValue("url", "https://example.com"); // write to subnode
rootConfig.save(); // save all
Calling rootConfig.save()
saves all changes from all nodes.
File file = new File("main.conf");
RootConfig rootConfig = new RootConfig(file);
rootConfig.load();
double value = rootConfig.getDouble("someValue").orElse(0d); // get saved value or 0
rootConfig.setValue("someValue", Math.random()); // overwrite old value
rootConfig.save(); // save it
Calling rootConfig.load()
load all config to memory.
Use several configuration files if you plan a huge variety of parameters
ConfigNode someNode = rootConfig.subNode("class");
someNode.setAs(new TestClass()); // now the node "class" has values from the object
rootConfig.save() // save changes
someNode.getAs(TestClass.class).orElse(new TestClass()); // get object with values from node or default
Architecturally, the node has a separate parameter with JsonElement type for storing an object inside itself.
Calling someNode.setAs()
clear node and set JsonElement parameter.
TestClass object = someNode.getAs(TestClass.class)
.orElse(new TestClass());
double value = object.variableName;
object.variableName = 123d;
someNode.setAs(object);
rootConfig.save();
- All parameters not from objects are translated to String. Therefore, to store more complex structures, such as arrays, you need to use separate objects
- There is no handling of cases where the configuration has been changed externally. Although initially the library was created to read the configuration only at startup and save changes (example config) at shutdown
- Without additional technical values in the config, it is impossible to determine whether the node was saved as a class. Therefore, it is not worth using the usual methods for obtaining values for class-nodes. And
getAs()
will not work at all for normal nodes. - The library has been tested in normal scenarios, but this does not exclude all errors. Please send bug reports here: [email protected] (en/ru)