Skip to content

Basic Usage

frosxt edited this page Jan 18, 2026 · 1 revision

Basic Usage

This guide covers loading, reading, writing, and saving JSON configurations.

1. Loading a Configuration

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);

2. Accessing Values

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");

3. Setting Values

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");

4. Saving

Once you have modified the configuration, save it back to a file.

try {
    config.save(file);
} catch (IOException e) {
    e.printStackTrace();
}

Complete Example

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"));
    }
}

Clone this wiki locally