Class AbstractSerializer<I>
java.lang.Object
io.fluxcapacitor.javaclient.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 Flux Capacitor framework for all (de)serialization needs.
-
Constructor Summary
ConstructorsModifierConstructorDescriptionprotected
AbstractSerializer
(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 I
asIntermediateValue
(Object input) Converts an object into its intermediate form (e.g., JsonNode) used for revision downcasting.protected String
Converts aType
to a readable string name.<V> V
Creates a deep copy of the object.<V> V
Converts 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 ofSerializedObject
values 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 Object
Hook to clone a deserialized object.protected abstract <V> V
Converts a deserialized object to the desired target type.protected abstract Object
doDeserialize
(Data<?> data, String type) Core method to deserialize the givenData
with 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 inData
form.Downcasts a deserialized object to the desired revision number.protected Type
Determines the full type signature of the given object.protected String
getTypeString
(Object object) Resolves the effective string representation of an object's type, including generic parameters if applicable.protected boolean
isKnownType
(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-basedData
object, 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 java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
Methods inherited from interface io.fluxcapacitor.javaclient.common.serialization.ContentFilter
filterContent
Methods inherited from interface io.fluxcapacitor.javaclient.common.serialization.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-basedData
object, using the specified format.- Specified by:
serialize
in interfaceSerializer
- Parameters:
object
- the object to serializeformat
- the desired serialization format- Returns:
- a
Data<byte[]>
object containing the serialized bytes
-
serializeToOtherFormat
Converts common simple object types (e.g., String, byte[]) to the requested alternative format. Used when the object cannot be serialized using the current default format. -
getType
Determines the full type signature of the given object. -
asString
Converts aType
to a readable string name. -
getTypeString
Resolves the effective string representation of an object's type, including generic parameters if applicable. -
doSerialize
Hook for serializing the object to bytes using the primary format. Must be implemented by subclasses.- Throws:
Exception
-
deserialize
public <S extends SerializedObject<byte[]>> Stream<DeserializingObject<byte[],S>> deserialize(Stream<S> dataStream, UnknownTypeStrategy unknownTypeStrategy) Deserializes a stream ofSerializedObject
values into deserialized objects. Applies upcasters, format detection, and lazy deserialization as needed.- Specified by:
deserialize
in 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:
convert
in 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:
clone
in 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:
registerUpcasters
in 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:
registerDowncasters
in 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:
registerTypeCaster
in 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:
upcastType
in 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:
downcast
in interfaceSerializer
- Parameters:
object
- the object to downcastdesiredRevision
- the target revision- Returns:
- a revisioned form of the object
-
downcast
Downcasts an object already inData
form.- Specified by:
downcast
in interfaceSerializer
- Parameters:
object
- the serialized datadesiredRevision
- the target revision number- Returns:
- a transformed object matching the older revision
-
doClone
Hook to clone a deserialized object. Subclasses must implement this if custom cloning is needed. -
doConvert
Converts a deserialized object to the desired target type. May delegate to a type mapping library. -
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
Core method to deserialize the givenData
with an explicit type hint. Must be implemented by concrete subclasses.- Throws:
Exception
-
asIntermediateValue
Converts an object into its intermediate form (e.g., JsonNode) used for revision downcasting. Must be implemented by concrete serializers.
-