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> type) Whether an instance of the given class can be dynamically created.<E extends T>
Edeserialize
(Class<E> type, Fields fields) Deserializes an object.abstract void
deserialize
(T object, Fields fields) Deserializes an object.Resolves a class by its ID.abstract <E extends T>
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:
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
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 deserializing 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.
- Parameters:
type
- 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 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 bynewInstance(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 offields
- 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
-