Class ElementMapper

java.lang.Object
org.gcube.informationsystem.serialization.ElementMapper

public class ElementMapper extends Object
A central class for handling JSON serialization and deserialization of Element types.

This class configures a Jackson ObjectMapper with custom settings for the Information System model, including subtype registration and fallback mechanisms for unknown types.

Author:
Luca Frosini (ISTI - CNR)
  • Field Details

    • mapper

      protected static final org.gcube.com.fasterxml.jackson.databind.ObjectMapper mapper
      The object mapper.
    • knownTypes

      protected static final Map<String,Class<? extends Element>> knownTypes
      A map of known types, keyed by type name.
    • interfaceToImplementation

      protected static final Map<Class<? extends Element>,Class<? extends Element>> interfaceToImplementation
      A map of interfaces to their implementation classes.
  • Constructor Details

    • ElementMapper

      public ElementMapper()
  • Method Details

    • getObjectMapper

      public static org.gcube.com.fasterxml.jackson.databind.ObjectMapper getObjectMapper()
      Returns the configured ObjectMapper instance.
      Returns:
      The singleton ObjectMapper.
    • addDynamicAssociation

      public static <El extends Element, ELImpl extends El> void addDynamicAssociation(Class<El> interfaceClz, Class<ELImpl> implementationClass)
      Adds a dynamic mapping between an interface and its implementation class.
      Type Parameters:
      El - The interface type.
      ELImpl - The implementation type.
      Parameters:
      interfaceClz - The interface class.
      implementationClass - The implementation class.
    • registerSubtypes

      protected static <El extends Element, ELImpl extends El> void registerSubtypes(Class<El> interfaceClz, Class<ELImpl> implementationClass)
      Registers a subtype with the object mapper.
      Type Parameters:
      El - The element type.
      ELImpl - The implementation type.
      Parameters:
      interfaceClz - The interface class.
      implementationClass - The implementation class.
    • registerSubtype

      public static <El extends Element> void registerSubtype(Class<El> clz)
      Registers a subtype with the object mapper.
      Type Parameters:
      El - The element type.
      Parameters:
      clz - The class to register.
    • marshal

      public static <T extends OutputStream, El extends Element> T marshal(El object, T stream) throws org.gcube.com.fasterxml.jackson.core.JsonGenerationException, org.gcube.com.fasterxml.jackson.databind.JsonMappingException, IOException
      Serializes an object to an OutputStream.
      Type Parameters:
      T - The type of the output stream.
      El - The type of the element.
      Parameters:
      object - The object to serialize.
      stream - The output stream.
      Returns:
      The output stream.
      Throws:
      IOException - if an error occurs during serialization.
      org.gcube.com.fasterxml.jackson.core.JsonGenerationException - if an error occurs during JSON generation.
      org.gcube.com.fasterxml.jackson.databind.JsonMappingException - if an error occurs during JSON mapping.
    • marshal

      public static <T extends Writer, El extends Element> T marshal(El object, T writer) throws org.gcube.com.fasterxml.jackson.core.JsonGenerationException, org.gcube.com.fasterxml.jackson.databind.JsonMappingException, IOException
      Serializes an object to a Writer.
      Type Parameters:
      T - The type of the writer.
      El - The type of the element.
      Parameters:
      object - The object to serialize.
      writer - The writer.
      Returns:
      The writer.
      Throws:
      IOException - if an error occurs during serialization.
      org.gcube.com.fasterxml.jackson.core.JsonGenerationException - if an error occurs during JSON generation.
      org.gcube.com.fasterxml.jackson.databind.JsonMappingException - if an error occurs during JSON mapping.
    • marshal

      public static <El extends Element> String marshal(El object) throws org.gcube.com.fasterxml.jackson.core.JsonProcessingException
      Serializes an object to a JSON string.
      Type Parameters:
      El - The type of the element.
      Parameters:
      object - The object to serialize.
      Returns:
      The JSON string.
      Throws:
      org.gcube.com.fasterxml.jackson.core.JsonProcessingException - if an error occurs during serialization.
    • marshal

      public static <El extends Element> String marshal(List<El> list) throws org.gcube.com.fasterxml.jackson.core.JsonProcessingException
      Serializes a list of objects to a JSON string.
      Type Parameters:
      El - The type of the elements in the list.
      Parameters:
      list - The list to serialize.
      Returns:
      The JSON string.
      Throws:
      org.gcube.com.fasterxml.jackson.core.JsonProcessingException - if an error occurs during serialization.
    • marshal

      public static <El extends Element> String marshal(El[] array) throws org.gcube.com.fasterxml.jackson.core.JsonProcessingException
      Serializes an array of objects to a JSON string.
      Type Parameters:
      El - The type of the elements in the array.
      Parameters:
      array - The array to serialize.
      Returns:
      The JSON string.
      Throws:
      org.gcube.com.fasterxml.jackson.core.JsonProcessingException - if an error occurs during serialization.
    • getError

      protected static StringBuffer getError(String unknownType)
      Returns an error message for an unknown type.
      Parameters:
      unknownType - The unknown type.
      Returns:
      The error message.
    • setTypeToBestAvailable

      protected static org.gcube.com.fasterxml.jackson.databind.node.ObjectNode setTypeToBestAvailable(org.gcube.com.fasterxml.jackson.databind.node.ObjectNode objectNode)
      Sets the type of an object to the best available supertype.
      Parameters:
      objectNode - The object node.
      Returns:
      The object node with the updated type.
    • analizeTypes

      protected static org.gcube.com.fasterxml.jackson.databind.JsonNode analizeTypes(org.gcube.com.fasterxml.jackson.databind.node.ObjectNode objectNode)
      Analyzes the types in an object node.
      Parameters:
      objectNode - The object node.
      Returns:
      The analyzed object node.
    • analizeTypes

      protected static org.gcube.com.fasterxml.jackson.databind.node.ArrayNode analizeTypes(org.gcube.com.fasterxml.jackson.databind.node.ArrayNode arrayNode)
      Analyzes the types in an array node.
      Parameters:
      arrayNode - The array node.
      Returns:
      The analyzed array node.
    • unmarshal

      public static <El extends Element> El unmarshal(Class<El> clz, Reader reader) throws org.gcube.com.fasterxml.jackson.core.JsonParseException, org.gcube.com.fasterxml.jackson.databind.JsonMappingException, IOException
      Deserializes an object from a Reader.
      Type Parameters:
      El - The type of the element.
      Parameters:
      clz - The class of the element.
      reader - The reader.
      Returns:
      The deserialized object.
      Throws:
      IOException - if an error occurs during deserialization.
      org.gcube.com.fasterxml.jackson.core.JsonParseException - if an error occurs during JSON parsing.
      org.gcube.com.fasterxml.jackson.databind.JsonMappingException - if an error occurs during JSON mapping.
    • unmarshal

      public static <El extends Element> El unmarshal(Class<El> clz, InputStream stream) throws org.gcube.com.fasterxml.jackson.core.JsonParseException, org.gcube.com.fasterxml.jackson.databind.JsonMappingException, IOException
      Deserializes an object from an InputStream.
      Type Parameters:
      El - The type of the element.
      Parameters:
      clz - The class of the element.
      stream - The input stream.
      Returns:
      The deserialized object.
      Throws:
      IOException - if an error occurs during deserialization.
      org.gcube.com.fasterxml.jackson.core.JsonParseException - if an error occurs during JSON parsing.
      org.gcube.com.fasterxml.jackson.databind.JsonMappingException - if an error occurs during JSON mapping.
    • unmarshal

      public static <El extends Element> El unmarshal(Class<El> clz, String string) throws org.gcube.com.fasterxml.jackson.core.JsonParseException, org.gcube.com.fasterxml.jackson.databind.JsonMappingException, IOException
      Deserializes an object from a JSON string.
      Type Parameters:
      El - The type of the element.
      Parameters:
      clz - The class of the element.
      string - The JSON string.
      Returns:
      The deserialized object.
      Throws:
      IOException - if an error occurs during deserialization.
      org.gcube.com.fasterxml.jackson.core.JsonParseException - if an error occurs during JSON parsing.
      org.gcube.com.fasterxml.jackson.databind.JsonMappingException - if an error occurs during JSON mapping.
    • unmarshalList

      public static <El extends Element> List<El> unmarshalList(Class<El> clz, String string) throws org.gcube.com.fasterxml.jackson.core.JsonParseException, org.gcube.com.fasterxml.jackson.databind.JsonMappingException, IOException
      Deserializes a list of objects from a JSON string.
      Type Parameters:
      El - The type of the elements in the list.
      Parameters:
      clz - The class of the elements.
      string - The JSON string.
      Returns:
      The deserialized list.
      Throws:
      IOException - if an error occurs during deserialization.
      org.gcube.com.fasterxml.jackson.core.JsonParseException - if an error occurs during JSON parsing.
      org.gcube.com.fasterxml.jackson.databind.JsonMappingException - if an error occurs during JSON mapping.
    • unmarshalList

      public static <El extends Element> List<El> unmarshalList(String string) throws org.gcube.com.fasterxml.jackson.core.JsonParseException, org.gcube.com.fasterxml.jackson.databind.JsonMappingException, IOException
      Deserializes a list of objects from a JSON string.
      Type Parameters:
      El - The type of the elements in the list.
      Parameters:
      string - The JSON string.
      Returns:
      The deserialized list.
      Throws:
      IOException - if an error occurs during deserialization.
      org.gcube.com.fasterxml.jackson.core.JsonParseException - if an error occurs during JSON parsing.
      org.gcube.com.fasterxml.jackson.databind.JsonMappingException - if an error occurs during JSON mapping.