Class JacksonSerializer
- java.lang.Object
-
- io.axoniq.dataprotection.serialization.json.JacksonSerializer
-
- All Implemented Interfaces:
Serializer
public class JacksonSerializer extends Object implements Serializer
Serializer implementation that uses Jackson to serialize objects into a JSON format. Although the Jackson serializer requires classes to be compatible with this specific serializer, it providers much more compact serialization, while still being human readable.- Since:
- 2.2
- Author:
- Allard Buijze
-
-
Constructor Summary
Constructors Constructor Description JacksonSerializer()Initialize the serializer with a default ObjectMapper instance.JacksonSerializer(com.fasterxml.jackson.databind.ObjectMapper objectMapper)Initialize the serializer with the givenobjectMapperto serialize and parse the objects to JSON.JacksonSerializer(com.fasterxml.jackson.databind.ObjectMapper objectMapper, RevisionResolver revisionResolver)Initialize the serializer with the givenobjectMapperto serialize and parse the objects to JSON.JacksonSerializer(com.fasterxml.jackson.databind.ObjectMapper objectMapper, RevisionResolver revisionResolver, Converter converter)Initialize the serializer with the givenobjectMapperto serialize and parse the objects to JSON.JacksonSerializer(com.fasterxml.jackson.databind.ObjectMapper objectMapper, RevisionResolver revisionResolver, Converter converter, ClassLoader classLoader)Initialize the serializer with the givenobjectMapperto serialize and parse the objects to JSON.JacksonSerializer(RevisionResolver revisionResolver, Converter converter)Initialize the serializer using a default ObjectMapper instance, using the givenrevisionResolverto define revision for each object to serialize, and givenconverterto be used by upcasters.
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description <T> booleancanSerializeTo(Class<T> expectedRepresentation)Indicates whether this Serializer is capable of serializing to the givenexpectedRepresentation.ClassclassForType(SerializedType type)Returns the class for the given type identifier.<S,T>
Tdeserialize(SerializedObject<S> serializedObject)Deserializes the first object read from the givenbytes.ConvertergetConverter()Returns theConverterused by this Serializer to convert between serialized representations.com.fasterxml.jackson.databind.ObjectMappergetObjectMapper()Returns the ObjectMapper used by this serializer, allowing for configuration of the serialization settings.protected com.fasterxml.jackson.databind.ObjectReadergetReader(Class<?> type)Provides the ObjectReader, with which objects are read from the JSON form.protected RevisionResolvergetRevisionResolver()Returns the revision resolver used by this serializer.protected com.fasterxml.jackson.databind.ObjectWritergetWriter()Provides the ObjectWriter, with which objects are serialized to JSON form.protected voidregisterConverters(ChainingConverter converter)Registers converters with the givenconverterwhich depend on the actual contents of the serialized for to represent a JSON format.protected StringresolveClassName(SerializedType serializedType)Resolve the class name from the givenserializedType.<T> SerializedObject<T>serialize(Object object, Class<T> expectedRepresentation)Serialize the givenobjectinto a Serialized Object containing the givenexpectedRepresentation.SerializedTypetypeForClass(Class type)Returns the type identifier for the given class.
-
-
-
Constructor Detail
-
JacksonSerializer
public JacksonSerializer()
Initialize the serializer with a default ObjectMapper instance. Revisions are resolved using@Revisionannotations on the serialized classes.
-
JacksonSerializer
public JacksonSerializer(com.fasterxml.jackson.databind.ObjectMapper objectMapper)
Initialize the serializer with the givenobjectMapperto serialize and parse the objects to JSON. This objectMapper allows for customization of the serialized form.- Parameters:
objectMapper- The objectMapper to serialize objects and parse JSON with
-
JacksonSerializer
public JacksonSerializer(RevisionResolver revisionResolver, Converter converter)
Initialize the serializer using a default ObjectMapper instance, using the givenrevisionResolverto define revision for each object to serialize, and givenconverterto be used by upcasters.- Parameters:
revisionResolver- The strategy to use to resolve the revision of an objectconverter- The factory providing the converter instances for upcasters
-
JacksonSerializer
public JacksonSerializer(com.fasterxml.jackson.databind.ObjectMapper objectMapper, RevisionResolver revisionResolver)Initialize the serializer with the givenobjectMapperto serialize and parse the objects to JSON. This objectMapper allows for customization of the serialized form. The givenrevisionResolveris used to resolve the revision from an object to be serialized.- Parameters:
objectMapper- The objectMapper to serialize objects and parse JSON withrevisionResolver- The strategy to use to resolve the revision of an object
-
JacksonSerializer
public JacksonSerializer(com.fasterxml.jackson.databind.ObjectMapper objectMapper, RevisionResolver revisionResolver, Converter converter)Initialize the serializer with the givenobjectMapperto serialize and parse the objects to JSON. This objectMapper allows for customization of the serialized form. The givenrevisionResolveris used to resolve the revision from an object to be serialized. The givenconverteris the converter factory used by upcasters to convert between content types.- Parameters:
objectMapper- The objectMapper to serialize objects and parse JSON withrevisionResolver- The strategy to use to resolve the revision of an objectconverter- The factory providing the converter instances for upcasters
-
JacksonSerializer
public JacksonSerializer(com.fasterxml.jackson.databind.ObjectMapper objectMapper, RevisionResolver revisionResolver, Converter converter, ClassLoader classLoader)Initialize the serializer with the givenobjectMapperto serialize and parse the objects to JSON. This objectMapper allows for customization of the serialized form. The givenrevisionResolveris used to resolve the revision from an object to be serialized. The givenconverteris the converter factory used by upcasters to convert between content types.- Parameters:
objectMapper- The objectMapper to serialize objects and parse JSON withrevisionResolver- The strategy to use to resolve the revision of an objectconverter- The factory providing the converter instances for upcastersclassLoader- The class loader to load classes with when deserializing
-
-
Method Detail
-
registerConverters
protected void registerConverters(ChainingConverter converter)
Registers converters with the givenconverterwhich depend on the actual contents of the serialized for to represent a JSON format.- Parameters:
converter- The ChainingConverter instance to register the converters with.
-
serialize
public <T> SerializedObject<T> serialize(Object object, Class<T> expectedRepresentation)
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.
-
getObjectMapper
public final com.fasterxml.jackson.databind.ObjectMapper getObjectMapper()
Returns the ObjectMapper used by this serializer, allowing for configuration of the serialization settings.- Returns:
- the ObjectMapper instance used by his serializer
-
getWriter
protected com.fasterxml.jackson.databind.ObjectWriter getWriter()
Provides the ObjectWriter, with which objects are serialized to JSON form. This method may be overridden to change the configuration of the writer to use.- Returns:
- The writer to serialize objects with
-
getReader
protected com.fasterxml.jackson.databind.ObjectReader getReader(Class<?> type)
Provides the ObjectReader, with which objects are read from the JSON form. This method may be overridden to change the configuration of the reader to use.- Parameters:
type- The type of object to create a reader for- Returns:
- The writer to serialize objects with
-
canSerializeTo
public <T> boolean canSerializeTo(Class<T> expectedRepresentation)
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.
-
deserialize
public <S,T> T deserialize(SerializedObject<S> serializedObject)
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
-
classForType
public Class classForType(SerializedType type) throws UnknownSerializedTypeException
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 throws UnknownSerializedTypeException.- Specified by:
classForTypein interfaceSerializer- Parameters:
type- The type identifier of the object- Returns:
- the Class representing the type of the serialized Object
- Throws:
UnknownSerializedTypeException- if thetypecannot be resolved to a class
-
resolveClassName
protected String resolveClassName(SerializedType serializedType)
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
public SerializedType typeForClass(Class type)
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
public Converter 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
-
getRevisionResolver
protected RevisionResolver getRevisionResolver()
Returns the revision resolver used by this serializer.- Returns:
- the revision resolver
-
-