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

    Fields
    Modifier and Type
    Field
    Description
    static final String
    The file extension supported by this serializer.
    protected final com.google.gson.Gson
    The Gson instance used for serialization and deserialization.

    Fields inherited from class enterprises.iwakura.keqing.Serializer

    serializedFileContents
  • Constructor Summary

    Constructors
    Constructor
    Description
    Creates a new instance of GsonSerializer with a default Gson configuration.
  • Method Summary

    Modifier and Type
    Method
    Description
    protected void
    deepMerge(com.google.gson.JsonObject source, com.google.gson.JsonObject target)
    Deep merges the source JsonObject into the target JsonObject.
    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.
    protected Class<?>
    Returns the wrapper class for a given primitive class.
    protected com.google.gson.JsonArray
    mergeArrays(List<com.google.gson.JsonElement> jsonElements)
    Merges a list of JsonElements into a single JsonArray by concatenation.
    protected com.google.gson.JsonObject
    mergeObjects(List<com.google.gson.JsonElement> jsonElements)
    Merges a list of JsonElements into a single JsonObject by deep merging.
    protected <T> T
    parseElement(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>
    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.
    void
    serialize(String postfix, String content)
    Serializes the given content into a JsonElement using JsonParser and associates it with the specified postfix.
    boolean
    Determines if the given file extension is supported by this serializer.

    Methods inherited from class java.lang.Object

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

    • FILE_EXTENSION

      public static final String FILE_EXTENSION
      The file extension supported by this serializer.
      See Also:
    • gson

      protected final com.google.gson.Gson gson
      The 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

      public boolean supportsFileExtension(String extension)
      Determines if the given file extension is supported by this serializer.
      Specified by:
      supportsFileExtension in class enterprises.iwakura.keqing.Serializer<com.google.gson.JsonElement>
    • serialize

      public void serialize(String postfix, String content) throws IOException
      Serializes the given content into a JsonElement using JsonParser and associates it with the specified postfix.
      Specified by:
      serialize in class enterprises.iwakura.keqing.Serializer<com.google.gson.JsonElement>
      Parameters:
      postfix - the postfix to associate with the serialized content
      content - 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:
      readProperty in class enterprises.iwakura.keqing.Serializer<com.google.gson.JsonElement>
      Type Parameters:
      T - the type of the property value
      Parameters:
      postfix - the postfix associated with the serialized content
      postfixPriorities - the list of postfix priorities to consider if the specified postfix is not found
      path - the property path to read
      clazz - 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

      protected com.google.gson.JsonObject mergeObjects(List<com.google.gson.JsonElement> jsonElements)
      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

      protected com.google.gson.JsonArray mergeArrays(List<com.google.gson.JsonElement> jsonElements)
      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 from
      target - the target JsonObject to merge into
    • parseElement

      protected <T> T parseElement(com.google.gson.JsonElement element, Class<T> clazz)
      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 parse
      clazz - 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

      protected Class<?> getWrapperClass(Class<?> clazz)
      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 from
      path - 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

      public <T> Optional<T> readProperty(String postfix, String path, Class<T> clazz)
      This method is unsupported in GsonSerializer. Use readProperty(String, List, String, Class) instead for correct JSON merging behavior.
      Specified by:
      readProperty in class enterprises.iwakura.keqing.Serializer<com.google.gson.JsonElement>
      Type Parameters:
      T - the type of the property value
      Parameters:
      postfix - the postfix associated with the serialized content
      path - the property path to read
      clazz - 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