Class JsonMurmel

java.lang.Object
de.murmelmeister.library.configuration.JsonMurmel

public class JsonMurmel extends Object
JsonMurmel is a utility class for reading and writing JSON data to a file. It provides methods to get and set values in a JSON structure, supporting nested keys using dot notation. The class is thread-safe, allowing concurrent read and write operations.
  • Field Summary

    Fields
    Modifier and Type
    Field
    Description
    private final com.google.gson.Gson
     
    private final ReadWriteLock
     
    private final Path
     
  • Constructor Summary

    Constructors
    Constructor
    Description
    JsonMurmel(String fileName)
    Constructs a new JsonMurmel instance for handling JSON operations.
    Constructs a new JsonMurmel instance for handling JSON operations.
  • Method Summary

    Modifier and Type
    Method
    Description
    <T> T
    getValue(String key, Class<T> type)
    Retrieves the value associated with the specified key from the JSON file.
    <T> T
    getValue(String key, Class<T> type, T fallback)
    Retrieves the value associated with the specified key from the JSON file.
    void
    Removes a key from the JSON file.
    <T> void
    setValue(String key, T value)
    Sets the value associated with the specified key in the JSON file.

    Methods inherited from class java.lang.Object

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

    • path

      private final Path path
    • gson

      private final com.google.gson.Gson gson
    • lock

      private final ReadWriteLock lock
  • Constructor Details

    • JsonMurmel

      public JsonMurmel(String fileName)
      Constructs a new JsonMurmel instance for handling JSON operations.
      Parameters:
      fileName - The name of the file path to the JSON file that this instance will operate on.
    • JsonMurmel

      public JsonMurmel(Path path)
      Constructs a new JsonMurmel instance for handling JSON operations.
      Parameters:
      path - The path to the JSON file that this instance will operate on.
  • Method Details

    • getValue

      public <T> T getValue(String key, Class<T> type)
      Retrieves the value associated with the specified key from the JSON file. If the key does not exist or contains a null value, this method returns null.
      Type Parameters:
      T - The type of the value to retrieve.
      Parameters:
      key - The key whose associated value is to be returned, using dot notation for nested keys (e.g., "parent.child.key").
      type - The expected class type of the value to be retrieved.
      Returns:
      The value associated with the specified key, or null if the key does not exist or contains a null value.
      Throws:
      JsonMurmelException - If an error occurs while reading the JSON file.
    • getValue

      public <T> T getValue(String key, Class<T> type, T fallback)
      Retrieves the value associated with the specified key from the JSON file. If the key does not exist or contains a null value, the fallback value will be returned. Additionally, the fallback value will be set to the specified key in the JSON file.
      Type Parameters:
      T - The type of the value to retrieve.
      Parameters:
      key - The key whose associated value is to be returned, using dot notation for nested keys (e.g., "parent.child.key").
      type - The expected class type of the value to be retrieved.
      fallback - The fallback value to return and set if the key is not found or is null.
      Returns:
      The value associated with the specified key, or the fallback value if the key does not exist or contains a null value.
      Throws:
      JsonMurmelException - If an error occurs while reading or writing the JSON file.
    • setValue

      public <T> void setValue(String key, T value)
      Sets the value associated with the specified key in the JSON file. If the key does not exist, it will be created. Nested keys can be specified using dot notation (e.g., "parent.child.key"). If the file or parent directories do not exist, they will be created.
      Parameters:
      key - The key to associate the value with, using dot notation for nested keys.
      value - The value to associate with the specified key.
      Throws:
      JsonMurmelException - If an error occurs while reading or writing the JSON file.
    • removeKey

      public void removeKey(String key)
      Removes a key from the JSON file. If the key is not found, no action is taken. If the parent object becomes empty after removing the key, it will also be removed.
      Parameters:
      key - The key to remove, using dot notation for nested keys (e.g., "parent.child.key").
      Throws:
      JsonMurmelException - If an error occurs while reading or writing the JSON file.