Class JsonMurmel
java.lang.Object
de.murmelmeister.library.configuration.JsonMurmel
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
FieldsModifier and TypeFieldDescriptionprivate final com.google.gson.Gsonprivate final ReadWriteLockprivate final Path -
Constructor Summary
ConstructorsConstructorDescriptionJsonMurmel(String fileName) Constructs a new JsonMurmel instance for handling JSON operations.JsonMurmel(Path path) Constructs a new JsonMurmel instance for handling JSON operations. -
Method Summary
Modifier and TypeMethodDescription<T> TRetrieves the value associated with the specified key from the JSON file.<T> TRetrieves the value associated with the specified key from the JSON file.voidRemoves a key from the JSON file.<T> voidSets the value associated with the specified key in the JSON file.
-
Field Details
-
path
-
gson
private final com.google.gson.Gson gson -
lock
-
-
Constructor Details
-
JsonMurmel
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
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
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
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
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
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.
-