Class StorageSerializer
java.lang.Object
dev.crafty.core.storage.serialization.StorageSerializer
Utility class for serializing and deserializing objects using Jackson.
- Since:
- 1.0.0
-
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionstatic <T> T
Converts a JSON string to an object.static <T> T
Converts a JSON string to an object with generic type parameters.static <T> T
Converts a Map to an object.static <T> T
Converts a YAML string to an object.static com.fasterxml.jackson.databind.ObjectMapper
Gets the JSON ObjectMapper instance.static com.fasterxml.jackson.databind.ObjectMapper
Gets the YAML ObjectMapper instance.static String
Converts an object to a JSON string.Converts an object to a Map.static String
Converts an object to a YAML string.
-
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
Converts a JSON string to an object.- Type Parameters:
T
- The type of the object- Parameters:
json
- The JSON string to deserializevalueType
- 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 deserializevalueType
- The class of the object to createparameterClasses
- 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
Converts a YAML string to an object.- Type Parameters:
T
- The type of the object- Parameters:
yaml
- The YAML string to deserializevalueType
- The class of the object to create- Returns:
- The deserialized object
- Throws:
IOException
- If deserialization fails
-
toMap
Converts an object to a Map.- Parameters:
object
- The object to convert- Returns:
- A Map representation of the object
-
fromMap
Converts a Map to an object.- Type Parameters:
T
- The type of the object- Parameters:
map
- The map to convertvalueType
- 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
-