Class 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 a String, 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..

    • Method Detail

      • type

        public final TypeToken<Ttype()
        Get the general type token applicable for this serializer. This token may be parameterized.
        Returns:
        The type token for this serializer
      • 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 usage
        obj - 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 serialize
        typeSupported - 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 a String, in a representation that can be interpreted by this serializer again
        Parameters:
        item - The item to serialize
        Returns:
        The serialized form of the item