Class Yggdrasil
Yggdrasil uses String IDs to identify classes, thus all classes to be (de)serialised have to be registered to Yggdrasil before doing anything (they can also be registered while
Yggdrasil is working, but you must make sure that all classes are registered in time when deserialising). A ClassResolver
or YggdrasilSerializer
can also be used
to find classes and IDs dynamically.
Default behaviour
A Java object can be serialised and deserialised if it is a primitive, a primitive wrapper, a String, an enum or PseudoEnum
(both require an ID), or its class meets all
of the following requirements:
- It implements
YggdrasilSerializable
- It has an ID assigned to it (using the methods described above)
- It provides a nullary constructor (any access modifier) (in particular anonymous and non-static inner classes can't be serialised)
- All its non-transient and non-static fields are serialisable according to these requirements
Yggdrasil will generate errors if an object loaded either has too many fields and/or is missing some in the stream.
Customisation
Any object that does not meet the above requirements for serialisation can still be (de)serialised using an YggdrasilSerializer
(useful for objects of an external API),
or by implementing YggdrasilSerializable.YggdrasilExtendedSerializable
.
The behaviour in case of an invalid or outdated stream can be defined likewise, or one can implement YggdrasilSerializable.YggdrasilRobustSerializable
or YggdrasilSerializable.YggdrasilRobustEnum
respectively.
-
Field Summary
Modifier and TypeFieldDescriptionstatic short
latest protocol versionstatic int
Magic Number: "Ygg\0"short
-
Constructor Summary
-
Method Summary
Modifier and TypeMethodDescriptionvoid
excessiveField(Object o, Fields.FieldContext field)
Class<?>
getEnumConstant(Class<T> c, String id)
static String
static String
Gets the ID of a field.void
incompatibleField(Object o, Field f, Fields.FieldContext field)
boolean
isSerializable(Class<?> c)
<T> @Nullable T
loadFromFile(File f, Class<T> expectedType)
static void
void
missingField(Object o, Field f)
newOutputStream(OutputStream out)
Deprecated.Deprecated.void
void
void
registerSingleClass(Class<?> c)
Registers a class and uses itsYggdrasilID
as id.void
registerSingleClass(Class<?> c, String id)
void
saveToFile(Object o, File f)
-
Field Details
-
MAGIC_NUMBER
public static final int MAGIC_NUMBERMagic Number: "Ygg\0"hex: 0x59676700
- See Also:
- Constant Field Values
-
LATEST_VERSION
public static final short LATEST_VERSIONlatest protocol version- See Also:
- Constant Field Values
-
version
public final short version
-
-
Constructor Details
-
Yggdrasil
public Yggdrasil() -
Yggdrasil
public Yggdrasil(short version)
-
-
Method Details
-
newOutputStream
- Throws:
IOException
-
newInputStream
- Throws:
IOException
-
newXMLOutputStream
Deprecated.- Throws:
IOException
-
newXMLInputStream
Deprecated.- Throws:
IOException
-
registerClassResolver
-
registerSingleClass
-
registerSingleClass
Registers a class and uses itsYggdrasilID
as id. -
registerFieldHandler
-
isSerializable
-
getClass
- Throws:
StreamCorruptedException
-
getID
- Throws:
NotSerializableException
-
getID
Gets the ID of a field.This method performs no checks on the given field.
- Parameters:
f
-- Returns:
- The field's id as given by its
YggdrasilID
annotation, or its name if it's not annotated.
-
getID
-
getEnumConstant
public static <T extends Enum<T>> Enum<T> getEnumConstant(Class<T> c, String id) throws StreamCorruptedException- Throws:
StreamCorruptedException
-
excessiveField
- Throws:
StreamCorruptedException
-
missingField
- Throws:
StreamCorruptedException
-
incompatibleField
public void incompatibleField(Object o, Field f, Fields.FieldContext field) throws StreamCorruptedException- Throws:
StreamCorruptedException
-
saveToFile
- Throws:
IOException
-
loadFromFile
- Throws:
IOException
-
main
-