public class AvroSerializer extends Object implements 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.
Modifier and Type | Class and Description |
---|---|
static class |
AvroSerializer.Builder
Builder to set up Avro Serializer using some defaults.
|
Modifier | Constructor and Description |
---|---|
protected |
AvroSerializer(AvroSerializer.Builder builder)
Creates the serializer instance.
|
Modifier and Type | Method and Description |
---|---|
static AvroSerializer.Builder |
builder()
Creates a builder for Avro Serializer with defaults applied.
|
<T> boolean |
canSerializeTo(Class<T> expectedRepresentation)
Indicates whether this Serializer is capable of serializing to the given
expectedRepresentation . |
Class |
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 given
bytes . |
Converter |
getConverter()
Returns the
Converter used by this Serializer to convert between serialized representations. |
protected String |
resolveClassName(SerializedType serializedType)
Resolve the class name from the given
serializedType . |
<T> SerializedObject<T> |
serialize(Object object,
Class<T> expectedRepresentation)
Serialize the given
object into a Serialized Object containing the given
expectedRepresentation . |
SerializedType |
typeForClass(Class type)
Returns the type identifier for the given class.
|
protected AvroSerializer(@Nonnull AvroSerializer.Builder builder)
builder
- builder containing relevant settings.public static AvroSerializer.Builder builder()
The RevisionResolver
defaults to AnnotationRevisionResolver
, the Converter
defaults
to ChainingConverter
and the default serialization strategies are enabled, which effectively activates
the SpecificRecordBaseSerializerStrategy
.
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 artifactsAvroSerializerStrategy
either passed via
AvroSerializer.Builder.addSerializerStrategy(AvroSerializerStrategy)
or activated via
AvroSerializer.Builder.includeDefaultAvroSerializationStrategies(boolean)
, which is a default.public <T> SerializedObject<T> serialize(Object object, @Nonnull Class<T> expectedRepresentation)
Serializer
object
into a Serialized Object containing the given
expectedRepresentation
.
Use Serializer.canSerializeTo(Class)
to detect whether the expectedRepresentation
is supported by
this serializer.serialize
in interface Serializer
T
- The expected data type representing the serialized objectobject
- The object to serializeexpectedRepresentation
- The expected data type representing the serialized objectpublic <S,T> T deserialize(@Nonnull SerializedObject<S> serializedObject)
Serializer
bytes
. The bytes
are not consumed
from the array or modified in any way. The resulting object instance is cast to the expected type.deserialize
in interface Serializer
S
- The data type of the serialized objectT
- The expected deserialized typeserializedObject
- the instance describing the type of object and the bytes providing the serialized datapublic <T> boolean canSerializeTo(@Nonnull Class<T> expectedRepresentation)
Serializer
expectedRepresentation
.
When true
, this does *not* guarantee that the serialization and (optional) conversion will also
succeed when executed. For example, when a serializer produces a byte[]
containing JSON, trying to
convert to a Dom4J Document will fail, even though this serializer has a converter to convert
byte[]
to Dom4J instances.canSerializeTo
in interface Serializer
T
- The type of data a Serialized Object should containexpectedRepresentation
- The type of data a Serialized Object should containtrue
if the expectedRepresentation
is supported, otherwise false
.public Class classForType(@Nonnull SerializedType type)
Serializer
type
is 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 an UnknownSerializedType
instance which proides access to the raw underlying data.classForType
in interface Serializer
type
- The type identifier of the objectprotected String resolveClassName(SerializedType serializedType)
serializedType
. 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.serializedType
- The serialized type to resolve the class name forpublic SerializedType typeForClass(@Nullable Class type)
Serializer
Serializer.serialize(Object, Class)
.typeForClass
in interface Serializer
type
- Class representing the type of the serializable Object.public Converter getConverter()
Serializer
Converter
used by this Serializer to convert between serialized representations. Generally,
this Converter depends on the type of data the serializer serializes to.getConverter
in interface Serializer
Copyright © 2010–2025. All rights reserved.