Package ch.njol.yggdrasil
Class JRESerializer
- All Implemented Interfaces:
ClassResolver
-
Constructor Summary
-
Method Summary
Modifier and TypeMethodDescriptionboolean
canBeInstantiated(Class<? extends Object> c)
Whether an instance of the given class can be dynamically created.<E> E
deserialize(Class<E> c, Fields fields)
Deserialises an object.void
deserialize(Object o, Fields fields)
Deserialises an object.@Nullable Class<?>
Resolves a class by its ID.@Nullable String
Gets an ID for a Class.<T> @Nullable T
newInstance(Class<T> c)
Creates a new instance of the given class.Serialises the given object.
-
Constructor Details
-
JRESerializer
public JRESerializer()
-
-
Method Details
-
getClass
Description copied from interface:ClassResolver
Resolves a class by its ID.- Specified by:
getClass
in interfaceClassResolver
- Specified by:
getClass
in classYggdrasilSerializer<Object>
- 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
-
getID
Description copied from interface:ClassResolver
Gets an ID for a Class. The ID is used to identify the type of a saved object.// TODO make sure that it's unique
- Parameters:
c
- The class to get the ID of- Returns:
- The ID of the given class, or null if this is not a class of the implementor
-
serialize
Description copied from class:YggdrasilSerializer
Serialises the given object.Use return new
Fields
(this); to emulate the default behaviour.- Specified by:
serialize
in classYggdrasilSerializer<Object>
- Parameters:
o
- The object to serialise- Returns:
- A Fields object representing the object's fields to serialise. Must not be null.
-
canBeInstantiated
Description copied from class:YggdrasilSerializer
Whether an instance of the given class can be dynamically created. If this method returns false,YggdrasilSerializer.newInstance(Class)
andYggdrasilSerializer.deserialize(Object, Fields)
will not be called for the given class, butYggdrasilSerializer.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 thisPlease note that you must not change the return value of this function ever - it is not saved in the stream. // TODO clarify
- Overrides:
canBeInstantiated
in classYggdrasilSerializer<Object>
- Parameters:
c
- The class to check- Returns:
- true by default
-
newInstance
Description copied from class:YggdrasilSerializer
Creates a new instance of the given class.- Specified by:
newInstance
in classYggdrasilSerializer<Object>
- Parameters:
c
- The class as read from stream- Returns:
- A new instance of the given class. Must not be null if
YggdrasilSerializer.canBeInstantiated(Class)
returned true.
-
deserialize
Description copied from class:YggdrasilSerializer
Deserialises an object.Use fields.
setFields
(o); to emulate the default behaviour.- Specified by:
deserialize
in classYggdrasilSerializer<Object>
- Parameters:
o
- The object to deserialise as returned byYggdrasilSerializer.newInstance(Class)
.fields
- The fields read from stream- Throws:
StreamCorruptedException
- If deserialisation failed because the data read from stream is incomplete or invalid.
-
deserialize
public <E> E deserialize(Class<E> c, Fields fields) throws StreamCorruptedException, NotSerializableExceptionDescription copied from class:YggdrasilSerializer
Deserialises an object.- Overrides:
deserialize
in classYggdrasilSerializer<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
-