Package com.teampotato.potacore.client
Class SimpleJsonConfig
java.lang.Object
com.teampotato.potacore.client.SimpleJsonConfig
Manages a JSON-based configuration file with simple key-value storage.
This class handles loading, saving, and reloading configurations from/to a JSON file,
and provides type-safe methods to access configuration values. If the configuration file
or parent directories do not exist, they are automatically created.
Example usage:
SimpleJsonConfig config = new SimpleJsonConfig(Paths.get("config/config.json"));
config.put("enableFeature", true);
config.saveConfig();
boolean featureEnabled = config.get("enableFeature", Boolean.class);
Thread Safety: This class is not thread-safe. External synchronization is required if used in concurrent environments.
-
Constructor Summary
ConstructorsConstructorDescriptionSimpleJsonConfig(Path configPath) Constructs a new configuration for the specified file path. -
Method Summary
Modifier and TypeMethodDescription<T> TRetrieves a configuration value without explicit type checking.<T> TType-safe method to retrieve a configuration value.voidStores a key-value pair in the configuration and immediately persists to disk.voidReloads the configuration from disk, discarding any unsaved changes.voidSaves the current configuration state to the JSON file.
-
Constructor Details
-
SimpleJsonConfig
Constructs a new configuration for the specified file path. Automatically loads existing configuration or initializes a new file if missing.- Parameters:
configPath- Path to the JSON configuration file (e.g.,Paths.get("config/settings.json"))- Throws:
RuntimeException- If file/directory creation fails or JSON parsing errors occur during initial load.
-
-
Method Details
-
saveConfig
public void saveConfig()Saves the current configuration state to the JSON file.- Throws:
RuntimeException- If writing to the file fails (e.g., I/O errors or insufficient permissions).
-
reloadConfig
public void reloadConfig()Reloads the configuration from disk, discarding any unsaved changes.- Throws:
RuntimeException- If file reading or JSON parsing fails during reload.
-
put
Stores a key-value pair in the configuration and immediately persists to disk.- Parameters:
key- Configuration key (case-sensitive)value- Configuration value (must be serializable by Gson)
-
get
Retrieves a configuration value without explicit type checking. Returnsnullif the key does not exist. Warning: Type mismatches will throwClassCastExceptionat runtime.- Type Parameters:
T- Inferred return type (unsafe - no compile-time validation)- Parameters:
key- Configuration key to retrieve- Returns:
- Value associated with the key, or
nullif missing
-
get
Type-safe method to retrieve a configuration value. Returnsnullif the key does not exist or the value type mismatches.- Type Parameters:
T- Type of the expected return value- Parameters:
key- Configuration key to retrievetype- Expected class of the return value (e.g.,Boolean.class)- Returns:
- Value cast to the requested type, or
nullif missing/type-mismatched
-