Package ch.njol.yggdrasil
Class JRESerializer
- All Implemented Interfaces:
ClassResolver
-
Constructor Summary
-
Method Summary
Modifier and TypeMethodDescriptionboolean
canBeInstantiated
(Class<?> type) Whether an instance of the given class can be dynamically created.<E> E
deserialize
(Class<E> type, Fields fields) Deserializes an object.void
deserialize
(Object object, Fields fields) Deserializes an object.@Nullable Class
<?> Resolves a class by its ID.@Nullable String
Gets an ID for a Class.<T> T
newInstance
(Class<T> type) 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 saved object.- Parameters:
type
- 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:
object
- 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 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.
- Overrides:
canBeInstantiated
in classYggdrasilSerializer<Object>
- Parameters:
type
- 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:
type
- 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
Deserializes an object.Use fields.
setFields
(o); to emulate the default behaviour.- Specified by:
deserialize
in classYggdrasilSerializer<Object>
- Parameters:
object
- The object to deserialize as returned byYggdrasilSerializer.newInstance(Class)
.fields
- The fields read from stream- Throws:
StreamCorruptedException
- If deserialization failed because the data read from stream is incomplete or invalid.
-
deserialize
public <E> E deserialize(Class<E> type, Fields fields) throws StreamCorruptedException, NotSerializableException Description copied from class:YggdrasilSerializer
Deserializes an object.- Overrides:
deserialize
in classYggdrasilSerializer<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
-