Class JacksonSerializer

java.lang.Object
org.axonframework.serialization.json.JacksonSerializer
All Implemented Interfaces:
Serializer

public class JacksonSerializer extends Object implements Serializer
Serializer implementation that uses Jackson 2 to serialize objects into a JSON format. Although the Jackson serializer requires classes to be compatible with this specific serializer, it provides much more compact serialization, while still being human-readable.
Since:
2.2
Author:
Allard Buijze
  • Constructor Details

  • Method Details

    • builder

      public static JacksonSerializer.Builder builder()
      Instantiate a Builder to be able to create a JacksonSerializer.

      The RevisionResolver is defaulted to an AnnotationRevisionResolver, the Converter to a ChainingConverter, the ObjectMapper defaults to a ObjectMapper() result and the ClassLoader to the ClassLoader of this class.

      Upon instantiation, the ObjectMapper will get two modules registered to it by default, (1) the MetaDataDeserializer and the (2) JavaTimeModule. Lastly, if the provided converter is of type ChainingConverter, the registerConverters(org.axonframework.serialization.ChainingConverter) is performed to automatically add the JsonNodeToByteArrayConverter and ByteArrayToJsonNodeConverter.

      Returns:
      a Builder to be able to create a JacksonSerializer
    • defaultSerializer

      public static JacksonSerializer defaultSerializer()
      Instantiate a default JacksonSerializer.

      The RevisionResolver is defaulted to an AnnotationRevisionResolver, the Converter to a ChainingConverter, the ObjectMapper defaults to a ObjectMapper() result and the ClassLoader to the ClassLoader of this class.

      Upon instantiation, the ObjectMapper will get two modules registered to it by default, (1) the MetaDataDeserializer and the (2) JavaTimeModule. Lastly, if the provided converter is of type ChainingConverter, the registerConverters(org.axonframework.serialization.ChainingConverter) is performed to automatically add the JsonNodeToByteArrayConverter and ByteArrayToJsonNodeConverter.

      Returns:
      a JacksonSerializer
    • registerConverters

      protected void registerConverters(ChainingConverter converter)
      Registers converters with the given converter which depend on the actual contents of the serialized form to represent a JSON format.
      Parameters:
      converter - The ChainingConverter instance to register the converters with.
    • serialize

      public <T> SerializedObject<T> serialize(Object object, @Nonnull Class<T> expectedRepresentation)
      Description copied from interface: Serializer
      Serialize the given object into a Serialized Object containing the given expectedRepresentation.

      Use Serializer.canSerializeTo(Class) to detect whether the expectedRepresentation is supported by this serializer.

      Specified by:
      serialize in interface Serializer
      Type Parameters:
      T - The expected data type representing the serialized object
      Parameters:
      object - The object to serialize
      expectedRepresentation - 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(@Nonnull Class<T> expectedRepresentation)
      Description copied from interface: Serializer
      Indicates whether this Serializer is capable of serializing to the given 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.

      Specified by:
      canSerializeTo in interface Serializer
      Type Parameters:
      T - The type of data a Serialized Object should contain
      Parameters:
      expectedRepresentation - The type of data a Serialized Object should contain
      Returns:
      true if the expectedRepresentation is supported, otherwise false.
    • deserialize

      public <S, T> T deserialize(@Nonnull SerializedObject<S> serializedObject)
      Description copied from interface: Serializer
      Deserializes the first object read from the given bytes. The bytes are not consumed from the array or modified in any way. The resulting object instance is cast to the expected type.
      Specified by:
      deserialize in interface Serializer
      Type Parameters:
      S - The data type of the serialized object
      T - 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(@Nonnull SerializedType type)
      Description copied from interface: Serializer
      Returns the class for the given type identifier. The result of this method must guarantee that the deserialized SerializedObject with the given 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.

      Specified by:
      classForType in interface Serializer
      Parameters:
      type - The type identifier of the object
      Returns:
      the Class representing the type of the serialized Object
    • resolveClassName

      protected String resolveClassName(SerializedType serializedType)
      Resolve the class name from the given 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.
      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: Serializer
      Returns the type identifier for the given class. This is the type identifier of the Serialized object as returned by Serializer.serialize(Object, Class).
      Specified by:
      typeForClass in interface Serializer
      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: Serializer
      Returns the Converter used by this Serializer to convert between serialized representations. Generally, this Converter depends on the type of data the serializer serializes to.
      Specified by:
      getConverter in interface Serializer
      Returns:
      the converter used by this Serializer
    • getRevisionResolver

      protected RevisionResolver getRevisionResolver()
      Returns the revision resolver used by this serializer.
      Returns:
      the revision resolver