-
Notifications
You must be signed in to change notification settings - Fork 0
Basic Usage
frosxt edited this page Jan 18, 2026
·
1 revision
This guide covers loading, reading, writing, and saving JSON configurations.
You can load a configuration from a File, Reader, or String.
import com.github.frosxt.jsonconfig.api.JsonConfiguration;
File file = new File("config.json");
// Load from file (creates a new empty config if file doesn't exist/is empty is NOT automatic, you must handle existence)
JsonConfiguration config = JsonConfiguration.loadConfiguration(file);Use the getter methods to retrieve values. You can provide a default value to be returned if the key is missing.
// Get simple values
String host = config.getString("server.host");
int port = config.getInt("server.port", 8080); // 8080 is the default
boolean debug = config.getBoolean("debug");
// Get a list
List<String> users = config.getStringList("whitelist");Use set(path, value) to modify the configuration.
config.set("server.host", "127.0.0.1");
config.set("server.port", 9090);
config.set("whitelist", List.of("alice", "bob"));
// You can create new nested sections automatically
config.set("database.credentials.username", "admin");Once you have modified the configuration, save it back to a file.
try {
config.save(file);
} catch (IOException e) {
e.printStackTrace();
}public class App {
public static void main(String[] args) throws IOException {
File configFile = new File("config.json");
JsonConfiguration config = JsonConfiguration.loadConfiguration(configFile);
// Set defaults if missing
if (!config.contains("setup")) {
config.set("setup", true);
config.set("version", 1);
config.save(configFile);
}
System.out.println("Version: " + config.getInt("version"));
}
}