Class JacksonSerializer

java.lang.Object
io.fluxzero.sdk.common.serialization.AbstractSerializer<com.fasterxml.jackson.databind.JsonNode>
io.fluxzero.sdk.common.serialization.jackson.JacksonSerializer
All Implemented Interfaces:
ContentFilter, Serializer, DocumentSerializer

public class JacksonSerializer extends AbstractSerializer<com.fasterxml.jackson.databind.JsonNode> implements DocumentSerializer
A concrete Serializer implementation based on Jackson.

This is the default serializer used in Fluxzero, supporting:

  • Serialization and deserialization using Jackson's ObjectMapper
  • Integration with upcasters/downcasters for versioned data evolution
  • Intermediate representation based on JsonNode for revision tracking
  • DocumentSerializer support for document store interoperability
  • Type caching and memoization for performance

You can customize or replace this serializer entirely by subclassing or injecting your own implementation of AbstractSerializer.

  • Field Details

    • defaultObjectMapper

      public static com.fasterxml.jackson.databind.json.JsonMapper defaultObjectMapper
      Default JsonMapper instance used for JSON serialization and deserialization.

      In advanced scenarios, users may replace this field with a custom JsonMapper. However, this is generally discouraged unless strictly necessary.

      A better approach for customizing Jackson behavior is to provide your own modules via the Jackson Module SPI (ServiceLoader mechanism), which avoids overriding global configuration and ensures compatibility.

      Warning: This mapper is also used to construct and parse search documents. Misconfiguration may result in inconsistencies in search indexing or data loss.

  • Constructor Details

    • JacksonSerializer

      public JacksonSerializer()
      Constructs a default JacksonSerializer with no up/downcasters.
    • JacksonSerializer

      public JacksonSerializer(Collection<?> casterCandidates)
      Constructs a JacksonSerializer with the given up/downcaster candidates.
    • JacksonSerializer

      public JacksonSerializer(com.fasterxml.jackson.databind.json.JsonMapper objectMapper)
      Constructs a JacksonSerializer with a specific JsonMapper instance.
    • JacksonSerializer

      public JacksonSerializer(com.fasterxml.jackson.databind.json.JsonMapper objectMapper, Collection<?> casterCandidates)
      Constructs a JacksonSerializer with an object mapper and up/downcaster candidates.
    • JacksonSerializer

      public JacksonSerializer(com.fasterxml.jackson.databind.json.JsonMapper objectMapper, Collection<?> casterCandidates, JacksonInverter inverter)
      Full constructor with object mapper, caster candidates and custom document inverter.
  • Method Details

    • supportsConsumerFiltering

      protected boolean supportsConsumerFiltering()
      Returns a canonical string name for the given type.
      Overrides:
      supportsConsumerFiltering in class AbstractSerializer<com.fasterxml.jackson.databind.JsonNode>
    • asString

      protected String asString(Type type)
      Description copied from class: AbstractSerializer
      Converts a Type to a readable string name.
      Overrides:
      asString in class AbstractSerializer<com.fasterxml.jackson.databind.JsonNode>
    • serializedPropertyPaths

      public List<String> serializedPropertyPaths(Object payload, String propertyPath)
      Maps standard bean properties using this mapper's naming, alias and unwrapping configuration. Custom codecs, dynamic output and type/root wrappers require an explicit mapping override; unsupported mappings fail closed.
      Specified by:
      serializedPropertyPaths in interface Serializer
    • doSerialize

      protected byte[] doSerialize(Object object) throws Exception
      Serializes the object to a JSON byte array.
      Specified by:
      doSerialize in class AbstractSerializer<com.fasterxml.jackson.databind.JsonNode>
      Throws:
      Exception
    • doDeserialize

      protected Object doDeserialize(Data<?> data, String type) throws Exception
      Deserializes a Data instance into an object of the given type using the Jackson object mapper. Supports JsonNode, byte arrays and strings as serialized input.
      Specified by:
      doDeserialize in class AbstractSerializer<com.fasterxml.jackson.databind.JsonNode>
      Throws:
      Exception
    • asIntermediateValue

      protected com.fasterxml.jackson.databind.JsonNode asIntermediateValue(Object input)
      Converts the given object into a JsonNode for use in revision downcasting.
      Specified by:
      asIntermediateValue in class AbstractSerializer<com.fasterxml.jackson.databind.JsonNode>
    • isKnownType

      protected boolean isKnownType(String type)
      Determines whether the given type is known to the Jackson type system.
      Overrides:
      isKnownType in class AbstractSerializer<com.fasterxml.jackson.databind.JsonNode>
    • deserializeUnknownType

      protected Stream<DeserializingObject<byte[],?>> deserializeUnknownType(SerializedObject<?> s)
      Fallback handler for deserialization of unknown types. Attempts best-effort conversion using Jackson.
      Overrides:
      deserializeUnknownType in class AbstractSerializer<com.fasterxml.jackson.databind.JsonNode>
    • getJavaType

      protected com.fasterxml.jackson.databind.JavaType getJavaType(String type)
      Resolves a canonical or registered simple/partial JavaType for the given string-based type name. Historical constant-specific enum subclass names are accepted after type aliases and upcasters have run.
    • getCanonicalType

      protected String getCanonicalType(Type type)
      Computes the canonical string representation of a Type. Constant-specific enum subclasses use their declaring enum unless the configured mapper already supports their original type. This also applies to inferred collection elements and map keys/values.
    • getRevision

      protected int getRevision(Object object)
      Uses the declaring enum's revision when its constant-specific subclass is normalized to that enum. Explicitly supported subtype codecs retain their original type and revision.
      Overrides:
      getRevision in class AbstractSerializer<com.fasterxml.jackson.databind.JsonNode>
    • toDocument

      public SerializedDocument toDocument(Object value, String id, String collection, Instant timestamp, Instant end, Metadata metadata)
      Description copied from interface: DocumentSerializer
      Serializes a given value into a SerializedDocument, using the specified identifiers, timestamps, and metadata.
      Specified by:
      toDocument in interface DocumentSerializer
      Parameters:
      value - the value to be serialized
      id - the unique identifier of the document (within the collection)
      collection - the name of the document collection
      timestamp - the timestamp marking the start of the document's relevance
      end - optional timestamp marking the end of the document's relevance
      metadata - additional metadata to include with the document
      Returns:
      a SerializedDocument representing the given value
    • fromDocument

      public <T> T fromDocument(SerializedDocument document)
      Description copied from interface: DocumentSerializer
      Deserializes the payload of the given document into a Java object using the type information contained in the document.
      Specified by:
      fromDocument in interface DocumentSerializer
      Type Parameters:
      T - the target type
      Parameters:
      document - the complete SerializedDocument to deserialize
      Returns:
      the deserialized object
    • modelStateSnapshot

      public Object modelStateSnapshot(Object value)
      Description copied from interface: DocumentSerializer
      Captures an immutable, equality-comparable representation of the complete logical Model state for schema-only source handlers. This must not depend on storage envelopes (for example random encryption IVs), and subsequent mutation of the input must not change the snapshot. Custom serializers may implement this opt-in capability; other document operations do not require it.
      Specified by:
      modelStateSnapshot in interface DocumentSerializer
    • fromDocument

      public <T> T fromDocument(SerializedDocument document, Class<T> type)
      Description copied from interface: DocumentSerializer
      Deserializes the payload of the given document into an instance of the specified type.
      Specified by:
      fromDocument in interface DocumentSerializer
      Type Parameters:
      T - the target type
      Parameters:
      document - the complete SerializedDocument to deserialize
      type - the target class for deserialization
      Returns:
      the deserialized object
    • doConvert

      public <V> V doConvert(Object value, Type type)
      Converts an object into another type using Jackson’s ObjectMapper.
      Specified by:
      doConvert in class AbstractSerializer<com.fasterxml.jackson.databind.JsonNode>
    • doClone

      public Object doClone(Object value)
      Performs a field-level clone by copying values from the original object into a new instance of the same type. This first converts the value to an ObjectNode.
      Specified by:
      doClone in class AbstractSerializer<com.fasterxml.jackson.databind.JsonNode>