Class AvroSerializer
- All Implemented Interfaces:
Serializer
This serializer is intended to work for classes, representing messages specified by Avro Schema. It uses a delegate
serializer to serialize all other artifacts: metadata, tokens, sagas, snapshots and other non-message artifacts. In
general, it is a good idea to configure event and/or
message serializer to use avro, but let your default serializer
use jackson or another serializer of your choice.
The serialization/deserialization is delegated to the AvroSerializerStrategy implementations. By default, the
SpecificRecordBaseSerializerStrategy is provided and configured, to support Java classes generated by Avro
Maven plugin (delivered by Avro Java distribution). The AvroSerializer.Builder allows to register further strategies.
At least one strategy has to be configured for this serializer to work.
- Since:
- 4.11.0
- Author:
- Simon Zambrovski, Jan Galinski
-
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionstatic classBuilder to set up Avro Serializer using some defaults. -
Constructor Summary
ConstructorsModifierConstructorDescriptionprotectedAvroSerializer(AvroSerializer.Builder builder) Creates the serializer instance. -
Method Summary
Modifier and TypeMethodDescriptionstatic AvroSerializer.Builderbuilder()Creates a builder for Avro Serializer with defaults applied.<T> booleancanSerializeTo(Class<T> expectedRepresentation) Indicates whether this Serializer is capable of serializing to the givenexpectedRepresentation.classForType(SerializedType type) Returns the class for the given type identifier.<S,T> T deserialize(SerializedObject<S> serializedObject) Deserializes the first object read from the givenbytes.Returns theConverterused by this Serializer to convert between serialized representations.protected StringresolveClassName(SerializedType serializedType) Resolve the class name from the givenserializedType.<T> SerializedObject<T> Serialize the givenobjectinto a Serialized Object containing the givenexpectedRepresentation.typeForClass(Class type) Returns the type identifier for the given class.
-
Constructor Details
-
AvroSerializer
Creates the serializer instance.- Parameters:
builder- builder containing relevant settings.
-
-
Method Details
-
builder
Creates a builder for Avro Serializer with defaults applied.The
RevisionResolverdefaults toAnnotationRevisionResolver, theConverterdefaults toChainingConverterand the default serialization strategies are enabled, which effectively activates theSpecificRecordBaseSerializerStrategy.The following fields are mandatory, to create the Serializer:
AvroSerializer.Builder.schemaStore(SchemaStore), to lookup schemas by their fingerprintsAvroSerializer.Builder.serializerDelegate(Serializer), to deserialize all non-Avro artifacts- at least one
AvroSerializerStrategyeither passed viaAvroSerializer.Builder.addSerializerStrategy(AvroSerializerStrategy)or activated viaAvroSerializer.Builder.includeDefaultAvroSerializationStrategies(boolean), which is a default.
- Returns:
- fluent builder instance.
-
serialize
Description copied from interface:SerializerSerialize the givenobjectinto a Serialized Object containing the givenexpectedRepresentation. UseSerializer.canSerializeTo(Class)to detect whether theexpectedRepresentationis supported by this serializer.- Specified by:
serializein interfaceSerializer- Type Parameters:
T- The expected data type representing the serialized object- Parameters:
object- The object to serializeexpectedRepresentation- The expected data type representing the serialized object- Returns:
- the instance representing the serialized object.
-
deserialize
Description copied from interface:SerializerDeserializes the first object read from the givenbytes. Thebytesare not consumed from the array or modified in any way. The resulting object instance is cast to the expected type.- Specified by:
deserializein interfaceSerializer- Type Parameters:
S- The data type of the serialized objectT- The expected deserialized type- Parameters:
serializedObject- the instance describing the type of object and the bytes providing the serialized data- Returns:
- the serialized object, cast to the expected type
-
canSerializeTo
Description copied from interface:SerializerIndicates whether this Serializer is capable of serializing to the givenexpectedRepresentation. Whentrue, this does *not* guarantee that the serialization and (optional) conversion will also succeed when executed. For example, when a serializer produces abyte[]containing JSON, trying to convert to a Dom4J Document will fail, even though this serializer has a converter to convertbyte[]to Dom4J instances.- Specified by:
canSerializeToin interfaceSerializer- Type Parameters:
T- The type of data a Serialized Object should contain- Parameters:
expectedRepresentation- The type of data a Serialized Object should contain- Returns:
trueif theexpectedRepresentationis supported, otherwisefalse.
-
classForType
Description copied from interface:SerializerReturns the class for the given type identifier. The result of this method must guarantee that the deserialized SerializedObject with the giventypeis an instance of the returned Class. If a class cannot be resolved (i.e. because the class is not available on this JVM's classpath) this method returns anUnknownSerializedTypeinstance which proides access to the raw underlying data.- Specified by:
classForTypein interfaceSerializer- Parameters:
type- The type identifier of the object- Returns:
- the Class representing the type of the serialized Object
-
resolveClassName
Resolve the class name from the givenserializedType. This method may be overridden to customize the names used to denote certain classes, for example, by leaving out a certain base package for brevity.- Parameters:
serializedType- The serialized type to resolve the class name for- Returns:
- The fully qualified name of the class to load
-
typeForClass
Description copied from interface:SerializerReturns the type identifier for the given class. This is the type identifier of the Serialized object as returned bySerializer.serialize(Object, Class).- Specified by:
typeForClassin interfaceSerializer- Parameters:
type- Class representing the type of the serializable Object.- Returns:
- The type identifier of the object
-
getConverter
Description copied from interface:SerializerReturns theConverterused by this Serializer to convert between serialized representations. Generally, this Converter depends on the type of data the serializer serializes to.- Specified by:
getConverterin interfaceSerializer- Returns:
- the converter used by this Serializer
-