Class SimpleJsonConfig

java.lang.Object
com.teampotato.potacore.client.SimpleJsonConfig

public class SimpleJsonConfig extends Object
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

    Constructors
    Constructor
    Description
    SimpleJsonConfig(Path configPath)
    Constructs a new configuration for the specified file path.
  • Method Summary

    Modifier and Type
    Method
    Description
    <T> T
    get(String key)
    Retrieves a configuration value without explicit type checking.
    <T> T
    get(String key, Class<T> type)
    Type-safe method to retrieve a configuration value.
    void
    put(String key, Object value)
    Stores a key-value pair in the configuration and immediately persists to disk.
    void
    Reloads the configuration from disk, discarding any unsaved changes.
    void
    Saves the current configuration state to the JSON file.

    Methods inherited from class java.lang.Object

    clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
  • Constructor Details

    • SimpleJsonConfig

      public SimpleJsonConfig(Path configPath)
      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

      public void put(String key, Object value)
      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

      @Nullable public <T> T get(String key)
      Retrieves a configuration value without explicit type checking. Returns null if the key does not exist. Warning: Type mismatches will throw ClassCastException at 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 null if missing
    • get

      @Nullable public <T> T get(String key, Class<T> type)
      Type-safe method to retrieve a configuration value. Returns null if the key does not exist or the value type mismatches.
      Type Parameters:
      T - Type of the expected return value
      Parameters:
      key - Configuration key to retrieve
      type - Expected class of the return value (e.g., Boolean.class)
      Returns:
      Value cast to the requested type, or null if missing/type-mismatched