Class JsonConfig
java.lang.Object
com.thecsdev.common.config.JsonConfig
- Direct Known Subclasses:
ModConfig
A container for various configurable properties that can be saved and to and
loaded from the user's disk drive.
This configuration system uses reflection to save and load properties found within a given container.
The data is saved and loaded in JSON format.
This configuration system uses reflection to save and load properties found within a given container.
The data is saved and loaded in JSON format.
- See Also:
- API Note:
- Depends on Guava -
com.google.*.
-
Field Summary
FieldsModifier and TypeFieldDescriptionprotected @Nullable FileThe mainFilewhere thisJsonConfigis to be stored at.static final com.google.gson.GsonThe mainGsoninstance used byJsonConfig, for serialization and deserialization of config properties. -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionprotected @Nullable FileReturns theFilethat is used bysaveToFile()andloadFromFile()when saving and loading thisJsonConfig.getDeserializableProperties(@NotNull Class<? extends JsonConfig> clazz) FromgetProperties(Class), filters out only deserializable properties.getProperties(@NotNull Class<? extends JsonConfig> clazz) Returns aStreamof allFields (including inherited) that should be handled during serialization and deserialization of the givenJsonConfigClass.static String[]getPropertyNames(@NotNull Field property) Resolves the serialized names for aFieldusing itsSerializedNameannotation (if present), returning the primary name followed by any alternates.getSerializableProperties(@NotNull Class<? extends JsonConfig> clazz) FromgetProperties(Class), filters out only serializable properties.final voidfinal voidloadFromJson(@NotNull com.google.gson.JsonObject from) Loads thisJsonConfigfrom aJsonObject, overriding any properties of thisJsonConfigthat were stored in theJsonObject.protected voidonLoad(@NotNull com.google.gson.JsonObject from) Called afterloadFromJson(JsonObject)finishes its execution.protected voidonSave(@NotNull com.google.gson.JsonObject to) Called aftersaveToJson(JsonObject)finishes its execution.final voidfinal @NotNull com.google.gson.JsonObjectfinal voidsaveToJson(@NotNull com.google.gson.JsonObject to) Saves thisJsonConfigto an existingJsonObjectinstance.protected voidsetConfigFile(@Nullable File file) Sets theFilethat is used bysaveToFile()andloadFromFile()when saving and loading thisJsonConfig.
-
Field Details
-
GSON
@Internal public static final com.google.gson.Gson GSONThe mainGsoninstance used byJsonConfig, for serialization and deserialization of config properties. -
configFile
The mainFilewhere thisJsonConfigis to be stored at.
-
-
Constructor Details
-
JsonConfig
public JsonConfig() -
JsonConfig
-
-
Method Details
-
getConfigFile
Returns theFilethat is used bysaveToFile()andloadFromFile()when saving and loading thisJsonConfig.- See Also:
-
setConfigFile
Sets theFilethat is used bysaveToFile()andloadFromFile()when saving and loading thisJsonConfig.- Parameters:
file- The newFilevalue.- See Also:
-
onSave
Called aftersaveToJson(JsonObject)finishes its execution.- Parameters:
to- TheJsonObjectthe data was saved to.
-
onLoad
Called afterloadFromJson(JsonObject)finishes its execution.- Parameters:
from- TheJsonObjectthe data was loaded from.
-
saveToJson
@NotNull public final @NotNull com.google.gson.JsonObject saveToJson() -
saveToJson
public final void saveToJson(@NotNull @NotNull com.google.gson.JsonObject to) throws NullPointerException Saves thisJsonConfigto an existingJsonObjectinstance.- Parameters:
to- TheJsonObjectto save to.- Throws:
NullPointerException- If the argument isnull.
-
loadFromJson
public final void loadFromJson(@NotNull @NotNull com.google.gson.JsonObject from) throws NullPointerException Loads thisJsonConfigfrom aJsonObject, overriding any properties of thisJsonConfigthat were stored in theJsonObject. Mismatched types andnullvalues do not override existing values.- Parameters:
from- TheJsonObjectto load from.- Throws:
NullPointerException- If the argument isnull.
-
saveToFile
- Throws:
NullPointerException- IfgetConfigFile()isnull.IOException- If anIOExceptiontakes place.
-
loadFromFile
- Throws:
NullPointerException- IfgetConfigFile()isnull.IOException- If anIOExceptiontakes place.
-
getProperties
public static final Stream<Field> getProperties(@NotNull @NotNull Class<? extends JsonConfig> clazz) throws NullPointerException Returns aStreamof allFields (including inherited) that should be handled during serialization and deserialization of the givenJsonConfigClass.Fields are included only if they meet all the following conditions:
- Are not static
- Are not transient
- Are not arrays
- Are primitive types or
SerializableorJsonConfig - Are annotated with Expose
- Parameters:
clazz- theJsonConfigclass to inspect.- Returns:
- a
Streamof eligibleFields. - Throws:
NullPointerException- If the argument isnull.
-
getSerializableProperties
public static final Stream<Field> getSerializableProperties(@NotNull @NotNull Class<? extends JsonConfig> clazz) throws NullPointerException FromgetProperties(Class), filters out only serializable properties.- Parameters:
clazz- theJsonConfigclass to inspect.- Throws:
NullPointerException- If the argument isnull.
-
getDeserializableProperties
public static final Stream<Field> getDeserializableProperties(@NotNull @NotNull Class<? extends JsonConfig> clazz) throws NullPointerException FromgetProperties(Class), filters out only deserializable properties.- Parameters:
clazz- TheJsonConfigclass to inspect.- Throws:
NullPointerException- If the argument isnull.
-
getPropertyNames
public static String[] getPropertyNames(@NotNull @NotNull Field property) throws NullPointerException Resolves the serialized names for aFieldusing itsSerializedNameannotation (if present), returning the primary name followed by any alternates.- Parameters:
property- TheFieldto inspect.- Returns:
- An array of names to use for serialization/deserialization (never null, non-empty).
- Throws:
NullPointerException- If the argument isnull.
-