Class AbstractSerializer<I>
- Type Parameters:
I- the internal intermediate format (e.g., JsonNode) used during downcasting
- All Implemented Interfaces:
ContentFilter, Serializer
- Direct Known Subclasses:
JacksonSerializer
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, io.fluxzero.common.serialization.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 io.fluxzero.common.api.SerializedObject<byte[]>>
Stream<DeserializingObject<byte[], S>> deserialize(Stream<S> dataStream, UnknownTypeStrategy unknownTypeStrategy) Deserializes a stream ofSerializedObjectvalues into deserialized objects.deserializeFirstMessage(io.fluxzero.common.api.SerializedMessage message, io.fluxzero.common.MessageType messageType, String topic) Deserializes oneSerializedMessageinto the first resultingDeserializingMessage.deserializeFirstMessageOrNull(io.fluxzero.common.api.SerializedMessage message, io.fluxzero.common.MessageType messageType, String topic) Deserializes oneSerializedMessageinto the first resultingDeserializingMessage, ornullif no result remains.protected Stream<DeserializingObject<byte[], ?>> deserializeOtherFormat(io.fluxzero.common.api.SerializedObject<byte[]> s) Handles deserialization of objects in formats other than the current default (e.g., fallback string deserialization).protected Stream<DeserializingObject<byte[], ?>> deserializeUnknownType(io.fluxzero.common.api.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(io.fluxzero.common.api.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.downcast(io.fluxzero.common.api.Data<?> object, int desiredRevision) 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 booleanIndicates whether exact or package aliases are currently registered.protected booleanisKnownType(String type) Checks whether a given serialized type is recognized on the classpath.io.fluxzero.common.api.SerializedObject<byte[]> normalize(io.fluxzero.common.api.SerializedObject<?> serializedObject) Normalizes an already-serialized object to the target byte-based representation of this serializer.io.fluxzero.common.RegistrationregisterDowncasters(Object... casterCandidates) Registers custom downcasters for transforming newer object states to older revisions.io.fluxzero.common.RegistrationregisterPackageAlias(String oldPackage, String newPackage) Registers an alias from a legacy package and all its subpackages to a current package.io.fluxzero.common.RegistrationregisterTypeAlias(String oldType, String newType) Registers an exact alias from a serialized type name to its current type name.io.fluxzero.common.RegistrationregisterTypeCaster(String oldType, String newType) Registers a type mapping for upcasting old type names to new ones.io.fluxzero.common.RegistrationregisterUpcasters(Object... casterCandidates) Registers custom upcasters for handling older serialized formats.io.fluxzero.common.api.Data<byte[]> Serializes the given object into a byte-basedDataobject, using the specified format.protected io.fluxzero.common.api.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 potentially chained exact and package aliases.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, io.fluxzero.common.serialization.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
-
normalize
public io.fluxzero.common.api.SerializedObject<byte[]> normalize(io.fluxzero.common.api.SerializedObject<?> serializedObject) Description copied from interface:SerializerNormalizes an already-serialized object to the target byte-based representation of this serializer.- Specified by:
normalizein interfaceSerializer- Parameters:
serializedObject- the serialized object to normalize- Returns:
- the normalized byte-based serialized object
-
serializeToOtherFormat
-
getType
-
asString
-
getTypeString
-
doSerialize
-
deserialize
public <S extends io.fluxzero.common.api.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
-
deserializeFirstMessage
public Optional<DeserializingMessage> deserializeFirstMessage(io.fluxzero.common.api.SerializedMessage message, io.fluxzero.common.MessageType messageType, String topic) Description copied from interface:SerializerDeserializes oneSerializedMessageinto the first resultingDeserializingMessage.This preserves the same semantics as
deserializeMessages(Stream.of(message), ...).findAny(): upcasters that drop a message returnOptional.empty(), and upcasters that split a message contribute only their first result.- Specified by:
deserializeFirstMessagein interfaceSerializer- Parameters:
message- the serialized messagemessageType- the type of message (COMMAND, EVENT, etc.)topic- the topic of the message if the type is CUSTOM or DOCUMENT, otherwisenull- Returns:
- the first deserialized message, if any
-
deserializeFirstMessageOrNull
public DeserializingMessage deserializeFirstMessageOrNull(io.fluxzero.common.api.SerializedMessage message, io.fluxzero.common.MessageType messageType, String topic) Description copied from interface:SerializerDeserializes oneSerializedMessageinto the first resultingDeserializingMessage, ornullif no result remains.This preserves the same semantics as
deserializeMessages(Stream.of(message), ...).findAny(): upcasters that drop a message returnnull, and upcasters that split a message contribute only their first result.- Specified by:
deserializeFirstMessageOrNullin interfaceSerializer- Parameters:
message- the serialized messagemessageType- the type of message (COMMAND, EVENT, etc.)topic- the topic of the message if the type is CUSTOM or DOCUMENT, otherwisenull- Returns:
- the first deserialized message, or
null
-
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
-
registerTypeAlias
Description copied from interface:SerializerRegisters an exact alias from a serialized type name to its current type name.This is the preferred name for
Serializer.registerTypeCaster(String, String). Multiple aliases may be registered and chained. Exact aliases take precedence over package aliases.- Specified by:
registerTypeAliasin interfaceSerializer- Parameters:
oldType- the legacy serialized type namenewType- the current type name- Returns:
- a registration handle
-
registerPackageAlias
Description copied from interface:SerializerRegisters an alias from a legacy package and all its subpackages to a current package.Implementations that support package aliases should preserve the class-name suffix and prefer the longest matching package prefix when multiple aliases match.
- Specified by:
registerPackageAliasin interfaceSerializer- Parameters:
oldPackage- the legacy package name, without a trailing wildcardnewPackage- the current package name, without a trailing wildcard- Returns:
- a registration handle
-
upcastType
Resolves the current type from potentially chained exact and package aliases.- Specified by:
upcastTypein interfaceSerializer- Parameters:
type- the original type- Returns:
- the remapped (or unchanged) type name
-
hasTypeAliases
protected boolean hasTypeAliases()Indicates whether exact or package aliases are currently registered. -
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
protected Stream<DeserializingObject<byte[],?>> deserializeOtherFormat(io.fluxzero.common.api.SerializedObject<byte[]> s) Handles deserialization of objects in formats other than the current default (e.g., fallback string deserialization). -
deserializeUnknownType
protected Stream<DeserializingObject<byte[],?>> deserializeUnknownType(io.fluxzero.common.api.SerializedObject<?> serializedObject) Hook for handling deserialization of unknown types. Subclasses can override to log or recover gracefully. -
doDeserialize
-
asIntermediateValue
-