Class YggdrasilSerializer<T>

java.lang.Object
ch.njol.yggdrasil.YggdrasilSerializer<T>
All Implemented Interfaces:
ClassResolver
Direct Known Subclasses:
JRESerializer, Serializer

public abstract class YggdrasilSerializer<T> extends Object implements ClassResolver
Utility to be able to save and load classes with Yggdrasil that the user has no control of, e.g. classes of an external API.
  • Constructor Details

    • YggdrasilSerializer

      public YggdrasilSerializer()
  • Method Details

    • getClass

      @Nullable public abstract @Nullable Class<? extends T> getClass(String id)
      Description copied from interface: ClassResolver
      Resolves a class by its ID.
      Specified by:
      getClass in interface ClassResolver
      Parameters:
      id - The ID used when storing objects
      Returns:
      The Class object that represents data with the given ID, or null if the ID does not belong to the implementor
    • serialize

      public abstract Fields serialize(T object) throws NotSerializableException
      Serialises the given object.

      Use return new Fields(this); to emulate the default behaviour.

      Parameters:
      object - The object to serialise
      Returns:
      A Fields object representing the object's fields to serialise. Must not be null.
      Throws:
      NotSerializableException - If this object could not be serialized
    • canBeInstantiated

      public boolean canBeInstantiated(Class<? extends T> type)
      Whether an instance of the given class can be dynamically created. If this method returns false, newInstance(Class) and deserialize(Object, Fields) will not be called for the given class, but deserialize(Class, Fields) will be used instead, and having any reference to an object of the given class in its own fields' graph will cause Yggdrasil to throw an exception upon serialisation as no reference to the object will be available when deserializing the object. // TODO allow this

      Please note that you must not change the return value of this function ever - it is not saved in the stream.

      Parameters:
      type - The class to check
      Returns:
      true by default
    • newInstance

      @Nullable public abstract <E extends T> E newInstance(Class<E> c)
      Creates a new instance of the given class.
      Parameters:
      c - The class as read from stream
      Returns:
      A new instance of the given class. Must not be null if canBeInstantiated(Class) returned true.
    • deserialize

      public abstract void deserialize(T object, Fields fields) throws StreamCorruptedException, NotSerializableException
      Deserializes an object.

      Use fields.setFields(o); to emulate the default behaviour.

      Parameters:
      object - The object to deserialize as returned by newInstance(Class).
      fields - The fields read from stream
      Throws:
      StreamCorruptedException - If deserialization failed because the data read from stream is incomplete or invalid.
      NotSerializableException
    • deserialize

      public <E extends T> E deserialize(Class<E> type, Fields fields) throws StreamCorruptedException, NotSerializableException
      Deserializes an object.
      Parameters:
      type - The class to get an instance of
      fields - The fields read from stream
      Returns:
      An object representing the read fields. Must not be null (throw an exception instead).
      Throws:
      StreamCorruptedException - If deserialization failed because the data read from stream is incomplete or invalid.
      NotSerializableException - If the class is not serializable