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

      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 o) throws NotSerializableException
      Serialises the given object.

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

      Parameters:
      o - 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 serialised
    • canBeInstantiated

      public boolean canBeInstantiated(Class<? extends T> c)
      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 deserialising 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. // TODO clarify

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

      public abstract <E extends T> @Nullable 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 o, Fields fields) throws StreamCorruptedException, NotSerializableException
      Deserialises an object.

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

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

      public <E extends T> E deserialize(Class<E> c, Fields fields) throws StreamCorruptedException, NotSerializableException
      Deserialises an object.
      Parameters:
      c - 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 deserialisation failed because the data read from stream is incomplete or invalid.
      NotSerializableException - If the class is not serialisable