Class AbstractSerializer<I>
java.lang.Object
io.fluxzero.sdk.common.serialization.AbstractSerializer<I>
- Type Parameters:
I- the internal intermediate format (e.g., JsonNode) used during downcasting
- All Implemented Interfaces:
ContentFilter, Serializer
- Direct Known Subclasses:
JacksonSerializer
Abstract base implementation of the
Serializer interface.
This class provides common functionality for serialization frameworks including:
- Upcasting and downcasting of serialized data based on revision
- Support for multiple serialization formats
- Lazy deserialization with memoization
- Type upcasting via explicit mappings
Concrete subclasses must implement the core logic for serializing and deserializing objects using a specific format (e.g., JSON or Protobuf). This includes:
doSerialize(Object)doDeserialize(Data, String)doClone(Object)doConvert(Object, Type)asIntermediateValue(Object)for downcasting
Thread-safe and reusable. Used throughout the Fluxzero framework for all (de)serialization needs.
-
Constructor Summary
ConstructorsModifierConstructorDescriptionprotectedAbstractSerializer(Collection<?> casterCandidates, Converter<byte[], I> converter, String format) Constructs a new serializer with the provided caster candidates and converter. -
Method Summary
Modifier and TypeMethodDescriptionprotected abstract IasIntermediateValue(Object input) Converts an object into its intermediate form (e.g., JsonNode) used for revision downcasting.protected StringConverts aTypeto a readable string name.<V> VCreates a deep copy of the object.<V> VConverts the given object to the specified target type.<S extends SerializedObject<byte[]>>
Stream<DeserializingObject<byte[], S>> deserialize(Stream<S> dataStream, UnknownTypeStrategy unknownTypeStrategy) Deserializes a stream ofSerializedObjectvalues into deserialized objects.protected Stream<DeserializingObject<byte[], ?>> deserializeOtherFormat(SerializedObject<byte[]> s) Handles deserialization of objects in formats other than the current default (e.g., fallback string deserialization).protected Stream<DeserializingObject<byte[], ?>> deserializeUnknownType(SerializedObject<?> serializedObject) Hook for handling deserialization of unknown types.protected abstract ObjectHook to clone a deserialized object.protected abstract <V> VConverts a deserialized object to the desired target type.protected abstract ObjectdoDeserialize(Data<?> data, String type) Core method to deserialize the givenDatawith an explicit type hint.protected abstract byte[]doSerialize(Object object) Hook for serializing the object to bytes using the primary format.Downcasts an object already inDataform.Downcasts a deserialized object to the desired revision number.protected TypeDetermines the full type signature of the given object.protected StringgetTypeString(Object object) Resolves the effective string representation of an object's type, including generic parameters if applicable.protected booleanisKnownType(String type) Checks whether a given serialized type is recognized on the classpath.registerDowncasters(Object... casterCandidates) Registers custom downcasters for transforming newer object states to older revisions.registerTypeCaster(String oldType, String newType) Registers a type mapping for upcasting old type names to new ones.registerUpcasters(Object... casterCandidates) Registers custom upcasters for handling older serialized formats.Data<byte[]> Serializes the given object into a byte-basedDataobject, using the specified format.protected Data<byte[]> serializeToOtherFormat(Object object, String format) Converts common simple object types (e.g., String, byte[]) to the requested alternative format.upcastType(String type) Resolves the current type from a potentially chained upcast mapping.Methods inherited from class Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, waitMethods inherited from interface ContentFilter
filterContentMethods inherited from interface Serializer
deserialize, deserialize, deserializeMessage, deserializeMessages, deserializeMessages, deserializeMessages, deserializeMessages, registerCasters, serialize
-
Constructor Details
-
AbstractSerializer
protected AbstractSerializer(Collection<?> casterCandidates, Converter<byte[], I> converter, String format) Constructs a new serializer with the provided caster candidates and converter.- Parameters:
casterCandidates- a collection of objects providing up/down-casting functionalityconverter- a converter used to assist in interpreting serialized dataformat- the default serialization format name (e.g., "json")
-
-
Method Details
-
serialize
Serializes the given object into a byte-basedDataobject, using the specified format.- Specified by:
serializein interfaceSerializer- Parameters:
object- the object to serializeformat- the desired serialization format- Returns:
- a
Data<byte[]>object containing the serialized bytes
-
serializeToOtherFormat
-
getType
-
asString
-
getTypeString
-
doSerialize
-
deserialize
public <S extends SerializedObject<byte[]>> Stream<DeserializingObject<byte[],S>> deserialize(Stream<S> dataStream, UnknownTypeStrategy unknownTypeStrategy) Deserializes a stream ofSerializedObjectvalues into deserialized objects. Applies upcasters, format detection, and lazy deserialization as needed.- Specified by:
deserializein interfaceSerializer- Type Parameters:
S- the type of the serialized object- Parameters:
dataStream- data input stream to deserializeunknownTypeStrategy- value that determines what to do when encountering unknown types- Returns:
- a stream containing deserialization results
-
convert
Converts the given object to the specified target type.- Specified by:
convertin interfaceSerializer- Type Parameters:
V- the result type- Parameters:
value- the input valuetype- the target type- Returns:
- the converted value
-
clone
Creates a deep copy of the object. For known types (collections, maps, etc.) a shallow copy is made; otherwise, usesdoClone(Object).- Specified by:
clonein interfaceSerializer- Type Parameters:
V- the type of the value- Parameters:
value- the object to clone- Returns:
- a deep copy
-
registerUpcasters
Registers custom upcasters for handling older serialized formats.- Specified by:
registerUpcastersin interfaceSerializer- Parameters:
casterCandidates- beans with upcasting logic- Returns:
- a registration handle
-
registerDowncasters
Registers custom downcasters for transforming newer object states to older revisions.- Specified by:
registerDowncastersin interfaceSerializer- Parameters:
casterCandidates- beans with downcasting logic- Returns:
- a registration handle
-
registerTypeCaster
Registers a type mapping for upcasting old type names to new ones.- Specified by:
registerTypeCasterin interfaceSerializer- Parameters:
oldType- the legacy type namenewType- the canonical or updated type name- Returns:
- a registration handle
-
upcastType
Resolves the current type from a potentially chained upcast mapping.- Specified by:
upcastTypein interfaceSerializer- Parameters:
type- the original type- Returns:
- the remapped (or unchanged) type name
-
downcast
Downcasts a deserialized object to the desired revision number.- Specified by:
downcastin interfaceSerializer- Parameters:
object- the object to downcastdesiredRevision- the target revision- Returns:
- a revisioned form of the object
-
downcast
Downcasts an object already inDataform.- Specified by:
downcastin interfaceSerializer- Parameters:
object- the serialized datadesiredRevision- the target revision number- Returns:
- a transformed object matching the older revision
-
doClone
-
doConvert
-
isKnownType
Checks whether a given serialized type is recognized on the classpath. -
deserializeOtherFormat
Handles deserialization of objects in formats other than the current default (e.g., fallback string deserialization). -
deserializeUnknownType
protected Stream<DeserializingObject<byte[],?>> deserializeUnknownType(SerializedObject<?> serializedObject) Hook for handling deserialization of unknown types. Subclasses can override to log or recover gracefully. -
doDeserialize
-
asIntermediateValue
-