f3f9d9280d
Co-authored-by: Tobias Nett <skaldarnar@googlemail.com> |
||
---|---|---|
.. | ||
src | ||
README.MD | ||
build.gradle.kts |
README.MD
TypeHandlerLibrary
It is really not subsystem. but used by them
A library of type handlers. This is used for the construction of class metadata. This library should be initialised by adding a number of base type handlers, describing how to serialize each supported type. It will then produce serializers for classes (through their ClassMetadata) on request.
Type handlers provide the algorithms for serializing and deserializing types - this uses an implementation agnostic set of interfaces so TypeHandlers can be used for different serialization techniques (Json, Protobuf, etc).
Structure
Serializer
- central class for (de)serialization.TypeHandlerLibrary
- core class for registeringTypeHandlerFactory
sTypeHandlerFactory
- factory forTypeHandler
sTypeHandler
- interface for handling convertiong from<your class>
to PersistentData and otherwise.PersistedData
- interface for target format/protocol(e.g. gson/protobuf)PersistedDataSerializer
- interface for creating/fillingPersistedData
PersistedDataWriter
- interface for writingPersistedData
to bytes or outputStreamPersistedDataReader
- interface for readingPersistedData
from bytes or inputStream
Usage
- Create
Serializer
:Serializer serializer = new Serializer( typeHandlerLibrary, // Previosly filled with your TypeHandlers persistedDataSerializer, // format implementation persistedDataWriter, // format implementation persistedDataReader, // format implementation )
- Serialize:
orserializer.serialize( obj, // Your object to serialize TypeInfo.of(YourClass.class), // Type of obj (also can handle generic types) outputStream); // Output stream for writing.
byte[] bytes = serializer.serialize( obj, // Your object to serialize TypeInfo.of(YourClass.class)); // Type of obj (also can handle generic types)
- Deserialize:
orOptional<YourClass> obj = serializer.deserialize( TypeInfo.of(YourClass.class), // Type of obj (also can handle generic types) inputStream); // Input Stream for reading.
byte[] bytes; // your format data in bytes; Optional<YourClass> obj = serializer.deserialize( TypeInfo.of(YourClass.class), // Type of obj (also can handle generic types) bytes);
(De)Serialization Process
Serialization: Deserialization:
Implement your own format
You should implements:
PersistedData
- for storing your format data.PersistedDataSerializer
- for creating yourPersistedData
fromTypeHandler
PersistedDataWriter
- for writing yourPersistedData
to outputStream or bytesPersistedDataReader
- for reading yourPersistedData
from inputStream or bytes
Implement your own handler for java type
Just implement TypeHandler
and register it in TypeHandlerLibrary
Example( BooleanTypeHandler
from coreTypes ):
public class BooleanTypeHandler extends TypeHandler<Boolean> {
@Override
public PersistedData serializeNonNull(Boolean value, PersistedDataSerializer serializer) {
return serializer.serialize(value);
}
@Override
public Optional<Boolean> deserialize(PersistedData data) {
if (data.isBoolean()) {
return Optional.of(data.getAsBoolean());
}
return Optional.empty();
}
}
Or implement TypeHandlerFactory
and register it in TypeHandlerLibrary