Package ch.njol.yggdrasil
Class JRESerializer
- All Implemented Interfaces:
ClassResolver
-
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionbooleancanBeInstantiated(Class<? extends Object> c)Whether an instance of the given class can be dynamically created.<E> Edeserialize(Class<E> c, Fields fields)Deserialises an object.voiddeserialize(Object o, Fields fields)Deserialises an object.@Nullable Class<?>Resolves a class by its ID.@Nullable StringGets an ID for a Class.<T> @Nullable TnewInstance(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:ClassResolverResolves a class by its ID.- Specified by:
getClassin interfaceClassResolver- Specified by:
getClassin 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:ClassResolverGets 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:YggdrasilSerializerSerialises the given object.Use return new
Fields(this); to emulate the default behaviour.- Specified by:
serializein 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:YggdrasilSerializerWhether 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:
canBeInstantiatedin classYggdrasilSerializer<Object>- Parameters:
c- The class to check- Returns:
- true by default
-
newInstance
Description copied from class:YggdrasilSerializerCreates a new instance of the given class.- Specified by:
newInstancein 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:YggdrasilSerializerDeserialises an object.Use fields.
setFields(o); to emulate the default behaviour.- Specified by:
deserializein 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:YggdrasilSerializerDeserialises an object.- Overrides:
deserializein 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
-