Package dev.selena.luacore.utils.config
Class ConfigLoader
java.lang.Object
dev.selena.luacore.utils.config.ConfigLoader
Config loader used to load and save json config files
-
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionstatic <T> TloadConfig(Class<T> cls, File file) Loads a provided config object from a given JSON file.static voidsaveConfig(Object config, File file) Saves a config object to the specified file in JSON formatstatic StringtoJsonWithComments(Object obj, com.google.gson.Gson gson) Converts an Object to a JSON string using GSON and injects the value of the @Comment annotation above
-
Constructor Details
-
ConfigLoader
public ConfigLoader()
-
-
Method Details
-
loadConfig
public static <T> T loadConfig(Class<T> cls, File file) throws IOException, InstantiationException, IllegalAccessException, InvocationTargetException, NoSuchMethodException Loads a provided config object from a given JSON file. If the file does not exist, it also creates the file using the given object defaults- Type Parameters:
T- The class type you want to map to- Parameters:
cls- The object type you wish to load, also dictates the class of the returned objectfile- The file that is to be created/read from- Returns:
- The object loaded from file
- Throws:
IOException- Thrown when the file cannot be foundInstantiationException- Throwing when the class has an initialization issueIllegalAccessException- Thrown when trying to access a field without the correct access levelInvocationTargetException- Thrown when the mapper class has a constructor that does not match the arguments (There should be none)NoSuchMethodException- Thrown when the class somehow manages not to have a constructor
-
toJsonWithComments
Converts an Object to a JSON string using GSON and injects the value of the @Comment annotation above- Parameters:
obj- The object to convert to JSON.gson- The Gson instance used for serialization.- Returns:
- A JSON string with comments for each field.
-
saveConfig
Saves a config object to the specified file in JSON format- Parameters:
config- The object to be savedfile- The file to which the object is saved- Throws:
IOException- Thrown when the file has some issue saving
-