Welcome to the JC4J! This library provides you with easy management of your .json configuration files! With JC4J you can create configs and easily read them avoiding unsafe casts and boilerplate code.
This README is for the version 3.4b!
If you have any questions, or you have found bugs, please contact with me in my discord — Nedelis#9496!
You can find docs here
You have 2 options to install this library.
- Download the latest release from the releases page
- Import downloaded
.JARfile into your project - That's all.
soon...
Return to the table of contents
This library uses custom logging system, that can be configured in the way you want.
By default, logger has name JC4J, logging level ALL and message pattern HH:mm:ss logger-name [message-log-level] message.
To change these settings, you need reassign LOGGER variable that can be found in the class JSONConfig4Java.
Here is an example:
// package ...;
// import ...;
@SuppressWarnings("ALL")
public class Foo {
// You should customize logger in the initialization function (for example, here, I do it in the main function).
public static void main(String[] args) {
// first, create a new logger
JC4JLogger NEW_LOGGER = new JC4JLoggerBuilder(/*here you can put custom name*/).
setPathToLogFile(Path.of("./logs/log.log")).
setLogLevel(JC4JLogLevel.ALL).
/*you can also customize message pattern by setPattern()*/
build();
// next, reassign the default logger
JSONConfig4Java.LOGGER = NEW_LOGGER;
}
}That's all. If you want to find out details about logging system, you can read javadocs.
Return to the table of contents
In this section, I'll give you 2 examples. First will be simple and second will be hard. But, at first, I recommend you to read javadocs during reading this examples.
Return to the table of contents
Examples list:
Well, let's imagine that we have this default config, at the directory ./resources/default_settings.json:
{
"window-size": "1000:1000",
"loudness": 100,
"gui-size-amplifier": 1.25,
"auto-save": true
}So, now we need to get all these settings. Let's create a new class that will contain all settings.
// package ...;
// import... ;
public class Settings {
// At first, let's wrap the config with wrapper
private static final ConfigWrapper configWrapper = ConfigWrapper.of(
Path.of("./config").normalize().toString(),
"config",
Path.of("./resources/default_settings.json").normalize().toFile()
);
// The first parameter is the path to the modifiable config file that will be generated
// Second is the name of the modifiable config file
// Third is the default config file
// Now, we are able to get all the settings
public static final String WINDOW_SIZE = configWrapper.get("window-size").toJavaValue(JSONConfigValueType.STR);
// So, at first we get parameter from the config, and then we convert it to java value.
// p.s. function toJavaValue() at the JSONValue class has 2 variations:
// here we used variation, that specifies return type, but also we could use default value instead
public static final int LOUDNESS = configWrapper.getAsJavaValue("loudness", JSONConfigValueType.INT);
public static final double GUI_AMPLIFIER = configWrapper.getAsJavaValue("gui-size-amplifier", JSONConfigValueType.DOUBLE);
public static final boolean AUTO_SAVE = configWrapper.getAsJavaValue("auto-save", JSONConfigValueType.BOOL);
}That's all. You can find other ways to get config's values by reading javadocs.
Return to the table of contents
Return to the list of examples
Well, now let's imagine, that our last config became bigger:
{
"main-settings": {
"gui": {
"gui-size-amplifier": 1.25
},
"graphics": {
"textures-resolution": "medium",
"antialiasing": "MSAA 2x",
"reflections": "medium",
"sky-resolution": "medium",
"draw-distance": "medium",
"load-distance": "medium",
"shadows": "ultra",
"particles": "all",
"ray-tracing": false
},
"sound": {
"type": "stereo",
"master-volume": 100,
"surrounding": 100,
"music": 100,
"entities": 100,
"objects": 100,
"weather": 100
}
},
"other-settings": {
"auto-save": true
},
"modifications-list": [
"com.example.mods:optimization-pack-v3.4"
]
}Looks pretty big, isn't it? Well, now let's imagine that at some moment we need to get entities loudness, antialiasing type, modifications list and check whether auto-save is enabled or not.
// package... ;
// import... ;
public class Settings {
// Create wrapper
private static final ConfigWrapper configWrapper = ConfigWrapper.of(
Path.of("./config").normalize().toString(),
"config",
Path.of("./resources/default_settings.json").normalize().toFile()
);
public static final int SOUND_ENTITIES = configWrapper.
getAsJavaValue("main-settings", JSONValueType.JS_VAL_MAP).
get("sound").toJavaValue(JSONValueType.JS_VAL_MAP).
get("entities").toJavaValue(JSONValueType.INT);
// So, here, at first, we got the 'main-settings' map from the config
// Then we got 'sound' map from the 'main-settings' map
// And then we finally found 'entities' sound value
public static final String ANTIALIASING_TYPE = configWrapper.
getAsJavaValue("main-settings", JSONValueType.JS_VAL_MAP).
get("graphics").toJavaValue(JSONValueType.JS_VAL_MAP).
get("antialiasing").toJavaValue(JSONValueType.STR);
public static final List<?> MODIFICATIONS = configWrapper.getAsJavaValue("modifications-list", JSONValueType.JS_VAL_LIST);
public static final boolean AUTO_SAVE = configWrapper.
getAsJavaValue("other-settings", JSONValueType.JS_VAL_MAP).
get("auto-save").toJavaValue(JSONValueType.BOOL);
}Phew, that's all! All other settings can be obtained in the same way. I hope that now you understand how to use JC4J!
Return to the table of contents
Return to the list of examples
Well, in future I'm going to add:
- Config bundles
- Runtime config change
- Links system that will allow you to get any config value by special string.