Package ch.njol.yggdrasil
Class YggdrasilSerializer<T>
java.lang.Object
ch.njol.yggdrasil.YggdrasilSerializer<T>
- All Implemented Interfaces:
ClassResolver
- Direct Known Subclasses:
JRESerializer
,Serializer
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 Summary
-
Method Summary
Modifier and TypeMethodDescriptionboolean
canBeInstantiated(Class<? extends T> c)
Whether an instance of the given class can be dynamically created.<E extends T>
Edeserialize(Class<E> c, Fields fields)
Deserialises an object.abstract void
deserialize(T o, Fields fields)
Deserialises an object.Resolves a class by its ID.abstract <E extends T>
@Nullable EnewInstance(Class<E> c)
Creates a new instance of the given class.abstract Fields
Serialises the given object.Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
Methods inherited from interface ch.njol.yggdrasil.ClassResolver
getID
-
Constructor Details
-
YggdrasilSerializer
public YggdrasilSerializer()
-
-
Method Details
-
getClass
Description copied from interface:ClassResolver
Resolves a class by its ID.- Specified by:
getClass
in interfaceClassResolver
- 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
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
Whether an instance of the given class can be dynamically created. If this method returns false,newInstance(Class)
anddeserialize(Object, Fields)
will not be called for the given class, butdeserialize(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 thisPlease 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
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, NotSerializableExceptionDeserialises an object.Use fields.
setFields
(o); to emulate the default behaviour.- Parameters:
o
- The object to deserialise as returned bynewInstance(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, NotSerializableExceptionDeserialises an object.- Parameters:
c
- The class to get an instance offields
- 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
-