Class ScalarSerializer<T>
- java.lang.Object
-
- ninja.leaping.configurate.objectmapping.serialize.ScalarSerializer<T>
-
- Type Parameters:
T
- The object type to serialize
- All Implemented Interfaces:
TypeSerializer<T>
public abstract class ScalarSerializer<T> extends Object implements TypeSerializer<T>
Serialize a value that can be represented as a scalar value within a node. Implementations must be able to serialize when one of the accepted types is aString
, and may support any other types as desired.When serializing to a node, null values will be passed through directly. If the type serialized by this serializer is one of the native types of the backing node, it will be written directly to the node without any transformation.
Any serialized value must be deserializable by the same serializer..
-
-
Constructor Summary
Constructors Modifier Constructor Description protected
ScalarSerializer(TypeToken<T> type)
protected
ScalarSerializer(Class<T> type)
-
Method Summary
All Methods Instance Methods Abstract Methods Concrete Methods Modifier and Type Method Description abstract T
deserialize(TypeToken<?> type, Object obj)
Given an object of unknown type, attempt to convert it into the given type.@Nullable T
deserialize(TypeToken<?> type, ConfigurationNode node)
Deserialize an object (of the correct type) from the given configuration node.T
deserialize(Object value)
Attempt to deserialize the provided object using an unspecialized type.void
serialize(TypeToken<?> type, @Nullable T obj, ConfigurationNode node)
Serialize an object to the given configuration node.abstract Object
serialize(T item, Predicate<Class<?>> typeSupported)
String
serializeToString(T item)
Serialize the item to aString
, in a representation that can be interpreted by this serializer again@Nullable T
tryDeserialize(@Nullable Object obj)
Attempt to deserialize the provided object, but rather than throwing an exception when a parse error occurs, return null instead.TypeToken<T>
type()
Get the general type token applicable for this serializer.
-
-
-
Constructor Detail
-
ScalarSerializer
protected ScalarSerializer(TypeToken<T> type)
-
ScalarSerializer
protected ScalarSerializer(Class<T> type)
-
-
Method Detail
-
type
public final TypeToken<T> type()
Get the general type token applicable for this serializer. This token may be parameterized.- Returns:
- The type token for this serializer
-
deserialize
public final @Nullable T deserialize(TypeToken<?> type, ConfigurationNode node) throws ObjectMappingException
Description copied from interface:TypeSerializer
Deserialize an object (of the correct type) from the given configuration node.- Specified by:
deserialize
in interfaceTypeSerializer<T>
- Parameters:
type
- The type of return value requirednode
- The node containing serialized data- Returns:
- An object
- Throws:
ObjectMappingException
- If the presented data is invalid
-
serialize
public final void serialize(TypeToken<?> type, @Nullable T obj, ConfigurationNode node)
Description copied from interface:TypeSerializer
Serialize an object to the given configuration node.- Specified by:
serialize
in interfaceTypeSerializer<T>
- Parameters:
type
- The type of the input objectobj
- The object to be serializednode
- The node to write to
-
deserialize
public abstract T deserialize(TypeToken<?> type, Object obj) throws ObjectMappingException
Given an object of unknown type, attempt to convert it into the given type.- Parameters:
type
- The specific type of the type's usageobj
- The object to convert- Returns:
- A converted object
- Throws:
ObjectMappingException
- If the object could not be converted for any reason
-
serialize
public abstract Object serialize(T item, Predicate<Class<?>> typeSupported)
- Parameters:
item
- The value to serializetypeSupported
- A predicate to allow choosing which types are supported- Returns:
- A serialized form of this object
-
deserialize
public final T deserialize(Object value) throws ObjectMappingException
Attempt to deserialize the provided object using an unspecialized type. This may fail on more complicated deserialization processes- Parameters:
value
- The object to deserialize.- Returns:
- The deserialized object, if possible
- Throws:
ObjectMappingException
- If unable to coerce the value to the requested type
-
tryDeserialize
public final @Nullable T tryDeserialize(@Nullable Object obj)
Attempt to deserialize the provided object, but rather than throwing an exception when a parse error occurs, return null instead.- Parameters:
obj
- The object to try to deserialize- Returns:
- An instance of the appropriate type, or null
- See Also:
deserialize(Object)
-
serializeToString
public final String serializeToString(T item)
Serialize the item to aString
, in a representation that can be interpreted by this serializer again- Parameters:
item
- The item to serialize- Returns:
- The serialized form of the item
-
-