Class StorageSerializer

java.lang.Object
dev.crafty.core.storage.serialization.StorageSerializer

public class StorageSerializer extends Object
Utility class for serializing and deserializing objects using Jackson.
Since:
1.0.0
  • Constructor Details

    • StorageSerializer

      public StorageSerializer()
  • Method Details

    • toJson

      public static String toJson(Object object) throws com.fasterxml.jackson.core.JsonProcessingException
      Converts an object to a JSON string.
      Parameters:
      object - The object to serialize
      Returns:
      The JSON string representation of the object
      Throws:
      com.fasterxml.jackson.core.JsonProcessingException - If serialization fails
    • fromJson

      public static <T> T fromJson(String json, Class<T> valueType) throws IOException
      Converts a JSON string to an object.
      Type Parameters:
      T - The type of the object
      Parameters:
      json - The JSON string to deserialize
      valueType - The class of the object to create
      Returns:
      The deserialized object
      Throws:
      IOException - If deserialization fails
    • fromJson

      public static <T> T fromJson(String json, Class<T> valueType, Class<?>... parameterClasses) throws IOException
      Converts a JSON string to an object with generic type parameters.
      Type Parameters:
      T - The type of the object
      Parameters:
      json - The JSON string to deserialize
      valueType - The class of the object to create
      parameterClasses - The classes of the generic type parameters
      Returns:
      The deserialized object
      Throws:
      IOException - If deserialization fails
    • toYaml

      public static String toYaml(Object object) throws com.fasterxml.jackson.core.JsonProcessingException
      Converts an object to a YAML string.
      Parameters:
      object - The object to serialize
      Returns:
      The YAML string representation of the object
      Throws:
      com.fasterxml.jackson.core.JsonProcessingException - If serialization fails
    • fromYaml

      public static <T> T fromYaml(String yaml, Class<T> valueType) throws IOException
      Converts a YAML string to an object.
      Type Parameters:
      T - The type of the object
      Parameters:
      yaml - The YAML string to deserialize
      valueType - The class of the object to create
      Returns:
      The deserialized object
      Throws:
      IOException - If deserialization fails
    • toMap

      public static Map<String,Object> toMap(Object object)
      Converts an object to a Map.
      Parameters:
      object - The object to convert
      Returns:
      A Map representation of the object
    • fromMap

      public static <T> T fromMap(Map<String,Object> map, Class<T> valueType)
      Converts a Map to an object.
      Type Parameters:
      T - The type of the object
      Parameters:
      map - The map to convert
      valueType - The class of the object to create
      Returns:
      The converted object
    • getJsonMapper

      public static com.fasterxml.jackson.databind.ObjectMapper getJsonMapper()
      Gets the JSON ObjectMapper instance.
      Returns:
      The JSON ObjectMapper
    • getYamlMapper

      public static com.fasterxml.jackson.databind.ObjectMapper getYamlMapper()
      Gets the YAML ObjectMapper instance.
      Returns:
      The YAML ObjectMapper