Package enterprises.iwakura.keqing.impl
Class GsonSerializer
java.lang.Object
enterprises.iwakura.keqing.Serializer<com.google.gson.JsonElement>
enterprises.iwakura.keqing.impl.GsonSerializer
public class GsonSerializer
extends enterprises.iwakura.keqing.Serializer<com.google.gson.JsonElement>
Gson serializer for
Keqing. Supports file types with ".json" extension.-
Field Summary
FieldsModifier and TypeFieldDescriptionstatic final StringThe file extension supported by this serializer.protected final com.google.gson.GsonThe Gson instance used for serialization and deserialization.Fields inherited from class enterprises.iwakura.keqing.Serializer
serializedFileContents -
Constructor Summary
ConstructorsConstructorDescriptionCreates a new instance of GsonSerializer with a default Gson configuration. -
Method Summary
Modifier and TypeMethodDescriptionprotected voiddeepMerge(com.google.gson.JsonObject source, com.google.gson.JsonObject target) Deep merges the source JsonObject into the target JsonObject.protected com.google.gson.JsonElementgetElementByPath(com.google.gson.JsonElement jsonElement, String path) Retrieves a nested JsonElement from the given JsonElement based on the specified dot-separated path.protected Class<?> getWrapperClass(Class<?> clazz) Returns the wrapper class for a given primitive class.protected com.google.gson.JsonArraymergeArrays(List<com.google.gson.JsonElement> jsonElements) Merges a list of JsonElements into a single JsonArray by concatenation.protected com.google.gson.JsonObjectmergeObjects(List<com.google.gson.JsonElement> jsonElements) Merges a list of JsonElements into a single JsonObject by deep merging.protected <T> TparseElement(com.google.gson.JsonElement element, Class<T> clazz) Parses a JsonElement into an instance of the specified class type.<T> Optional<T> readProperty(String postfix, String path, Class<T> clazz) This method is unsupported in GsonSerializer.<T> Optional<T> Reads a property from the serialized JsonElement associated with the given postfix and path, merging multiple JsonElements if necessary.voidSerializes the given content into a JsonElement usingJsonParserand associates it with the specified postfix.booleansupportsFileExtension(String extension) Determines if the given file extension is supported by this serializer.
-
Field Details
-
FILE_EXTENSION
The file extension supported by this serializer.- See Also:
-
gson
protected final com.google.gson.Gson gsonThe Gson instance used for serialization and deserialization.
-
-
Constructor Details
-
GsonSerializer
public GsonSerializer()Creates a new instance of GsonSerializer with a default Gson configuration.
-
-
Method Details
-
supportsFileExtension
Determines if the given file extension is supported by this serializer.- Specified by:
supportsFileExtensionin classenterprises.iwakura.keqing.Serializer<com.google.gson.JsonElement>
-
serialize
Serializes the given content into a JsonElement usingJsonParserand associates it with the specified postfix.- Specified by:
serializein classenterprises.iwakura.keqing.Serializer<com.google.gson.JsonElement>- Parameters:
postfix- the postfix to associate with the serialized contentcontent- the content to serialize- Throws:
IOException- if an I/O error occurs during serialization
-
readProperty
public <T> Optional<T> readProperty(String postfix, List<String> postfixPriorities, String path, Class<T> clazz) Reads a property from the serialized JsonElement associated with the given postfix and path, merging multiple JsonElements if necessary. This will merge JsonObjects by deep merging and JsonArrays by concatenation.- Overrides:
readPropertyin classenterprises.iwakura.keqing.Serializer<com.google.gson.JsonElement>- Type Parameters:
T- the type of the property value- Parameters:
postfix- the postfix associated with the serialized contentpostfixPriorities- the list of postfix priorities to consider if the specified postfix is not foundpath- the property path to readclazz- the class type to which the property value should be converted- Returns:
- an Optional containing the property value if found and successfully converted, or an empty Optional otherwise
-
mergeObjects
Merges a list of JsonElements into a single JsonObject by deep merging. Higher priority objects in the list will overwrite lower priority ones.- Parameters:
jsonElements- the list of JsonElements to merge- Returns:
- the merged JsonObject
-
mergeArrays
Merges a list of JsonElements into a single JsonArray by concatenation. Higher priority arrays in the list will appear first in the merged array.- Parameters:
jsonElements- the list of JsonElements to merge- Returns:
- the merged JsonArray
-
deepMerge
protected void deepMerge(com.google.gson.JsonObject source, com.google.gson.JsonObject target) Deep merges the source JsonObject into the target JsonObject. If both source and target have a value for the same key: - If both values are JsonObjects, they are merged recursively. - If both values are JsonArrays, they are concatenated. - Otherwise, the source value overwrites the target value.- Parameters:
source- the source JsonObject to merge fromtarget- the target JsonObject to merge into
-
parseElement
Parses a JsonElement into an instance of the specified class type.- Type Parameters:
T- the type of the property value- Parameters:
element- the JsonElement to parseclazz- the class type to which the JsonElement should be converted- Returns:
- an instance of the specified class type, or null if the element is null or cannot be parsed
-
getWrapperClass
Returns the wrapper class for a given primitive class. If the provided class is not a primitive type, it returns the class itself.- Parameters:
clazz- the class to get the wrapper for- Returns:
- the corresponding wrapper class, or the original class if it's not primitive
-
getElementByPath
protected com.google.gson.JsonElement getElementByPath(com.google.gson.JsonElement jsonElement, String path) Retrieves a nested JsonElement from the given JsonElement based on the specified dot-separated path.- Parameters:
jsonElement- the root JsonElement to start frompath- the dot-separated path to the desired property (e.g., "parent.child.property")- Returns:
- the JsonElement at the specified path, or null if any part of the path does not exist
-
readProperty
This method is unsupported in GsonSerializer. UsereadProperty(String, List, String, Class)instead for correct JSON merging behavior.- Specified by:
readPropertyin classenterprises.iwakura.keqing.Serializer<com.google.gson.JsonElement>- Type Parameters:
T- the type of the property value- Parameters:
postfix- the postfix associated with the serialized contentpath- the property path to readclazz- the class type to which the property value should be converted- Returns:
- nothing, as this method always throws an exception
- Throws:
UnsupportedOperationException- always thrown to indicate this method is unsupported
-